Source: https://www.cnblogs.com/xiaoliangup/p/9175932.html
Nginx HTTP configuration mainly includes three Blocks, the structure is as follows:
http {//This is the protocol level
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
gzip on;
server {//This is the server level
listen 80;
server_name localhost;
location / {//This is the request level
root html;
index index.html index.htm;
}
}
}
location section
By specifying the pattern to match the URI requested by the client, the basic The syntax is as follows: location [=|~|~*|^~|@] pattern{……}
1. No modifier means: it must start with the specified pattern, such as :
server {
server_name baidu.com;
location /abc {
……
}
}
Then, the following is correct:
http://baidu.com/abc
http://baidu.com/abc? p1
http://baidu.com/abc/
http://baidu.com/abcde
2,= Means: Must match the specified pattern exactly
server {
server_name sish
location = /abc {
……
}
}
So, the following is correct:
http://baidu.com/abc
http://baidu.com/abc?p1
The following is wrong:
http://baidu.com/abc/
http://baidu.com/abcde
3, ~ means: the specified regular expression should be case sensitive p>
server {
server_name baidu.com;
location ~ ^/abc$ {
……
}
}
So, the following is correct:
http://baidu.com/abc
http://baidu.com/abc?p1=11&p2=22
The following is wrong:
http://baidu.com/ABC
http://baidu.com/abc/
http://baidu.com/abcde
4, ~* means: the specified regular expression is not case sensitive< /p>
server {
server_name baidu.com;
location ~* ^/abc$ {
……
}
}
So, the following is correct:
http://baidu.com/abc
http://baidu..com/ABC
http://baidu..com/abc?p1=11&p2=22
The following is wrong:
http://baidu..com/abc/
http://baidu..com/abcde
5, ^~ Similar to the behavior without modifiers, but also by specifying The pattern starts, the difference is that if the pattern matches,
then stop searching for other patterns.
6. @: Define named location sections. These sections cannot be accessed by client sections, and can only be accessed by internally generated requests, such as try_files or error_page.
Search order and priority
1: Exact match with “=” is prioritized
2: Exact match without modifier
3: Regular expressions are in the order defined in the configuration file
4: With “^~” modifier, the beginning matches
5: With “~” or “~*” modifier, if the regular expression matches the URI
6: Without modifier, If the specified string matches the beginning of the URI
Location section matching example
location = / {
# Only queries that match /.
[ configuration A]
}
location / {
# matches any query starting with /, but regular expressions and some longer strings will be matched first.
[ configuration B]
}
location ^~ /images/ {
# Match any query starting with /images/ and stop searching, without checking regular expressions.
[ configuration C]
}
location ~* \.(gif|jpg|jpeg)$ {
# matches any file ending in gif, jpg, or jpeg, but all requests for the /images/ directory will be in Configuration C
理。
[ configuration D]
} Each
The processing of the request is as follows:
■/ → configuration A
■/documents/document.html → configuration B
■/images/1.gif → configuration C
■/documents/1.jpg → configuration D
The difference between root and alias instructions
location /img/ {
alias /var/www/image/;
}
#If you follow the above configuration, when accessing files in the /img/ directory, ningx will automatically go to the /var/www/image/ directory to find files
pre>
location /img/ {
root /var/www/image;
}
#If you follow this configuration, when accessing files in the /img/ directory, nginx will go to the /var/www/image/img/ directory to find document. ]
alias is the definition of a directory alias, and root is the definition of the top-level directory.
An important difference is that alias must end with “/”, otherwise the file will not be found. . . And root is optional~~
Nginx HTTP configuration mainly includes three blocks, the structure is as follows:
http {//This is the protocol level
include mime.types;
default_type application/octet-stream;
keepalive_timeout 65;
gzip on;
server {//This is the server level
listen 80;
server_name localhost;
location / {//This is the request level
root html;
index index.html index.htm;
}
}
}
server {
server_name baidu.com;
location /abc {
……
}
}
Then, the following is correct:
http://baidu.com/abc
http://baidu.com/abc? p1
http://baidu.com/abc/
http://baidu.com/abcde
server {
server_name sish
location = /abc {
……
}
}
So, the following is correct:
http://baidu.com/abc
http://baidu.com/abc?p1
The following is wrong:
http://baidu.com/abc/
http://baidu.com/abcde
server {
server_name baidu.com;
location ~ ^/abc$ {
……
}
}
So, the following is correct:
http://baidu.com/abc
http://baidu.com/abc?p1=11&p2=22
The following is wrong:
http://baidu.com/ABC
http://baidu.com/abc/
http://baidu.com/abcde
server {
server_name baidu.com;
location ~* ^/abc$ {
……
}
}
So, the following is correct:
http://baidu.com/abc
http://baidu..com/ABC
http://baidu..com/abc?p1=11&p2=22
The following is wrong:
http://baidu..com/abc/
http://baidu..com/abcde
Location section matching example
location = / {
# Only queries that match /.
[ configuration A]
}
location / {
# matches any query starting with /, but regular expressions and some longer strings will be matched first.
[ configuration B]
}
location ^~ /images/ {
# Match any query starting with /images/ and stop searching, without checking regular expressions.
[ configuration C]
}
location ~* \.(gif|jpg|jpeg)$ {
# matches any file ending in gif, jpg, or jpeg, but all requests for the /images/ directory will be in Configuration C
理。
[ configuration D]
} Each
The processing of the request is as follows:
■/ → configuration A
■/documents/document.html → configuration B
■/images/1.gif → configuration C
■/documents/1.jpg → configuration D
location /img/ {
alias /var/www/image/;
}
location /img/ {
root /var/www/image;
}