title: nginx proxy based on location
body: <p>I would like to setup an nginx proxy that is based on a location instead of the listening port or server_name.</p>
  
  <pre><code>upstream cluster
  {
      server cluster1:8080;
      server cluster2:8080;
  }
  server
  {
      listen 80;
      server_name mydomain.com;
      location /hbase
      {
          proxy_pass http://cluster;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
      }
  }
  </code></pre>
  
  <p>I know this is probably something really simple that I am missing.  Basically the routing seems to work, but it actually ends up calling "http://cluster1:8080/hbase"  when it should just route the traffic to the server without the hbase portion.  </p>
  
  <p>A rewrite rule would work, but I can't seem to find a way to rewrite in nginx; I can get it to redirect, but I want the actual port to be invisible to the outside world.</p>
  
  <p>This works perfectly, but I only want to allow traffic on port 80.</p>
  
  <pre><code>upstream cluster
  {
      server cluster1:8080;
      server cluster2:8080;
  }
  server
  {
      listen 555;
      server_name mydomain.com;
      location /
      {
          proxy_pass http://cluster;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_next_upstream error timeout invalid_header http_500 http_502 http_503;
      }
  }
  </code></pre>
  
  <p>Is there possibly a way to use regex to remove hbase from the forwarded request?</p>
  
  <p><strong>ANSWER:</strong></p>
  
  <pre><code>location /hbase
  {
      rewrite  ^/hbase$|^/hbase/$|^/hbase/(.*)$ /$1  break;
      proxy_pass http://cluster;
  }
  </code></pre>