2014-10-06 80 views
0

假設我要像Nginx的重定向與重寫

http://server1:8081/test/admin/option?options 
http://server1:8081/test/admin/option/suboption?options 
http://server1:8081/test/admin/option/suboption/subsuboption?options 

重定向URI來

http://server2:8080/tomcat/admin/option?options 
http://server2:8080/tomcat/admin/option/suboption?options 
http://server2:8080/tomcat/admin/option/suboption/subsuboption?options 

我必須使用什麼nginx的規則?我試過以下,但它不工作

location =/test/admin { 
    proxy_pass http://server2:8080/tomcat/admin; 
    proxy_redirect off; 
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
} 

回答

1

嘗試沿着這些路線的東西..

location /test/admin { 
    rewrite ^/test/admin(.*)$ /tomcat/admin$1; 
} 

location /tomcat/admin { 
    internal; 
    proxy_pass http://server2:8080; 
    […] 
} 

也就是說,重寫請求「雄貓/ admin」的,你可以選擇做僅對internal requests開放。

在那個位置塊,你可以再請求proxy pass

+0

非常感謝!一個通知是我想使用更一般的重寫規則,比如''重寫^/test/admin(。*)$/tomcat/admin $ 1 last;「'。有希望的是,這也適用 – danbst 2014-10-07 10:18:18

+0

這應該工作。由於您只需要一條規則,效率更高。你不需要爲單個規則需要「最後」的標誌。 – Dayo 2014-10-08 13:34:17