Configure Nginx proxy to Tomcat for multi-site properly

Share this post:

Replacing Apache with Nginx as a frontend proxy server to Tomcat is something I wanted to do for some time. However, I always had some issues with the multi-site (wildcard domain) setup.

Setting up a wildcard domain entry with proxy forward to Tomcat is something that simply worked in Apache. Especially, the host was simply forwarded to Tomcat. In Nginx this is different as one has to set “proxy_headers”. Furthermore, setting the “root” parameter was crucial in my case. In my research I’ve found that some people suggest to add a “nameProxy” parameter to the connector part in the Tomcat server.xml. However that approach does not work for me as I have multiple sites also within the same Tomcat container.

In any case, with the below configuration there is no change needed in the Tomcat configuration and it works with multiple sites in Nginx and Tomcat. Here is one of my site configuration in Nginx.

[code]server {
listen 80;
server_name *.domain.com;
access_log off;

root /var/www/myapp;

location / {
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;

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_http_version 1.1;

proxy_pass http://localhost:8080;
proxy_redirect off;
proxy_buffering off;
}
}
[/code]

There you go. Multi-site setup in Nginx with a proxy to Tomcat which in return can also host multiple sites. With this it is easy to add another server configuration pointing to the same Tomcat serving another site (like a static page or even another dynamic app).

Hope this helps anyone out there.

Over 10 years in business.

Self-funded. No investors. No bullshit.

More than 3,000 customers worldwide.

Helpmonks - no bullshit customer engagement service

Growth starts with action

Empower your team and delight your customers.

Helpmonks - email management for small businesses