nginx的location优先级
server { listen 80; server_name aa.lile.com; root /data/lile; index index.html index.htm index.php; charset utf-8; location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } access_log on; access_log /var/log/lile.com; sendfile off; location ~ \.(gif|jpg|png|css|js|swf|plist|xml)$ { expires 90d; } location / { add_header Access-Control-Allow-Origin *; } }
但是去访问的时候,返回的结果为:

并没有返回过期时间,猜测这是因为location这里的匹配是有优先级的
例1:
server { listen 80; server_name aaaa.lile.com; root /data/lile; index index.html index.htm index.php; charset utf-8; location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } access_log on; access_log /var/log/lile.com; sendfile off; location = /images/test.png { return 001; } location /images/test.png { return 002; } location \/images\/test\.png$ { return 003; } location = / { index index.html; } location / { return 005; } }
测试1:精准匹配

测试2:
这个按理说是应该要精准匹配到index.html,但是匹配到了返回005的这里,这是因为http://aaaa.lile.com会跳转到http://aaaa.lile.com/index.com

例2:
server { listen 80; server_name aaaaa.lile.com; root /data/lile; index index.html index.htm index.php; charset utf-8; location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } access_log on; access_log /var/log/lile.com; sendfile off; location /images/test.png { return 001; } location ^~ /images/ { return 002; } location ~ \/images\/test\.png$ { return 003; } location ~ \/images\/ { return 004; } }
测试1:

当匹配到001时,只是做记录,后面还要正则匹配,正则匹配到了003,若在正则匹配的时候没有匹配到,那么就会返回001
例3:
server { listen 80; server_name aaaaa.lile.com; root /data/lile; index index.html index.htm index.php; charset utf-8; location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } access_log on; access_log /var/log/lile.com; sendfile off; location /images/ { return 001; } location ^~ /images/test.png { return 002; } location ~ /images/test\.png$ { return 003; } location ~ \/images\/ { return 004; } }
测试1:

例4:
server { listen 80; server_name aa.lile.com; index index.html index.htm index.php; charset utf-8; location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } access_log on; access_log /var/log/lile.com; sendfile off; location ~* \.jpg$ { return 3; } location ^~ /a { return 4; } location /a/1.jpg { return 5; } location / { return 7; } }
测试1:

与

为什么不一样?
匹配流程图:

回到最上面的出现的问题,就能知道为什么了。
server { listen 80; server_name aa.lile.com; root /data/lile; index index.html index.htm index.php; charset utf-8; location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } access_log on; access_log /var/log/lile.com; sendfile off; location ~ \.(gif|jpg|png|css|js|swf|plist|xml)$ { return 111; expires 90d; } location / { return 222; add_header Access-Control-Allow-Origin *; } }
测试一下:按理说应该先匹配上面的那个返回111,但是结果却不一样,返回的是222;这是为什么呢

经过不断的看,终于发现了这个符号是错的,不是英文下的反斜线,而是全角符号的反斜线

改过来,重新reload一下nginx的配置文件,现在就返回正常了:

相关博文:
-------------------------------------------
个性签名:在平凡中坚持前行,总有一天会遇见不一样的自己!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!
微信公众号 微信打赏 支付宝打赏
posted on 2018-01-06 21:06 Captain_Li 阅读(15018) 评论(0) 收藏 举报
浙公网安备 33010602011771号