Nginx反向代理到URI

Nginx反向代理到URI,nginx,reverse-proxy,proxypass,Nginx,Reverse Proxy,Proxypass,我正在使用nginx创建一个反向代理,用于在现有遗留系统和基于Rails的新应用程序之间进行代理。默认情况下,我们会将所有流量发送到Rails站点,除非它们与某些URL匹配,或者请求主体不包含某些参数。但是,在某些情况下,URL与传统条件匹配,但是如果存在某些参数,我们需要重定向到新系统。例如,我们有一个URL/index.cfm?fuseaction=search,它通常会被发送到旧系统,但由于它包含fuseaction=search,我需要它重定向到/search/events的新系统。我已

我正在使用nginx创建一个反向代理,用于在现有遗留系统和基于Rails的新应用程序之间进行代理。默认情况下,我们会将所有流量发送到Rails站点,除非它们与某些URL匹配,或者请求主体不包含某些参数。但是,在某些情况下,URL与传统条件匹配,但是如果存在某些参数,我们需要重定向到新系统。例如,我们有一个URL
/index.cfm?fuseaction=search
,它通常会被发送到旧系统,但由于它包含
fuseaction=search
,我需要它重定向到
/search/events
的新系统。我已经编写了以下配置

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

我理解错误的意思,但我不知道如何修复它。也许我不需要代理通行证?也许我需要使用代理重定向?非常感谢您对我如何解决此问题的任何帮助。

代理\u pass应该可以,但我不知道为什么它会抱怨这行“//rails”确实是一个可访问的URI吗

我认为这个问题与使用$request_body有关,如果您知道$args是需要匹配的GET参数,我会切换到使用$args

比如:

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

在试图理解这个问题的过程中,我意识到我真正需要的是重定向,而不是尝试代理。除了我最终更改的许多内容之外,我还检查了
$args
变量,而不是
$request\u 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(永久)重定向。

谢谢您的回答。我已经弄明白了,但我忘了回答我自己的问题。答案是用重写来重定向。哦,是的,你的部分答案是正确的。我也不得不切换到
args
<代码>请求体,据我所知,总是空的或空的。我的荣幸,是的,重写,我错过了这一点$请求体可能不会包含太多内容,除非它是一个帖子。很高兴听到你把它解决了!Rgds…胡恩托/马特