Nginx and Tomcat - subdomain pointing to subdirectory

1.8k views Asked by At

I have the following configuration for my application example.com:

  1. Nginx as a reverse proxy to redirect traffic from port 80 to:
  2. Tomcat 7 where I deploy my JSF app, port 8080
  3. My xhtml files are mostly in the ROOT folder: /usr/local/tomcat7/webapps/ROOT
  4. But I have some files in a separate folder "forum": /usr/local/tomcat7/webapps/ROOT/forum

This is my default nginx configuration:

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/;
  }
}

So, to access the forum page I have to go to example.com/forum/forum.xhtml (forum.xhtml is the index)

Now, I'm trying to set a subdomain, forum.example.com, in nginx, that will point to the forum folder, keeping the URL. That is, I don't want ever to see the sufix "/forum/" in the URL. I want that the forum folder behaves as the root. So if the user tries forum.example.com/anotherPageFromROOT.xhtml he will not be able to.

This is what I have so far:

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;    
 }
}

Problems:

  1. If I click in a link (POST request) to the file "topic.xhtml" also inside the "forum" folder the URL will be forum.example.com/forum/topic.xhtml instead of forum.example.com/topic.xhtml as I want.
  2. The user is able to access forum.example.com/anotherPageFromROOT.xhtml normally

Could some one please help me on what I should do? Thanks!

EDIT: I think the root cause for the first problem is the action generated by JSF for my h:form. It is always generated with /forum/.

1

There are 1 answers

0
Alex Mazzariol On

Problem #1 is the elephant in the room here. You cannot (afaik) tell Tomcat to serve pages from a non-ROOT folder as if they were in the ROOT folder (i.e., without prepending the folder name in generated URLs). There is a path attribute for Context setting elements, but from personal experience it's not a great idea to change that (errors in portlet deployment, portlets deployed twice with double resource usage, and so on).

If Tomcat has such a setting (tell a portlet from a folder to generate root urls) then try to use it, otherwise your only option is to set up another Tomcat with the correct files in the root folder, so the generated URLs will not have the folder prefix.

Once this problem is solved you can move on to problem #2. If you set up a different Tomcat with the forum files in the ROOT folder, the problem will have disappeared; otherwise let us know what you did.