Nginx - minimum configuration! You deserve it

Migrant worker brother 2021-09-15 08:41:10

A secure server is one that allows only the required number of servers . Ideally , We will build the server based on the smallest system by enabling other functions separately . Minimal configuration also helps debug . If the error is not available in the smallest system , Then add functions respectively , Then continue to search for errors .

This is running nginx Minimum configuration required :

# /etc/nginx/nginx.confevents {}
# event context have to be defined to consider config valid
http {}
server {
listen 80;
server_name javatpoint.co www.javatpoint.co *.javatpoint.co;
return 200 "Hello";
}

Root,Location and try_files Instructions

Root Instructions

root Directive is used to set the root directory of the request , So as to allow nginx Map incoming requests to the file system .

server {
listen 80;
server_name javatpoint.co;
root /var/www/javatpoint.co;
}

It allows the nginx Return server content upon request :

javatpoint.co:80/index.html     # returns /var/www/learnfk.com/index.html
javatpoint.co:80/foo/index.html # returns /var/www/learnfk.com/foo/index.html
Location Instructions

location The instruction is used to request the URI( Uniform resource identifiers ) To set the configuration .

The grammar is :

location [modifier] path

Example :

location /foo {  # ...}

If no modifier is specified , The path is treated as a prefix , Then you can follow anything . The above example will match :

/foo
/fooo
/foo123
/foo/bar/index.html...

We can also use multiple... In a given context location Instructions :

server {
listen 80;
server_name javatpoint.co;
root /var/www/javatpoint.co;
location/{
return 200 "root";
}
location /foo {
return 200 "foo";
}
}
javatpoint.co:80 / # => "root"
javatpoint.co:80 /foo # => "foo"
javatpoint.co:80 /foo123 # => "foo"
javatpoint.co:80 /bar # => "root"

Nginx It also provides some information that can be used with location Modifier used in conjunction with the directive .

Modifier assigned priority :

= - Exact match
^~ - Preferential match
~ && ~* - Regex match
no modifier - Prefix match

First ,nginx All exact matches will be checked . If it doesn't exist , It will look for priorities . If this match also fails , Regular expression matching will be tested in the order in which it appears . If all other operations fail , The last prefix will be used to match .

location /match {
return 200 'Prefix match: will match everything that starting with /match';
}
location ~* /match[0-9] {
return 200 'Case insensitive regex match';
}
location ~ /MATCH[0-9] {
return 200 'Case sensitive regex match';
}
location ^~ /match0 {
return 200 'Preferential match';
}
location = /match {
return 200 'Exact match';
}
/match # => 'Exact match'
/match0 # => 'Preferential match'
/match1 # => 'Case insensitive regex match'
/MATCH1 # => 'Case sensitive regex match'
/match-abc # => 'Prefix match: matches everything that starting with /match'
try\_files Instructions

The instruction tries a different path , And return any path found .

try_files $uri index.html =404;

therefore ,/foo.html Will attempt to return the files in the following order :

$uri(/foo.html);
index.html

If not found :404

If we define in the server context try\_files, Then define where to find all requests , It won't be implemented try\_files. This happens because of... In the server context try\_files Its pseudo position is defined , This pseudo position is the lowest possible specific position . therefore , Definition location/ Will be more specific than our pseudo location .

server {  
try_files $uri /index.html =404;  
location/{ 
 }
}

therefore , We should avoid using... In a server context try_files:

server {  
location/{   
 try_files $uri /index.html =404; 
 }
}

More about Nginx Please refer to the old article for the configuration of :Nginx Common configuration summary ! From entry to work is enough

If you need to learn more about nginx The knowledge system of , You can also click the picture below to enter Nginx Knowledge system column learning .

 picture

Basic service installation 、 Profile Introduction 、 Virtual host configuration practice 、Nginx Optimization configuration details 、LNMP framework Nginx Reverse proxy load balancing configuration 、Nginx+Tomcat Multiple instances and load balancing configuration 、 High availability 、Nginx Version smooth upgrade and rollback 、Nginx Current limiting configuration 、Nginx Log production practice 、Nginx Introduction to online configuration file generation tools, etc .

author :GeekGay link :imooc.com/article/319107

Please bring the original link to reprint ,thank
Similar articles

2021-09-15

2021-09-15