2012-08-26 35 views
0

我有這兩個規則:爲什麼NGINX中的下列規則衝突?

location ~* ^.+\.(jpg|jpeg|gif|css|js|png)$ { 
     proxy_pass http://127.0.0.1:44843$request_uri; 
    } 

    location /pc { 
     proxy_pass http://127.0.0.1:12345$request_uri; 
    } 

的URI:

/pc/asser/somefile.js

跌倒在格蘭第二條規則,爲什麼呢?

它是「位置/ pc」規則的低優先級的問題嗎?

回答

1

Nginx的documentation定義它:

指令被以下面的方式進行處理:

  • 精確字符串匹配被首先處理。如果發現匹配,nginx將停止搜索並完成請求。
  • 接下來處理剩餘的文字字符串指令。如果使用「^〜」參數,那麼ngnix在這裏停止並完成請求。否則,nginx會繼續處理位置指令。
  • 處理正則表達式指定的所有位置指令(帶有〜和〜*參數)。如果一個正則表達式匹配請求,nginx會在這裏停止並完成請求。
  • 如果沒有正則表達式或沒有正則表達式匹配,則使用最具體的文字字符串匹配。

所以,你應該使用正則表達式/ PC太:

 
location ~ /pc/.* { 
    proxy_pass http://127.0.0.1:12345$request_uri; 
}
0

首先nginx檢查使用前綴字符串(前綴位置)定義的位置。 只有在它之後,它才按照它們在配置文件中出現的順序來檢查使用正則表達式定義的位置。

0

http://nginx.org/en/docs/http/ngx_http_core_module.html#location指定了nging位置指令是在看了順序:

To find a location matching a given request, nginx first checks locations defined 
using the prefix strings (prefix locations). Among them, the most specific one is 
searched. 

基本上這裏的問題是,前綴位置始終優先於正則表達式

因此,在您的配置中,如果兩個集團ks匹配,這是/ pc前綴優先級