title: Nginx per location / pass rewriten uri to proxy_pass
body: <p>What the quickest and cleanest solution if you want to proxy URL request to two different backends via proxypass based on location.</p>
  
  <pre><code>location /app1/ {
      alias /var/www/ruby/public;
      proxy_set_header  X-Real-IP  $remote_addr;
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header  Host $http_host;
      proxy_redirect    off;
      try_files $uri $uri/ @ruby;
  }
  
  location @ruby {
      proxy_pass http://127.0.0.1:3000;
  }
  
  location /app2/ {
      proxy_set_header  X-Real-IP  $remote_addr;
      proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header  Host $http_host;
      proxy_redirect    off;
      try_files $uri $uri/ @other;
  }
  
  location @other {
      proxy_pass http://127.0.0.1:8080;
  }
  </code></pre>
  
  <p>With this config nginx pass "/app1" or "/app2" to proxy and backend doesn't recognize the url/command .. </p>
  
  <p>as for instance would like to pass to <code>http://127.0.0.1:3000</code> only <code>/messages</code> when accessing <code>http://&lt;nginx&gt;/app1/messages</code> - but in configuration above also pass <code>/app1/</code> as <code>http://127.0.0.1:3000/app1/messages</code>. Same goes for <code>/app2</code></p>