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 - 我在使用pip install -r requirements.txt下載時,為什么部分能下載,部分不能下載2. mysql - 分庫分表、分區、讀寫分離 這些都是用在什么場景下 ,會帶來哪些效率或者其他方面的好處3. node.js - nodejs開發中常用的連接mysql的庫4. 網頁爬蟲 - python 爬取網站 并解析非json內容5. mysql - jdbc的問題6. Python爬蟲如何爬取span和span中間的內容并分別存入字典里?7. 視頻文件不能播放,怎么辦?8. mysql - 如何減少使用或者不用LEFT JOIN查詢?9. python - 編碼問題求助10. linux - python 抓取公眾號文章遇到驗證問題
