title: Nginx configuration to pass site directly to tomcat webapp with context
body: <h2>tl;dr version</h2>
  
  <p>How do you setup <code>nginx</code> as a reverse proxy for <code>example.com</code> to a locally running <code>tomcat</code> webapp at <code>http://127.0.0.1:8080/blah/</code> without breaking the <code>pageContext</code>?</p>
  
  <hr>
  
  <h3>Tomcat Setup</h3>
  
  <p>There exists a <a href="http://tomcat.apache.org/download-70.cgi">tomcat 7</a> webapp, <code>blah</code>, deployed with a <code>.war</code> file and sitting in <code>/var/lib/tomcat7/webapps/blah/</code>. </p>
  
  <p><code>tomcat</code> is running locally and accessible at <code>http://127.0.0.1:8080</code>. Multiple webapps are running and can be accessed at:</p>
  
  <ul>
  <li><code>http://127.0.0.1:8080/blah/</code></li>
  <li><code>http://127.0.0.1:8080/foo/</code></li>
  <li><code>http://127.0.0.1:8080/bar/</code></li>
  </ul>
  
  <p>Port <code>8080</code> is blocked externally by the firewall.</p>
  
  <h3>Nginx Setup</h3>
  
  <p><code>nginx</code> is running on the server as the gatekeeper. One site is enabled to access all of the local tomcat webapps mentioned above. This works fine for <code>example.com</code>:</p>
  
  <pre><code>server {
  listen  80; 
  server_name example.com;
  root /var/lib/tomcat/webapps/ROOT/;
  
    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_pass http://127.0.0.1:8080/;
    }
  }
  </code></pre>
  
  <h3>Question: how to configure an additional site to access <code>blah</code> directly?</h3>
  
  <p>Under <code>/etc/nginx/sites-enabled/</code> an additional site file is setup to route <code>http://blah.com</code> to <code>http://127.0.0.1:8080/blah/</code> but there are issues.</p>
  
  <pre><code>server {
    listen  80; 
    server_name blah.com *.blah.com;
    root /var/lib/tomcat/webapps/blah/;
  
    location / { 
      proxy_set_header X-Forwarded-Host   $host;
      proxy_set_header X-Real-IP          $remote_addr;  
      proxy_set_header X-Forwarded-Server $host;
      proxy_set_header X-Forwarded-For    $proxy_add_x_forwarded_for;
      proxy_pass                          http://127.0.0.1:8080/blah/;
    }
  }
  </code></pre>
  
  <p>This setup adds an extra <code>blah</code> to the context path, creating a <code>404</code> page because path <code>/blah/blah/</code> doesn't exist, which makes sense. Is there a simple way within <code>nginx</code> to 
  pass <code>blah.com</code> to the webapp root? </p>
  
  <p>Within the webapp, I'm using <code>${pageContext.request.contextPath}/path</code> for relative paths to webapp resource. I thought this was the correct way to handle internal tomcat paths but could this be part of the problem? I believe this is why I'm getting the extra <code>blah</code> in the route, creating the <code>404</code> page.</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
    &lt;meta charset="UTF-8"&gt;
    &lt;meta http-equiv="refresh" content="0; url=${pageContext.request.contextPath}/form"&gt;
    &lt;script type="text/javascript"&gt;
      window.location.href = "${pageContext.request.contextPath}/form"
    &lt;/script&gt;
    &lt;title&gt;Load BLAH&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;p&gt;If you are not redirected automatically, follow this &lt;a href="${pageContext.request.contextPath}/form"&gt;link&lt;/a&gt;.&lt;/p&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>This page is hit alright but the redirect goes to <code>/blah/blah/form</code> instead of <code>/blah/form</code> where the servlet actually exists.</p>
  
  <hr>
  
  <p>I've also tried other approaches including pointing <code>blah.com</code> to the tomcat root itself. This works in the sense that you can get to <code>blah</code> via <code>blah.com/blah/</code> but that's not really what we want.</p>
  
  <p>Additionally, it is completely acceptable (and desired) to still be able to access <code>blah</code> via <code>example.com/blah/</code>.</p>
  
  <p>Obviously, this is for an <code>nginx</code> novice but help me (and future novices) clear this up because the clear solution is eluding me and the <code>nginx</code> docs use the help too.</p>