title: Modifying a Location header with nginx proxy_pass
body: <p>I have an nginx <code>proxy_pass</code> setup to pass every request on <code>/api</code> through to a backend Tomcat REST service. This service in some cases returns a <code>Location</code> header which varies according to the type of request, e.g., <code>Location: http://foo.bar/baz/api/search/1234567</code> -- the <code>baz</code> part is due to it being hosted on Tomcat.</p>
  
  <p>My current configuration rewrites the <code>foo.bar</code> host name correctly, but leaves the <code>baz</code> part intact. I'd like to strip this, but the proxy_pass options seem to be limited to clearing or setting a new value for the header.</p>
  
  <p>Is there a way to modify headers dynamically before being passed on to the client, using a regex substitute, for instance? This is my nginx configuration:</p>
  
  <pre><code>location /api {
      proxy_set_header Host $host;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  
      proxy_max_temp_file_size 0;
      client_max_body_size    10m;
      client_body_buffer_size 128k;
      proxy_connect_timeout   90;
      proxy_send_timeout      90;
      proxy_read_timeout      90;
      proxy_buffers           32 4k;
      proxy_redirect off;
  
      proxy_pass http://foo.bar:8080/baz/api;
  }
  </code></pre>