2013-05-10 256 views
1

我正在用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?任何幫助我如何解決這個問題表示讚賞。

回答

1

在試圖理解這個問題的過程中,我意識到我真正需要的是一個重定向,而不是試圖代理。除了我最終改變的許多事情外,我還檢查了$args變量而不是$request_body。下面是重寫塊看時,我就完成

if ($args ~ fuseaction\=public\.race_search(_all(_by_type)?)?) 
{ 
    rewrite ^(.*)$ /search/events? permanent; 
} 

if ($args ~ fuseaction\=public\.tools) 
{ 
    rewrite ^(.*)$ /directors? permanent; 
} 

if ($args ~ fuseaction\=public\.contact) 
{ 
    rewrite ^(.*)$ /contact? permanent; 
} 

if ($args ~ fuseaction\=public\.spotlight) 
{ 
    rewrite ^(.*)$ /search/events? permanent; 
} 

if ($args ~ fuseaction\=public\.results) 
{ 
    rewrite ^(.*)$ /search/results? permanent; 
} 

的第一個參數的整個路徑,第二告訴它如何重寫匹配,與尾隨?從重寫刪除任何查詢參數等,第三個做301(永久)重定向。

1

proxy_pass應該沒問題,但我不知道爲什麼它在抱怨那條線,是「// rails」肯定是一個可訪問的URI?

我會認爲這個問題與使用$ request_body有關,如果您知道它們是您需要匹配的GET參數,那麼我會切換到使用$ args。

喜歡的東西:

 if ($args ~ "fuseaction(?!=public\.search(_all(_by_type)?)?)") 
+0

感謝您的回答。我已經知道了,但我忘了在這裏回答我自己的問題。答案是使用重寫進行重定向。 – 2013-06-30 21:10:18

+0

哦,是的,你的答案的一部分是正確的。我也必須切換到'args'。從我所知道的request_body'總是空的或空的。 – 2013-06-30 21:15:42

+1

我的榮幸,是重寫,好點我錯過了。 $ request_body可能不會包含太多,除非它是一個帖子。很高興聽到你解決它! RGDS .... Hoonto /馬特 – hoonto 2013-06-30 21:27:42