我正在用nginx創建一個反向代理,用於在現有的遺留系統和基於Rails的新應用程序之間進行代理。我們默認將所有流量發送到Rails站點,除非它們匹配某些URL或者請求主體不包含某些參數。但是,有些情況下,網址與舊版條件匹配,但如果存在某些參數,我們需要重定向到新系統。因此,例如,我們有一個URL爲/index.cfm?fuseaction=search
,通常會發送到舊系統,但由於它包含fuseaction=search
,我需要重定向到新系統/search/events
。我寫了下面的配置。Nginx反向代理到URI
upstream legacy {
server 10.0.0.1:80;
}
upstream rails {
server 10.0.0.2:80;
}
server {
listen 0.0.0.0:80;
server_name mydomain.com
root /usr/share/nginx/html;
index index.html index.htm;
location/{
proxy_pass http://rails;
if ($request_body ~* fuseaction(?!=public\.search(_all(_by_type)?)?))
{
proxy_pass http://legacy;
}
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~* /(index.cfm|images|uploads|admin|htmlArea|innovastudio|js|scripts|w3c)*$ {
proxy_pass http://legacy;
if ($request_body ~* fuseaction=public\.search(_all(_by_type)?)?)
{
proxy_pass http://rails/search/events;
}
proxy_next_upstream error timeout invalid_header http_500 http_502 http_503 http_504;
proxy_redirect off;
proxy_buffering off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
然而,當我嘗試啓動nginx的,我得到以下錯誤:
"proxy_pass" may not have URI part in location given by regular expression, or inside named location, or inside the "if" statement, or inside the "limit_except" block in /etc/nginx/sites-enabled/default:56
我明白什麼是錯誤的說法,但我不知道如何解決它。也許proxy_pass
不是我所需要的?也許我需要使用proxy_redirect
?任何幫助我如何解決這個問題表示讚賞。
感謝您的回答。我已經知道了,但我忘了在這裏回答我自己的問題。答案是使用重寫進行重定向。 – 2013-06-30 21:10:18
哦,是的,你的答案的一部分是正確的。我也必須切換到'args'。從我所知道的request_body'總是空的或空的。 – 2013-06-30 21:15:42
我的榮幸,是重寫,好點我錯過了。 $ request_body可能不會包含太多,除非它是一個帖子。很高興聽到你解決它! RGDS .... Hoonto /馬特 – hoonto 2013-06-30 21:27:42