2016-06-22 90 views
2

我想使用類似:我可以在try_files nginx指令中使用兩個命名的位置嗎?

location @a1 { 
    ... 
} 

location @a2 { 
    ... 
} 

location /path { 
    try_files @a1 $uri @a2 
} 

它可能的,什麼我應該做@ A1的位置繼續$ URI/@嘗試A2?

如果是這樣,nginx會如何處理?

+0

的可能的複製[NGINX嘗試\ _files使用多個命名的位置(http://stackoverflow.com/questions/21286850/nginx的試-文件與 - 多命名的位置) – claytond

回答

2

你不能,因爲try_files只會把它最後參數作爲命名的位置 - 在你的榜樣,nginx的將尋找一個名爲@a1文件,而忽略了location @a1塊。

try_files docs對此不是非常明確,只提到「最後一個參數也可以指向命名的位置」。

This answer顯示使用error_page的解決方法,這對您可能是一種有用的方法(取決於您想要通過步入@a1來執行什麼操作)。

More on try_files here

3

我發現this solution

location /static/ { 
    try_files $uri @static_svr1; 
} 
location @static_svr1{ 
    proxy_pass http://192.168.1.11$uri; 
    proxy_intercept_errors on; 
    recursive_error_pages on; 
    error_page 404 = @static_svr2; 
} 

location @static_svr2{ 
    proxy_pass http://192.168.1.12$uri; 
    proxy_intercept_errors on; 
    recursive_error_pages on; 
    error_page 404 = @static_svr3; 
} 

location @static_svr3{ 
    proxy_pass http://192.168.1.13$uri; 
} 

這個例子的工作原理是:

try_files $uri @static_svr1 @static_svr2 @static_svr3 
相關問題