2017-02-09 41 views
1

我有我的nginx的conf像:「位置」 和nginx的 「proxy_pass」 的不同水煤漿的X加速 - 重定向

location ^~ /mount_points/mount_point1 { 
    internal; 
    alias /repos/mount_point_one; 
} 

location ^~ /to_proxy { 
    internal; 
    proxy_pass http://myproxy:5000; 
} 

當我請求 'http://localhost/mount_points/mount_point1/myfile.zip' 我得到「/回購/ mount_point_one/MYFILE .zip「如預期。

雖然要求'http://localhost/to_proxy/myfile2.html',我得到「http://myproxy:5000/to_proxy/myfile2.html」。

在第一種情況下,「/ mount_points/mount_point1」部分被刪除,第二種情況下,「/ to_proxy」部分仍然存在,我必須在上游服務器中僞造一個「/ to_proxy」地址找出這個。

我錯過了什麼嗎?如果我只需要重寫url,如何刪除上游服務器的「/ to_proxy」部分問題?

謝謝。

回答

2

proxy_pass指令可以執行別名功能,但前提是提供了可選的URI。

location ^~ /to_proxy/ { 
    internal; 
    proxy_pass http://myproxy:5000/; 
} 

爲了正確地使該別名映射工作,後/也被添加到location參數。

查看this document瞭解詳情。

如果在location參數後/引起的問題,你可以使用一個rewrite ... break代替:

location ^~ /to_proxy { 
    internal; 
    rewrite ^/to_proxy(?:/(.*))?$ /$1 break; 
    proxy_pass http://myproxy:5000; 
} 
+0

謝謝@RichardSmith,後'/'工作! – chenxin