Nginx + Tomcat virtual hosts

nginxtomcat

I need to configure Nginx as a reverse proxy for Tomcat. The problem is when I try to access the web application through a subdomain (myapp.domain.com), in this case is displayed Tomcat webapps root, not myapp.

Nginx conf

server {
    listen 80;

    root /usr/share/tomcat/webapps/myapp;
    index index.php index.html index.htm;

    access_log off;

    server_name myapp.domain.com;

    location / {
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $host;
      proxy_pass http://127.0.0.1:8181;
    }
}

I tried adding this line in Tomcat's server.xml, inside the Host tag:

<Context path="" docBase="myapp" debug="0" reloadable="true" />

but in this way myapp became the default one and is "always" loaded, even if trying to access webapps root

Best Answer

If I understand your problem correctly, you need to give consideration to how Tomcat is interpreting the 'Host' header, by configuring virtual hosting for it (see https://tomcat.apache.org/tomcat-6.0-doc/virtual-hosting-howto.html).

Either that, or do not have the Context tag in server.xml, and change proxy_pass to:

proxy_pass http://127.0.0.1:8181/myapp