我们在讲解Nginx 配置文件详解中, 把Nginx的主配置文件分成了三部分,如图:
今天就带着大家继续学习Nginx主配置文件第三部分http块中的location指令
location是Nginx中的块级指令(block directive),location指令的功能是用来匹配不同的url请求,进而对请求做不同的处理和响应,这其中较难理解的是多个location的匹配顺序,本文会作为重点来解释和说明。
开始之前先明确一些约定,我们输入的网址叫做请求URI,nginx用请求URI与location中配置的URI做匹配。
location有两种匹配规则:
(1)“=”,精确匹配
- location = /abc/ {
- .....
- }
-
- # 只匹配http://abc.com/abc
- #http://abc.com/abc [匹配成功]
- #http://abc.com/abc/index [匹配失败]
-
(2)“~”,执行正则匹配,区分大小写。
- location ~ /Abc/ {
- .....
- }
- #http://abc.com/Abc/ [匹配成功]
- #http://abc.com/abc/ [匹配失败]
-
(3)“~*”,执行正则匹配,忽略大小写
- location ~* /Abc/ {
- .....
- }
- # 则会忽略 uri 部分的大小写
- #http://abc.com/Abc/ [匹配成功]
- #http://abc.com/abc/ [匹配成功]
-
(4)“^~”,表示普通字符串匹配上以后不再进行正则匹配。
- location ^~ /index/ {
- .....
- }
- #以 /index/ 开头的请求,都会匹配上
- #http://abc.com/index/index.page [匹配成功]
- #http://abc.com/error/error.page [匹配失败]
-
(5)不加任何规则
- location /index/ {
- ......
- }
- #http://abc.com/index [匹配成功]
- #http://abc.com/index/index.page [匹配成功]
- #http://abc.com/test/index [匹配失败]
- #http://abc.com/Index [匹配失败]
- # 匹配到所有uri
-
-
(6)“@”,nginx内部跳转
- location /index/ {
- error_page 404 @index_error;
- }
- location @index_error {
- .....
- }
- #以 /index/ 开头的请求,如果链接的状态为 404。则会匹配到 @index_error 这条规则上。
-
=>^~>~ | ~*>最长前缀匹配>/
- location = / {
- #规则A
- }
-
- location = /login {
- #规则B
- }
-
- location ^~ /static/ {
- #规则C
- }
-
- location ~ \.(gif|jpg|png|js|css)$ {
- #规则D
- }
-
- location ~* \.png$ {
- #规则E
- }
-
- location !~ \.xhtml$ {
- #规则F
- }
-
- location !~* \.xhtml$ {
- #规则G
- }
-
- location / {
- #规则H
- }
-
匹配结果:
- 访问根目录/, 比如http://localhost/ 将匹配规则A
-
- 访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H
-
- 访问 http://localhost/static/a.html 将匹配规则C
-
- 访问 http://localhost/b.jpg 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用, 而 http://localhost/static/c.png 则优先匹配到 规则C
-
- 访问 http://localhost/a.PNG 则匹配规则E, 而不会匹配规则D,因为规则E不区分大小写。
-
- 访问 http://localhost/a.xhtml 不会匹配规则F和规则G,http://localhost/a.XHTML不会匹配规则G,因为不区分大小写。规则F,规则G属于排除法,符合匹配规则但是不会匹配到。
-
- 访问 http://localhost/qll/id/1111 则最终匹配到规则H,因为以上规则都不匹配。
-
举个例子:
- server {
- listen 9001;
- server_name www.abc.com;
-
- location ~ /edu {
- proxy_pass http://127.0.0.1:8080;
- }
- }
-
我们访问www.abc.com:9001/edu,看下效果
访问 /edu 时,服务器首先去找edu文件,找不到则将edu当做目录,重定向到 /edu/,在该目录下找默认文件。
但是如果想这两种请求对应不同的处理,就要明确增加不带/结尾的location配置。例如:
- location /doc {
- proxy_pass http://www.doc123.com
- }
- location /doc/ {
- proxy_pass http://www.doc456.com
- }
-