title: nginx reverse proxy "ip address shows"
body: <p>I have nginx serving the <code>example.com</code> website on my server. The same server is running a tomcat installation serving the <code>http://server_ip:8080/app</code> webapp (which is a spring MVC webapp, secured with Spring Security). </p>
  
  <p>I want to be able to access the tomcat webapp using the <code>http://app.example.com</code> address. What I did is configure nginx as a reverse proxy as follows (in the <code>app.example.com</code> configuration file under <code>sites-enabled</code>):</p>
  
  <pre><code>server {
    listen          80;
    server_name     app.example.com;
    root            /var/lib/tomcat7/webapps/app;
  
  
    location / {
          proxy_set_header X-Forwarded-Host $host;
          proxy_set_header X-Forwarded-Server $host;
          proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
          proxy_set_header X-Real-IP $remote_addr;
          proxy_pass http://server_ip:8080/;
    }
  }
  </code></pre>
  
  <p>where <code>server_ip</code> is the IPv4 address of my server.</p>
  
  <p>Also, in the <code>/etc/tomcat7/server.xml</code> file I added:</p>
  
  <pre><code>&lt;Host name="localhost"  appBase="webapps" unpackWARs="true" autoDeploy="true"&gt;
      &lt;Context path="" docBase="app"&gt;
           &lt;WatchedResource&gt;WEB-INF/web.xml&lt;/WatchedResource&gt;
       &lt;/Context&gt;
      &lt;Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
             prefix="localhost_access_log." suffix=".txt"
             pattern="%h %l %u %t &amp;quot;%r&amp;quot; %s %b" /&gt;
  
  &lt;/Host&gt;
  </code></pre>
  
  <p>so that tomcat is by default sending me to the <code>app</code> webapp when I access <code>http://server_ip:8080</code> (the <code>app</code> webapp is the only thing it is serving anyway).</p>
  
  <p>Now, if I access <code>http://app.example.com</code> I get the login page for the <code>app</code> webapp alright; however, after logging in, the URL field of the browser is showing <code>http://server_ip:8080</code> instead of <code>http://app.example.com</code>, as I would expect. </p>
  
  <p>Anybody has a hint of what is causing this? All guides I found on the net seem to suggest a configuration like the one I have. I would love if someone could confirm if the configuration is ok, because that may point to a cause other than server config (may be related to the way spring security redirects after login).</p>
  
  <p>Thanks.</p>