Nginx and Tomcat - subdomain pointing to subdirectory
NickName:qxlab Ask DateTime:2015-06-11T09:04:55

Nginx and Tomcat - subdomain pointing to subdirectory

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/.

Copyright Notice:Content Author:「qxlab」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/30769891/nginx-and-tomcat-subdomain-pointing-to-subdirectory

More about “Nginx and Tomcat - subdomain pointing to subdirectory” related questions

Nginx and Tomcat - subdomain pointing to subdirectory

I have the following configuration for my application example.com: Nginx as a reverse proxy to redirect traffic from port 80 to: Tomcat 7 where I deploy my JSF app, port 8080 My xhtml files are mo...

Show Detail

nginx proxy subdirectory to subdomain on same machine

I'm using the docker image from linuxserver called swag which contains an nginx reverse proxy and a Let's encrypt certbot. Quite some dockerized apps are not designed to be accessed via subdirectory

Show Detail

routing subdomain to subdirectory on nginx

Please help as explicitly as possible. I have set up a domain on a home server running nginx on Ubuntu 15, and I have the dns pointed to it. I can use the domain to access the site and if I append /

Show Detail

How can Nginx add the Subdomain as parameter when a proxy_pass to Tomcat is executed

What I am trying to achieve The web application should be able to support multiple subdomains without having to make any changes on the nginx or tomcat every time a new subdomain is used. (I have a...

Show Detail

Share a sessionid between tomcat and nginx subdomain

I am making a POST from localhost page (tomcat) to sub.localhost (nginx server which passes the request to tomcat). I need this POST call to have the same session id that localhost page does, because

Show Detail

nginx subdomain with tomcat

I have configuration in tomcat server.xml like this. <Service name="Catalina"> <Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" URI

Show Detail

NGINX config: issue to proxy_pass a folder to a subdomain

I want to display the subdirectory /obvious on the subdomain obvious.example.com I added a CNAM record in Cloudflare to create the subdirectory, pointing to the regular app I added the following t...

Show Detail

redirect subdirectory to subdomain.subdomain.domain

i wanted to change the url for my subdirectory from "subdomain1.main.com/subdirectory" to "subdomain2.subdomain1.domain.com" i tried the htaccess code below but it doesnt work. can someone please ...

Show Detail

Configure subdomain with nginx

I have Linux ubuntu where I installed Open edX and Drupal. Open Edx using nginx and so I prefer to use nginx with Drupal too to avoid installing more packages for apache and load the machin. I would

Show Detail

NGINX Server block when MediaWiki in subdirectory

MediaWiki installation was in domain.com/subdirectory and worked with Apache as the server. Pages were found domain.com/subdirectory/index.php/Main_Page The server was changed to NGINX the other...

Show Detail