title: Nginx Reverse Proxy to URI
body: <p>I am creating a reverse proxy with nginx that is being used to proxy between an existing legacy system and a new application based on Rails.  We are by default sending all traffic to the Rails site, unless they match certain URLs or if the request body does not contain certain parameters.  However, there are cases where the URL matches the legacy conditions, but we need to redirect to the new system if certain parameters are present.  So, for example, we have a URL of <code>/index.cfm?fuseaction=search</code>, which would normally be sent to the legacy system, but since it contains <code>fuseaction=search</code>, I need that to redirect to the new system at <code>/search/events</code>.  I have written the following configuration.</p>
  
  <pre><code>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; 
         } 
  } 
  </code></pre>
  
  <p>However, when I try to start nginx, I get the following error:</p>
  
  <pre><code>"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
  </code></pre>
  
  <p>I understand what the error is saying, but I am not sure how to fix it.  Maybe <code>proxy_pass</code> is not what I need?  Maybe I need to use <code>proxy_redirect</code>?  Any help on how I can solve this problem is appreciated.</p>