I have setup an Ubuntu 15.10 x64 server on DigitalOcean to test several Java applications. I installed Nginx 1.9.3 and Tomcat8 and am using Nginx as a reverse proxy to forward all requests to Tomcat on port 8080.
Here is where things get dicey. I am running two applications on my tomcat installation, /webapp1 & /webapp2. I want to point subdomain.domain.com to /webapp1 and subdomain2.domain.com to /webapp2. In Apache, this is a fairly simple affair using mod_proxy, but with Nginx it's a bit more mysterious.
So far, here is what I have tried in my sites-enables/domain file.
1st Round
server{
server_name subdomain1.domain.com;
# ******************SSL configuration ************************
listen 443 ssl default_server;
ssl_certificate /etc/nginx/conf/ssl/domain.crt;
ssl_certificate_key /etc/nginx/conf/ssl/domain.key;
#*********************************************************************
#**********Proxy**********************
location / {
proxy_redirect off;
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://subdomain1.domain.com:8080/webapp1/;
}
The requests worked when I accessed https://subdomain1.domain.com but any links in my application that referenced the context path such as /webapp1/ resulted in a url that contained two context paths such as https://subdomain1.domain.com/webapp1/webapp1/. This caused all kinds of broken references, etc.
2nd Round
I found a thread that discussed a similar issue and the fix was to use a rewrite to strip the extra context path from the url.
server{
server_name subdomain1.domain.com;
# ******************SSL configuration ************************
listen 443 ssl default_server;
ssl_certificate /etc/nginx/conf/ssl/domain.crt;
ssl_certificate_key /etc/nginx/conf/ssl/domain.key;
#*********************************************************************
#**********Proxy**********************
location / {
proxy_redirect off;
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://subdomain1.domain.com:8080/webapp1/;
rewrite ^/webapp1/(.*)$ /$1 last;
}
This resolved my issue of a double context path in the url and will work for now, but I am wondering if there is a more elegant solution such that a rewrite is not needed. I doubt the rewrite is costly from a server resources stand point, but there's something about it I don't like.
Thank you in advance for your time.
Best Answer
Update
I have since read about locations configuration. Here is another configuration that resolves the double context path in the url and avoids having to use a rewrite. Basically, nginx uses both the locations configurations so when the uri has a double context path like https://subomdain1.domain.com/webapp1/webapp1/, it picks it up in my second location block, and forwards it to my tomcat server but without the first context path. I am not sure if this or the rewrite solution is more elegant.