NGINX configuration issues

Pit one. Static file server: the difference between location root and alias:

The main difference between root and alias is how nginx interprets the uri behind location, which will cause the two to map requests to server files in different ways:

  • The processing result of root is: root path + location path
  • The processing result of alias is: replace the location path with the alias path
  • alias is the definition of a directory alias, and root is the definition of the top-level directory.
  • It must be ended with "/" after the alias, otherwise the file will not be found. . . And root is optional ~~

root instance:

location ^~ /t/ {
     root /www/root/html/;
}

Note: If a requested URI is /t/a.html, the web server will return the /www/root/html/t/a.html file on the server.

alias instance (last / must bring):

location ^~ /t/ {
 alias /www/root/html/new_t/;
}

Note: If a requested URI is /t/a.html, the web server will return the /www/root/html/new_t/a.html file on the server. Note that this is new_t, because alias will discard the path configured after location and point the currently matched directory to the specified directory.

Pit two. NGINX location priority in configuration

1.location expression type:

  • ~ Means perform a regular match, case sensitive
  • ~ * Means perform a regular match, not case sensitive
  • ^ ~ Indicates common character matching. Use prefix matching. If the match is successful, it will no longer match other locations.
  • = Perform exact character matching. That is an exact match.
  • @ It defines a named location, used in internal orientation, such as error_page, try_files

2. Location priority description

The location of nginx is not much related to the order of location in the configuration. The type of positive location expression is related. For expressions of the same type, long strings will match first.
The following is a list of priorities:

  • The equal sign type (=) has the highest priority. Once the match is successful, no other matches are found.
  • ^ ~ Type expression. Once the match is successful, no other matches are found.
  • Regular expression types (~~ *) take precedence. If the regularity of multiple locations can match, the longest regular expression is used.
  • Regular string matching type. Match by prefix.
    Insert picture description here
Published 41 original articles · Liked 14 · Visitors 10,000+

Guess you like

Origin blog.csdn.net/Yunwei_Zheng/article/details/104018807