title: Edit a header value in nginx
body: <p><strong>Background</strong><br/>
  So I've got a server running a tomcat application hidden behind an Apache proxy. The proxy provides a more user friendly url as well as SSL encryption with automatic redirects so that the app is only accessible on https.</p>
  
  <p>I'm busy migrating this to an nginx proxy.</p>
  
  <p>One of the issues I've had is that upon login, my app sends back a "LocationAfterLogon" header in the http response in the form of</p>
  
  <pre><code>http://192.168.x.x:8080/myapp/index.jsp. 
  </code></pre>
  
  <p>That IP address returned is from the proxied server not visible on the internet. So then the browser gets a connection error trying to navigate to it.</p>
  
  <p>As a workaround, I've used nginx directives:</p>
  
  <ul>
  <li>proxy_hide_header: to hide the LocationAfterLogin header coming back from the proxied server</li>
  <li>add_header: to add a new LocationAfterLogin url.</li>
  </ul>
  
  <p>So my config looks as follows</p>
  
  <pre><code>            #header for location after logon of demo app
              add_header LocationAfterLogon http://example.com/demo/index.jsp;
              #hide the real LocationAfterLogon
              proxy_hide_header LocationAfterLogon;
  </code></pre>
  
  <p><strong>The Problem</strong><br/>
  I need to be able to do a regex replace or similar on LocationAfterLogon because it won't always be to index.jsp, depending on which url was intercepted by the login page.</p>
  
  <p>I am aware that I can also rewrite the tomcat app to send back a relative URL instead, but I'd like to do it all in nginx config. </p>
  
  <p>I've also read about nginx more_set_headers. Haven't tried it yet. Does it allow me to edit the headers?</p>
  
  <p>Apache has the <strong>Header edit</strong> directive which I was using previously, so I'm looking for something like that.</p>
  
  <p><strong>TL;DR</strong><br/>
  Is is possible to edit a header location using regex replace or similar in Nginx?</p>