Detailed instructions nginx location

Source: https://www.cnblogs.com/xiaoliangup/p/9175932.html

Copy the code
Nginx the HTTP configuration includes three blocks, the following structure: 
HTTP protocol level {// This is 
  the include the mime.types; 
  default_type file application / OCTET-Stream; 
  keepalive_timeout 65; 
  the gzip ON; 
    Server {// This is the server level 
      listen 80 ; 
      server_name localhost; 
        LOCATION / {// the request level is 
          the root HTML; 
          index index.html index.htm; 
        } 
      } 
}
Copy the code

location section

Request with a client by specifying the URI matches the pattern, the basic syntax is as follows: LOCATION [= | ~ | ~ * | ^ ~ | @] ...... pattern {}

1, there is no modifier, said: must begin with a specified pattern, such as:

Copy the code
server {
  server_name baidu.com;
  location /abc {
    ……
  }
}

So, the following is right:
http://baidu.com/abc
http://baidu.com/abc?p1
http://baidu.com/abc/
http://baidu.com/abcde

Copy the code

 

2, represents =: must exactly match the specified pattern

Copy the code
{Server 
server_name SISH 
  LOCATION = / ABC { 
    ...... 
  } 
} 
Then, the following is: 
http://baidu.com/abc 
http://baidu.com/abc?p1 
following is wrong: 
HTTP: // baidu .com / abc / 
http://baidu.com/abcde
Copy the code

 

3, ~ said: specified regular expression is case sensitive

Copy the code
{Server 
server_name Baidu.com; 
  LOCATION ~ ^ / ABC $ { 
    ...... 
  } 
} 
Then, the following is: 
http://baidu.com/abc 
http://baidu.com/abc?p1=11&p2=22 
follows is wrong: 
http://baidu.com/ABC 
http://baidu.com/abc/ 
http://baidu.com/abcde
Copy the code

 

4, ~ * said: specified regular expression case insensitive

Copy the code
{Server 
server_name Baidu.com; 
LOCATION ~ ^ * / {$ ABC 
    ...... 
  } 
} 
Then, the following is: 
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
Copy the code

 

5, no modifiers ^ ~ similar behavior, but also began to specify the mode, except that, if the pattern matches,
then stop searching for the other modes.
6, @: define named location segments which segments can not access the customer can only be generated by the internal request
seeking access to, such as try_files or the like error_page

And priority search order
1: an exact match with the "=" Priority
2: No exact match modifier
3: regular expressions in the order they are defined in the configuration file
4: with "^ ~" modifier, match beginning
5: with "~" or "~ *" modifier, if the regular expression matching URI
6: no modifier, if the URI matches the specified string at the beginning of

Copy the code
Location exemplary block matching

LOCATION = / {   # only match / query.   [Configuration A] } LOCATION / {   # matches any query / start, but the regular expression and some of the longer string is matched first.   [Configuration B] } LOCATION ^ ~ / images / {   # matches any / starting query / images and stop searching, regular expressions are not checked.   [Configuration C] } LOCATION ~ * \. (GIF | JPG | JPEG) {$   # matches any, jpg, or gif jpeg files to the end, but all requests / images / directory will Configuration C at the   treatment.   [Configuration D] } various processing request in the following example: ■ / Configuration → A ■ / Documents / document.html Configuration → B ■ / Images / 1.gif Configuration → C ■ / Documents / 1.jpg Configuration → D
Copy the code

 

root, alias command difference

location / img / { 
    alias / var / www / image /; 
}
# If the above configuration in accordance with the words, / img / directory of the file is accessed, ningx will automatically go to / var / www / image / directory to find the file
location /img/ {
    root /var/www/image;
}
When # If in this configuration, then the files in the / img / directory access, nginx will go to look for the files in / var / www / image / img / directory. ]

alias alias is a defined directory, root is defined in the top directory.

Another important difference is that behind the alias must use the "/" end, otherwise it will not find the file. . . The root is optional ~~

Guess you like

Origin www.cnblogs.com/laijinquan/p/11227319.html