Configuration instructions for nginx static resources
listen instruction
listen: used to configure the listening port.
grammar | listen address[:port] [default_server]...; listen port [default_server]...; |
---|---|
default | listen *:80 | *:8000 |
position | server |
The setting of listen is flexible. Let's familiarize ourselves with the common setting methods through several examples:
listen 127.0.0.1:8000; // listen localhost:8000 listens to the specified IP and port
listen 127.0.0.1; Listen to all ports of the specified IP
listen 8000; Listens for connections on the specified port
listen *:8000; Listens for connections on the specified port
default_ The server property is an identifier used to set this virtual host as the default host. The so-called default host means that it will be executed by default if the corresponding address:port is not matched. If not specified, the first server is used by default.
server{ listen 8080; server_name 127.0.0.1; location /{ root html; index index.html; } } server{ listen 8080 default_server; server_name localhost; default_type text/plain; return 444 'This is a error request'; }
Server_name instruction
server_name: used to set the virtual host service name.
127.0.0.1, localhost, domain name [www.baidu.com | www.jd.com]
grammar | server_name name ...; name can provide multiple, separated by spaces |
---|---|
Default value | server_name ""; |
position | Server |
About server_name can be configured in three ways:
Exact match Wildcard matching Regular Expression Matching
Configuration mode 1: accurate matching
For example:
server { listen 80; server_name www.itcast.cn www.itheima.cn; ... }
Configuration method 2: use wildcard configuration
server_ The wildcard "*" is supported in name, but it should be noted that the wildcard cannot appear in the middle of the domain name, but only in the first or last paragraph, such as:
server { listen 80; server_name *.itcast.cn www.itheima.*; # www.itcast.cn abc.itcast.cn www.itheima.cn www.itheima.com ... }
The following configuration will report an error:
server { listen 80; server_name www.*.cn www.itheima.c* ... }
Configuration 3: use regular expression configuration
server_ Regular expressions can be used in name, and ~ is used as the starting mark of the regular expression string.
Common regular expressions
code | explain |
---|---|
^ | Match search string start position |
$ | Match search string end position |
. | Matches any single character except newline \ n |
\ | Escape character, marking the next character as a special character |
[xyz] | A character set that matches any specified character |
[a-z] | Character range that matches any character within the specified range |
\w | Match A-Z a-z 0-9 and underscore with any of the following characters, equivalent to [A-Za-z0-9_] |
\d | Numeric character matching, equivalent to [0-9] |
{n} | Exactly match n times |
{n,} | Match at least n times |
{n,m} | Match at least n times and at most m times |
* | Zero or more times, equivalent to {0,} |
+ | One or more times, equivalent to {1,} |
? | Zero or once, equivalent to {0,1} |
The configuration is as follows:
server{ listen 80; server_name ~^www\.(\w+)\.com$; default_type text/plain; return 200 $1 $2 ..; } be careful ~No space can be added after it. Parentheses can take values
Match execution order
Due to server_ The name directive supports wildcards and regular expressions. Therefore, in a configuration file containing multiple virtual hosts, a server whose name is used by multiple virtual hosts may appear_ Name matching is successful. In this case, who will handle the current request?
server{ listen 80; server_name ~^www\.\w+\.com$; default_type text/plain; return 200 'regex_success'; } server{ listen 80; server_name www.itheima.*; default_type text/plain; return 200 'wildcard_after_success'; } server{ listen 80; server_name *.itheima.com; default_type text/plain; return 200 'wildcard_before_success'; } server{ listen 80; server_name www.itheima.com; default_type text/plain; return 200 'exact_success'; } server{ listen 80 default_server; server_name _; default_type text/plain; return 444 'default_server not found server'; } Conclusion: exact_success wildcard_before_success wildcard_after_success regex_success default_server not found server!!
No1:Accurate matching server_name No2:Wildcards match at the beginning server_name success No3:Wildcards match at the end server_name success No4:Regular Expression Matching server_name success No5:Default default_server Processing. If no default is specified, find the first one server
location instruction
server{ listen 80; server_name localhost; location / { } location /abc{ } ... }
location: the URI used to set the request
grammar | location [= | ~ | ~* | ^~ |@ ]uri {...} |
---|---|
Default value | - |
position | server,location |
The uri variable is the request string to be matched, which can contain neither regular expression nor regular expression. When searching for a matching location, the nginx server first uses the one that does not contain regular expression to find the one with the highest matching degree, and then matches through the one that contains regular expression. If it can be matched to direct access, If no match is found, the location with the highest matching degree is used to process the request.
Attribute introduction:
Without symbol, it is required to start in the specified mode
server { listen 80; server_name 127.0.0.1; location /abc{ default_type text/plain; return 200 "access success"; } } The following access is correct http://192.168.200.133/abc http://192.168.200.133/abc?p1=TOM http://192.168.200.133/abc/ http://192.168.200.133/abcdef
=: must exactly match the specified pattern before using for URIs that do not contain regular expressions
server { listen 80; server_name 127.0.0.1; location =/abc{ default_type text/plain; return 200 "access success"; } } Can match to http://192.168.200.133/abc http://192.168.200.133/abc?p1=TOM Cannot match http://192.168.200.133/abc/ http://192.168.200.133/abcdef
~: used to indicate that the current uri contains regular expressions and is case sensitive
~*: used to indicate that the current uri contains regular expressions and is case insensitive
In other words, if the uri contains a regular expression, it needs to be identified by the above two matches
server { listen 80; server_name 127.0.0.1; location ~^/abc\w${ default_type text/plain; return 200 "access success"; } } server { listen 80; server_name 127.0.0.1; location ~*^/abc\w${ default_type text/plain; return 200 "access success"; } }
^~: before using for URIs that do not contain regular expressions, the functions are the same as those without symbols. The only difference is that if the patterns match, the search for other patterns will stop.
server { listen 80; server_name 127.0.0.1; location ^~/abc{ default_type text/plain; return 200 "access success"; } }
Set the directory root / alias of the requested resource
Root: sets the root directory of the request
grammar | root path |
---|---|
Default value | root html; |
position | Http,server,location |
Path is the root directory path for the Nginx server to find resources after receiving the request.
alias: the URI used to change the location
grammar | alias path |
---|---|
Default value | — |
position | location |
Path is the modified root path.
Nginx sets the virtual directory through alias. In nginx configuration, the alias directory is different from the root directory. The above two instructions can specify the path to access resources. What is the difference between the two?
For example:
(1) Create an images directory in the / usr/local/nginx/html directory, and put a picture mv.png in the directory
location /images { root /usr/local/nginx/html; }
The path to access the picture is:
http:localhost/images/mv.png
(2) If you change root to alias
location /images { alias /usr/local/nginx/html; }
When you visit the above address again, an Error 404 will appear on the page. If you check the error log, you will find that the address is wrong, so it is verified that:
root The result is: root route+location route /usr/local/nginx/html/images/mv.png alias The result is:use alias Path replacement location route /usr/local/nginx/html/images
You need to change the path after alias to
location /images { alias /usr/local/nginx/html/images; }
(3) If the location path ends with /, the alias must also end with /, which is not required by root
Change the above configuration to
location /images/ { alias /usr/local/nginx/html/images; }
Access will cause problems. Check the error log or the path is wrong, so you need to add alias after it/
(4) As an example:
server { listen 80; server_name www.test.com; index index.html index.php index.htm; access_log /usr/local/nginx/logs/image.log; location / { root /var/www/html; } location /haha { //The matching path directory haha does not need to exist in the directory specified by alias alias /var/www/html/ops/; //Be sure to bring the "/" symbol after it rewrite ^/opp/hen.php(.*)$ /opp/hen.php?s=$1 last; # rewrite ^/opp/(.*)$ /opp/hen.php?s=$1 last; } location /wang { //The matching path directory wang must actually exist in the directory specified by root (there must be a wang directory under / var/www/html) root /var/www/html; } }
Summary:
root The result is: root route+location route alias The result is:use alias Path replacement location route alias Is the definition of a directory alias, root Is the meaning of the top-level directory. If location The path is/ending,be alias It must also be/ending, root No requirement So, in general, in nginx Good habits in configuration are: 1)stay location /Medium configuration root catalogue 2)stay location /path Medium configuration alias Virtual directory.
index instruction
index: set the default home page of the website
grammar | index file ...; |
---|---|
Default value | index index.html |
position | Http,server,location |
index can be followed by multiple settings. If no specific resources are specified during access, they will be searched in turn until the first one is found.
For example:
location / { root /usr/local/nginx/html; index index.html index.htm; } Access the location You can pass http://ip:port /, if nothing is added after the address, by default, access index.html and index.htm in turn, and find the first one to return
error_page instruction
error_page: set the error page of the website
grammar | error_page code ...[=[response]] uri; |
---|---|
Default value | — |
position | http,server,location |
How to handle when the corresponding response code appears.
For example:
(1) You can specify a specific jump address
server { error_page 404 http://www.itcast.cn; }
(2) You can specify a redirect address
server{ error_page 404 /50x.html; error_page 500 502 503 504 /50x.html; location =/50x.html{ root html; } }
(3) Use the @ match of location to complete the display of error information
server{ error_page 404 @jump_to_error; location @jump_to_error { default_type text/plain; return 404 'Not Found Page...'; } }
The optional option = [response] is used to change the corresponding code to another one
server{ error_page 404 =200 /50x.html; location =/50x.html{ root html; } } In this way, when the corresponding resource cannot be found in the return 404, it can be seen in the browser,The final returned status code is 200. You need to pay attention to this and write it error_page For the following content, 404 needs to be followed by a space, and 200 cannot be preceded by a space