nginx location
一、location的作用
location指令的作用是根据请求的URL来执行不同的应用,其实就是根据用户请求的网站地址URL进行匹配。匹配成功即进行相关的操作。
二、location语法
location的使用语法为:
1 2 3 | location [ = | ~ | ~* | ^~ ] uri { ... } |
对location语法列表说明
1 2 | location [=|~|~*\^~|@] uri {...} 指令 匹配标识 匹配的网站网址 匹配URI后要执行的字段 |
= 开头表示精确匹配
^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。
~ 开头表示区分大小写的正则匹配
~*开头表示不区分大小写的正则匹配
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则
/ 通用匹配,任何请求都会匹配到。
多个location配置的情况下匹配顺序为(参考资料而来,还未实际验证,试试就知道了,不必拘泥,仅供参考):
首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。
URI可以是普通的字符串地址路径,或者是正则表达式,匹配成功则执行后面大括号里的相关指令
三、location匹配示例
示例如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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作为方向代理服务器存在。
所以实际使用中,个人觉得至少有三个匹配规则定义,如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。 #这里是直接转发给后端应用服务器了,也可以是一个静态首页 # 第一个必选规则 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的配置文件如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | [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并测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | [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}\n" http: //www.dmtest1.com 402 [root@nginx extra]# curl -s -o /dev/ null -I -w "%{http_code}\n" http: //www.dmtest1.com/ 402 [root@nginx extra]# curl -s -o /dev/ null -I -w "%{http_code}\n" http: //www.dmtest1.com/index.html 401 [root@nginx extra]# curl -s -o /dev/ null -I -w "%{http_code}\n" http: //www.dmtest1.com/documents/document.html 403 [root@nginx extra]# curl -s -o /dev/ null -I -w "%{http_code}\n" http: //www.dmtest1.com/images/1.jpg 404 [root@nginx extra]# curl -s -o /dev/ null -I -w "%{http_code}\n" http: //www.dmtest1.com/documents/1.jpg 500 [root@nginx extra]# curl -s -o /dev/ null -I -w "%{http_code}\n" http: //www.dmtest1.com/dm/ 401 [root@nginx extra]# curl -s -o /dev/ null -I -w "%{http_code}\n" http: //www.dmtest1.com/test/ 401 |
更多详细信息请参考:https://www.cnblogs.com/kevingrace/p/6804429.html
******************************我也想难过的时候到海边走走,可是我的城市没有海。******************************
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY