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搜索路徑優(yōu)先級和算法描述如下:
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搜索的時(shí)候算法應(yīng)該是這樣的:
按照請求前綴搜索location最長普通前綴匹配(如果有=修飾符,則直接匹配該location,如果有^~修飾符,則直接使用該location),記住最長匹配location。 繼續(xù)按照配置順序搜索正則匹配,如果有,則直接匹配第一個(gè)匹配到的location,否則使用普通最長location也就說,按照我的配置,瀏覽器請求/favicon.ico的時(shí)候,應(yīng)該是直接精準(zhǔn)匹配到 1 才對啊,可是每次都匹配到 3。這里請教一下各位,我對Nginx location的理解有沒有偏差?然后為什么我的配置會(huì)是這樣的結(jié)果?
問題解答
回答1:按照樓主的方法配置了一下,我這里是可以進(jìn)到# 1的。我覺得可能是CDN緩存的問題。樓主試試別的文件rewrite或者在沒有CDN的環(huán)境下試試。
相關(guān)文章:
1. 在應(yīng)用配置文件 app.php 中找不到’route_check_cache’配置項(xiàng)2. html按鍵開關(guān)如何提交我想需要的值到數(shù)據(jù)庫3. HTML 5輸入框只能輸入漢字、字母、數(shù)字、標(biāo)點(diǎn)符號?正則如何寫?4. gvim - 誰有vim里CSS的Indent文件, 能縮進(jìn)@media里面的5. 跟著課件一模一樣的操作使用tp6,出現(xiàn)了錯(cuò)誤6. PHP類屬性聲明?7. javascript - 求助canvas繪制馬賽克的問題,老是取色不準(zhǔn)8. java - 安卓接入微信登錄,onCreate不會(huì)執(zhí)行9. javascript - 請教如何獲取百度貼吧新增的兩個(gè)加密參數(shù)10. html - 微信瀏覽器h5<video>標(biāo)簽問題
