web - nginx location 搜索算法问题!?
问题描述
先贴出我的server配置:
server { listen 80; server_name xxx; # 1 location = /favicon.ico {root /home/www-data/static;rewrite (.*) /img/designs/admin-favicon.ico; } #2 location = /robots.txt {root /home/www-data/static;rewrite (.*) /admin-robots.txt; } # 3 location / {proxy_pass_header Server;proxy_set_header Host $http_host;proxy_redirect off;proxy_set_header X-Real-IP $remote_addr;proxy_set_header X-Scheme $scheme;proxy_pass http://127.0.0.1:22222; }}
官方给出的location搜索路径优先级和算法描述如下:
To find location matching a given request, nginx first checks locations defined using the prefix strings (prefix locations). Among them, the location with the longest matching prefix is selected and remembered. Then regular expressions are checked, in the order of their appearance in the configuration file. The search of regular expressions terminates on the first match, and the corresponding configuration is used. If no match with a regular expression is found then the configuration of the prefix location remembered earlier is used.
按照我的理解,Nginx搜索的时候算法应该是这样的:
按照请求前缀搜索location最长普通前缀匹配(如果有=修饰符,则直接匹配该location,如果有^~修饰符,则直接使用该location),记住最长匹配location。 继续按照配置顺序搜索正则匹配,如果有,则直接匹配第一个匹配到的location,否则使用普通最长location也就说,按照我的配置,浏览器请求/favicon.ico的时候,应该是直接精准匹配到 1 才对啊,可是每次都匹配到 3。这里请教一下各位,我对Nginx location的理解有没有偏差?然后为什么我的配置会是这样的结果?
问题解答
回答1:按照楼主的方法配置了一下,我这里是可以进到# 1的。我觉得可能是CDN缓存的问题。楼主试试别的文件rewrite或者在没有CDN的环境下试试。
相关文章:
1. python的MySQLdb库中的executemany方法如何改变默认加上的单引号?2. python爬虫 - scrapy使用redis的时候,redis需要进行一些设置吗?3. 前端HTML与PHP+MySQL连接4. angular.js - angular指令中的scope属性中用&获取父作用域函数的问题5. mysql5.7就没有官方性质的详细配置文件吗?求大神告知6. angular.js - angularjs ng-class指令改变ng-click点击的class属性失效7. node.js - 问个问题 Uncaught (in promise)8. 如何解决docker宿主机无法访问容器中的服务?9. python 读取csv文件可以读取但内容错误,但单独用excel打开正常,如何解决?10. python - 如何用pandas处理分钟数据变成小时线?

网公网安备