关于nginx的URI匹配规则,nginx的官方文档中有这么一句话:
写道
If there are several matching location blocks nginx selects the one with the longest prefix.
也就是说,如果nginx的配置文件有多个location指令,那么匹配请求URI前缀最长的那个location指令将负责处理该请求。
下面举个真实的例子来帮助大家对该规则有更直观的理解。
此例中有以下location指令:
location / {
root /var/www;
index index.html index.htm;
}
location /cgit {
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME /var/www/cgit/cgit.cgi;
fastcgi_param PATH_INFO $uri;
fastcgi_pass 127.0.0.1:8999;
}
location = /404.html {
root /usr/share/nginx/html;
}
那么对于请求/cgit-css/cgit.css,它不会被 location /指令截获,而被location /cgit截获,因为该指令能够配最长的URI前缀。以下是nginx的关于URI和location指令匹配的debug日志:
2014/04/09 16:26:35 [debug] 4876#0: *57 http request line: "GET /cgit-css/cgit.css HTTP/1.1"
2014/04/09 16:26:35 [debug] 4876#0: *57 http uri: "/cgit-css/cgit.css"
2014/04/09 16:26:35 [debug] 4876#0: *57 http args: ""
2014/04/09 16:26:35 [debug] 4876#0: *57 http exten: "css"
2014/04/09 16:26:35 [debug] 4876#0: *57 http process request header line
2014/04/09 16:26:35 [debug] 4876#0: *57 http header: "Host: vmcentos64"
2014/04/09 16:26:35 [debug] 4876#0: *57 http header: "Accept-Encoding: gzip, deflate"
2014/04/09 16:26:35 [debug] 4876#0: *57 http header: "Connection: keep-alive"
2014/04/09 16:26:35 [debug] 4876#0: *57 http header: "Accept: text/css,*/*;q=0.1"
2014/04/09 16:26:35 [debug] 4876#0: *57 http header: "If-Modified-Since: Wed, 09 Apr 2014 14:25:02 GMT"
2014/04/09 16:26:35 [debug] 4876#0: *57 http header: "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_1) AppleWebKit/537.73.11 (KHTML, like Gecko) Version/7.0.1 Safari/537.73.11"
2014/04/09 16:26:35 [debug] 4876#0: *57 http header: "Accept-Language: zh-cn"
2014/04/09 16:26:35 [debug] 4876#0: *57 http header: "Referer: http://vmcentos64/cgit/"
2014/04/09 16:26:35 [debug] 4876#0: *57 http header: "Cache-Control: max-age=0"
2014/04/09 16:26:35 [debug] 4876#0: *57 http header done
2014/04/09 16:26:35 [debug] 4876#0: *57 event timer del: 3: 1189284449
2014/04/09 16:26:35 [debug] 4876#0: *57 generic phase: 0
2014/04/09 16:26:35 [debug] 4876#0: *57 rewrite phase: 1
2014/04/09 16:26:35 [debug] 4876#0: *57 http script regex: "^/$"
2014/04/09 16:26:35 [notice] 4876#0: *57 "^/$" does not match "/cgit-css/cgit.css", client: 192.168.56.1, server: _, request: "GET /cgit-css/cgit.css HTTP/1.1", host: "vmcentos64", referrer: "http://vmcentos64/cgit/"
2014/04/09 16:26:35 [debug] 4876#0: *57 test location: "/"
2014/04/09 16:26:35 [debug] 4876#0: *57 test location: "50x.html"
2014/04/09 16:26:35 [debug] 4876#0: *57 test location: "cgit"
2014/04/09 16:26:35 [debug] 4876#0: *57 using configuration "/cgit"
以上日志开始部分说明了HTTP请求的URI,中间是HTTP头信息,最后一行说明了那个location指令匹配了。由此可见,上述nginx配置会导致对css文件的请求转发到CGI程序上去了,这显然是不正确的。这种错误的配置的根源是对nginx的location匹配规则没有真正的理解。
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com