Location configuration of nginx
Location configuration of Nginx
Install the third-party module echo-nginx-module
# git clone https://github.com/openresty/echo-nginx-module.git
#. / configure-add-module=/path/to/echo-nginx-module
Location syntax
# location [= | ^ ~ | ~ | ~] / uri/ {… . }
# coding.net/u/aminglinux/p/nginx/git/blob/master/location/ruler.md
Location priority and case
# = higher than ^ ~ higher than ~ equal to higher than /
# coding.net/u/aminglinux/p/nginx/git/blob/master/location/priority.md
Location configuration of nginx
Nginx location grammar rules: location [= | ~ | ~ * | ^ ~] / uri/ {… }
The location matching variable of nginx is $uri
Symbol description
= indicates an exact match
^ ~ indicates that uri begins with a specified character or string
~ indicates case-sensitive regular matching
~ * indicates a case-insensitive regular match
/ Universal match, any request will match to
Rule priority
= higher than ^ ~ higher than * equals to higher than /
Rule exampl
Location = "/ 12.jpg" {...}
Such as:
Www.aminglinux.com/12.jpg matching
Www.aminglinux.com/abc/12.jpg mismatch
Location ^ ~ "/ abc/" {...}
Such as:
Www.aminglinux.com/abc/123.html matching
Www.aminglinux.com/a/abc/123.jpg mismatch
Location ~ "png" {...}
Such as:
Www.aminglinux.com/aaa/bbb/ccc/123.png matching
Www.aminglinux.com/aaa/png/123.html matching
Location ~ * "png" {...}
Such as:
Www.aminglinux.com/aaa/bbb/ccc/123.PNG matching
Www.aminglinux.com/aaa/png/123.html matching
Location / admin/ {...}
Such as:
Www.aminglinux.com/admin/aaa/1.php matching
Www.aminglinux.com/123/admin/1.php mismatch
Common sense
Some materials introduce that location support does not match!
Such as: location! ~ 'png' {...}
This is wrong, location does not support! ~
If there is such a requirement, it can be realized through if.
For example, if ($uri! ~ 'png') {.}
Note: location priority is less than if
Nginx location priority
= higher than ^ ~ higher than * equals to higher than /
Contrast / and ~
Example 1:
Server {
Listen 80
Server_name www.aminglinux.com
Root / tmp/123.com
Location / abc/ {echo "/";} location ~ 'abc' {echo ";}
}
Test command: curl-x127.0.0.1 purl 80 'www.aminglinux.com/abc/1.png'
The result is: ~
Compare ~ and ~ *
Example 2:
Server
{
Listen 80
Server_name www.aminglinux.com
Root / tmp/123.com
Location ~ 'abc' {echo' ~';} location ~ * 'abc' {echo' ~ *;}
}
Test command: curl-x127.0.0.1 purl 80 'www.aminglinux.com/abc/123.html'
The result is: ~
Example 3:
Server
{
Listen 80
Server_name www.aminglinux.com
Root / tmp/123.com
Location ~ * 'abc' {echo' ~ *;} location ~ 'abc' {echo';}
}
Test command: curl-x127.0.0.1 purl 80 'www.aminglinux.com/abc/123.html'
The result is ~ *
Conclusion: the priority of ~ and ~ * is actually the same. If both appear at the same time, which location comes first and which takes effect in the configuration file.
Compare ^ ~ and ~
Example 4:
Server
{
Listen 80
Server_name www.aminglinux.com
Root / tmp/123.com
Location ~'/ abc' {echo'~';} location ^'/ abc' {echo'^';}
}
Test command: curl-x127.0.0.1 purl 80 'www.aminglinux.com/abc/123.html
The result is: ^ ~
Contrast = and ^ ~
Example 5:
Server
{
Listen 80
Server_name www.aminglinux.com
Root / tmp/123.com
Location ^'/ abc.html' {echo'^';} location ='/ abc.html' {echo'=';}
}
Test command: curl-x127.0.0.1 purl 80 'www.aminglinux.com/abc.html
The result is: =