Nginx + Tomcat 8 + SSL reverse proxy

I’m following this example:

https://devtidbits.com/2015/12/08/nginx-as-a-reverse-proxy-to-apache-tomcat/

my goal is to have Tomcat running and Nginx in front of it.

I would like to have structure similar to this:

test.com/ – static site from nginx

test.com/tomcat – tomcat manger (with working links)

test.com/app – tomcat app (deployed at /app on tomcat)

app2.test.com/app

My Nginx conf is:

upstream tomcat {
    server 127.0.0.1:8080 fail_timeout=0;
}
server {
    listen 80;
    listen [::]:80;
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    include snippets/ssl-test.com.conf;
    include snippets/ssl-params.conf;
    root /var/www/test.com/html;
    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;
    server_name test.com www.test.com;
    location / {
        # First attempt to serve request as file, then as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }
    location ~ /.well-known {
                allow all;
        }

    location ~ ^/tomcat(/?)(.*)$ {  # OOPS!
      proxy_pass http://tomcat/$2$is_args$args;  # OOPS!
    }

    location /tomcat/ {
       include proxy_params;
       proxy_pass http://tomcat/;

        proxy_set_header HOST $host;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      }
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
  expires 1M;
 }
}

Thank you in advance!

Answer

Try the following one instead of your two tomcat locations:

location ~ /tomcat(.*)$ {
    include proxy_params;
    proxy_pass http://tomcat$1$is_args$args;

    proxy_set_header HOST $host;
    proxy_set_header X-Forwarded-Proto $scheme;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

Attribution
Source : Link , Question Author : Vasil Koicev , Answer Author : Tero Kilkanen

Leave a Comment