title: Nginx and Tomcat - subdomain pointing to subdirectory
body: <p>I have the following configuration for my application <em>example.com</em>:</p>
  
  <ol>
  <li>Nginx as a reverse proxy to redirect traffic from port 80 to:</li>
  <li>Tomcat 7 where I deploy my JSF app, port 8080</li>
  <li>My xhtml files are mostly in the ROOT folder: /usr/local/tomcat7/webapps/ROOT</li>
  <li>But I have some files in a separate folder "forum": /usr/local/tomcat7/webapps/ROOT/forum</li>
  </ol>
  
  <p>This is my default nginx configuration:</p>
  
  <pre><code>server {
      server_name  www.example.com;
      return 301 $scheme://example.com$request_uri;
  }
  server {
    listen          80;
    server_name     example.com.br;
    root            /usr/local/tomcat7/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>
  
  <p>So, to access the forum page I have to go to <em>example.com/forum/forum.xhtml</em> (forum.xhtml is the index)</p>
  
  <p>Now, I'm trying to set a <strong>subdomain</strong>, <em>forum.example.com</em>, in nginx, that will point to the forum folder, <strong>keeping the URL</strong>. That is, I don't want ever to see the sufix "/forum/" in the URL. I want that the forum folder behaves as the <strong>root</strong>. So if the user tries <em>forum.example.com/anotherPageFromROOT.xhtml</em> he will not be able to. </p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>server {
   listen          80;
   server_name     forum.example.com;
  
   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/;
         rewrite ^/$ /forum/forum.xhtml break;    
   }
  }
  </code></pre>
  
  <p>Problems:</p>
  
  <ol>
  <li>If I click in a link (POST request) to the file "topic.xhtml" also inside the "forum" folder the URL will be <em>forum.example.com/forum/topic.xhtml</em> instead of <em>forum.example.com/topic.xhtml</em> as I want. </li>
  <li>The user is able to access <em>forum.example.com/anotherPageFromROOT.xhtml</em> normally</li>
  </ol>
  
  <p>Could some one please help me on what I should do? Thanks!</p>
  
  <p>EDIT:
  I think the root cause for the first problem is the <code>action</code> generated by JSF for my <code>h:form</code>. It is always generated with <code>/forum/</code>. </p>