Nginx and Tomcat Reverse Proxy not Working

nginxreverse-proxytomcat

I had trouble configuring Nginx and Tomcat Server on Cent OS.

Here's the nginx.conf:

server {
    listen       80;
    server_name  subdomain.test.com;
    root         /usr/local/tomcat/webapps/webapp_folder;
    index        index.html index.htm index.jsp;

    proxy_set_header   Host             $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

    location ~ \.jsp {
       proxy_pass      http://127.0.0.1:8080;
    }
}

When I visit subdomain.test.com, I'll get a download dialog instead of the content of index.jsp.

Besides, I can visit the website normally by visiting ip_address:8080/webapp_folder.

I also tried to change the URL http://127.0.0.1:8080 to http://127.0.0.1:8080/webapp_folder, but when I started Nginx, I got following error message:

Restarting nginx daemon: nginxnginx: [emerg] "proxy_pass" cannot have URI part in location given by regular expression, or inside named location, or inside "if" statement, or inside "limit_except" block in /etc/nginx/nginx.conf:145

And I also tried this:

server {
    listen       80;
    server_name  subdomain.test.com;
    root         /usr/local/tomcat/webapps/webapp_folder;
    index        index.html index.htm index.jsp;

    proxy_set_header   Host             $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;

    location / {
       proxy_pass      http://127.0.0.1:8080/webapp_folder;
    }
}

But still not working.

Can you help me with the problem? Thx a lot!

Best Answer

The download dialog might appear because jsp is not defined in /etc/nginx/mime.types - although I would assume that a reverse proxy would not change the mime type provided by Tomcat.

I assume the WebApp is working correctly when you visit the application at port 8080? If so, see what happens when you pass all requests to Tomcat, like so:

server {
    listen       80;
    server_name  subdomain.test.com;
    root         /usr/local/tomcat/webapps/webapp_folder;
    index        index.html index.htm index.jsp;

    proxy_set_header   Host             $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_pass         http://localhost:8080;

}
Related Topic