Nginx and Tomcat - subdomain pointing to subdirectory
I have the following configuration for my example.com example:
- Nginx as a reverse proxy to redirect traffic from port 80 to:
- Tomcat 7 where I deploy JSF application, port 8080
- My xhtml files are mostly in the ROOT folder: / usr / local / tomcat7 / webapps / ROOT
- But I have files in a separate "forum" folder: / usr / local / tomcat7 / webapps / ROOT / forum
This is my default nginx config:
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 need 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, which will point to the forum folder, to store the url . That is, I don't want to ever see sufix "/ forum /" in a URL. I want the forum folder to behave as root . So if a user tries to load forum.example.com/anotherPageFromROOT.xhtml, he will fail.
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:
- If I click a link (POST request) to the "topic.xhtml" file also inside the "forum" folder, the url is forum.example.com/forum/topic.xhtml and not forum.example.com/topic.xhtml how i want.
- The user has access to the regular forum forum.example.com/anotherPageFromROOT.xhtml
Can someone please help me on what should I do? Thank!
EDIT: I think the root cause for the first problem is the action
JSF generated for mine h:form
. It is always generated with /forum/
.
source to share
Problem # 1 is the elephant in the room. You cannot (afaik) tell Tomcat to show pages from a non-ROOT folder as if they were in the ROOT folder (i.e. without adding the folder name in the generated urls). There is an attribute path
for customization items Context
, but from personal experience it is not a good idea to change this (errors in portlet deployment, two portlets deployed twice using a double resource, etc.).
If Tomcat has such a parameter (tell the portlet from the folder to generate root URLs) then try using it, otherwise the only option is to install another Tomcat with the correct files in the root folder, so the generated URLs will not have a folder prefix.
Once this problem is resolved, you can move on to problem # 2. If you installed another Tomcat with forum files in the ROOT folder, the problem goes away; otherwise, let us know what you did.
source to share