nginx location

一、location的作用

 location指令的作用是根据请求的URL来执行不同的应用,其实就是根据用户请求的网站地址URL进行匹配。匹配成功即进行相关的操作。

二、location语法

location的使用语法为:

location    [ = | ~ | ~* | ^~ ] uri {
    ...
}

 对location语法列表说明

location     [=|~|~*^~|@]            uri                        {...}
  指令          匹配标识            匹配的网站网址        匹配URI后要执行的字段

 = 开头表示精确匹配

^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。

~ 开头表示区分大小写的正则匹配

~*开头表示不区分大小写的正则匹配

!~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则

/ 通用匹配,任何请求都会匹配到。

多个location配置的情况下匹配顺序为(参考资料而来,还未实际验证,试试就知道了,不必拘泥,仅供参考):

首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。

URI可以是普通的字符串地址路径,或者是正则表达式,匹配成功则执行后面大括号里的相关指令

三、location匹配示例

示例如下:

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/a.gif, 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/category/id/1111 则最终匹配到规则H,因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。

所以实际使用中,个人觉得至少有三个匹配规则定义,如下:

#直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。  
#这里是直接转发给后端应用服务器了,也可以是一个静态首页  
# 第一个必选规则  
location = / {  
    proxy_pass http://tomcat:8080/index  
}  
   
# 第二个必选规则是处理静态文件请求,这是nginx作为http服务器的强项  
# 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用  
location ^~ /static/ {  
    root /webroot/static/;  
}  
location ~* .(gif|jpg|jpeg|png|css|js|ico)$ {  
    root /webroot/res/;  
}  
   
#第三个规则就是通用规则,用来转发动态请求到后端应用服务器  
#非静态文件请求就默认是动态请求,自己根据实际把握  
#毕竟目前的一些框架的流行,带.php,.jsp后缀的情况很少了  
location / {  
    proxy_pass http://tomcat:8080/  
}

 四、location匹配实战

下面是官方给出的location示例,我们通过该示例来验证不同的location标签生效的顺序。nginx的配置文件如下:

[root@nginx extra]# cat dmtest1.conf 
    server {
        listen       80;
        server_name  www.dmtest1.com dmtest1.com;
        location / {
            root   html/dmtest1;
			location / {
				return 401;
			}		
			
			location = / {
				return 402;
			}		
			location /documents/ {
				return 403;
			}		
			location ^~ /images/ {
				return 404;
			#匹配任何以 /images/ 开头的查询并停止搜索。任何正则表达式匹配将不会被检查。
			#"^~" 这个前缀的作用是在常规的字符串匹配检查之后,不做正则表达式的检查,即如果最明确的那个字符串匹配的location配置中有此前缀,那么不会做正则表达式的检查。
			}		
			location ~* .(gif|jpg|jpeg)$ {
				return 500;
			#匹配以任何gif、jip或jpeg结尾的请求。
			}		

        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
		#access_log logs/access_dmtest1.log main;
		access_log logs/access_dmtest1.log main gzip buffer=32k flush=5s;
		#access_log off;
    }
[root@nginx extra]# cat dmtest1.conf 
    server {
        listen       80;
        server_name  www.dmtest1.com dmtest1.com;
        location / {
            root   html/dmtest1;
			location / {
				return 401;
			}		
			
			location = / {
				return 402;
			}		
			location /documents/ {
				return 403;
			}		
			location ^~ /images/ {
				return 404;
			#匹配任何以 /images/ 开头的查询并停止搜索。任何正则表达式匹配将不会被检查。
			#"^~" 这个前缀的作用是在常规的字符串匹配检查之后,不做正则表达式的检查,即如果最明确的那个字符串匹配的location配置中有此前缀,那么不会做正则表达式的检查。
			}		
			location ~* .(gif|jpg|jpeg)$ {
				return 500;
			#匹配以任何gif、jip或jpeg结尾的请求。
			}		

        }
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
		#access_log logs/access_dmtest1.log main;
		access_log logs/access_dmtest1.log main gzip buffer=32k flush=5s;
		#access_log off;
    }

 检查语法,重载nginx并测试

[root@nginx extra]# ../../sbin/nginx -t
nginx: the configuration file /application/nginx-1.8.1/conf/nginx.conf syntax is ok
nginx: configuration file /application/nginx-1.8.1/conf/nginx.conf test is successful

[root@nginx extra]# systemctl reload nginx

[root@nginx extra]# curl -s -o /dev/null -I -w "%{http_code}
" http://www.dmtest1.com
402
[root@nginx extra]# curl -s -o /dev/null -I -w "%{http_code}
" http://www.dmtest1.com/
402
[root@nginx extra]# curl -s -o /dev/null -I -w "%{http_code}
" http://www.dmtest1.com/index.html
401
[root@nginx extra]# curl -s -o /dev/null -I -w "%{http_code}
" http://www.dmtest1.com/documents/document.html
403
[root@nginx extra]# curl -s -o /dev/null -I -w "%{http_code}
" http://www.dmtest1.com/images/1.jpg
404
[root@nginx extra]# curl -s -o /dev/null -I -w "%{http_code}
" http://www.dmtest1.com/documents/1.jpg
500
[root@nginx extra]# curl -s -o /dev/null -I -w "%{http_code}
" http://www.dmtest1.com/dm/
401
[root@nginx extra]# curl -s -o /dev/null -I -w "%{http_code}
" http://www.dmtest1.com/test/
401

 更多详细信息请参考:https://www.cnblogs.com/kevingrace/p/6804429.html

原文地址:https://www.cnblogs.com/Mr-Ding/p/9545432.html