Question

Nginx reverse proxy with Tomcat - WebSocet connection not being served correctly

Hello,

I’m currently working on a somewhat convoluted setup for a Tomcat-based web application, and I’m having trouble getting Nginx to work as a reverse proxy. My application has multiple components with specific routing requirements that I’m struggling to configure correctly so I decided to ask the community here.

To summarise my setup

I’ve tried configuring Nginx with the following settings, but I’m still having issues with the routing:

http {
    upstream tomcat_server {
        server domain1.com:8080;
    }

    server {
        listen 80;
        server_name domain1.com;

        location /static/ {
            root /path/to/static/files;
        }

        location / {
            proxy_pass http://tomcat_server;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }

        location /api/ {
            proxy_pass http://tomcat_server;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
    }

    server {
        listen 80;
        server_name domain2.com;

        location /ws/ {
            proxy_pass http://tomcat_server;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
        }
    }
}

I’ve removed my actual domains from the configuration file.

I’m still experiencing issues with the WebSocket connections and some static resources are not being served correctly. I’m not sure if I’m missing any important Nginx directives or if the configuration itself is incorrect.

If you need more information about my current setup, I’ll be happy to provide it.


Submit an answer


This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

KFSys
Site Moderator
Site Moderator badge
May 4, 2023
Accepted Answer

Heya,

Based on your description and configuration, I have some suggestions to improve your Nginx configuration.

  • Use alias instead of root for the /static/ location block. This is because alias is used when the URL path and the file system path don’t match exactly.

  • Add a trailing slash to the proxy_pass directive for the /api/ and /ws/ location blocks. This helps in avoiding any potential routing issues.

  • Add the X-Forwarded-For header to pass along the client’s IP address to the Tomcat application. This can be helpful for logging and debugging purposes.

  • Add proxy_http_version 1.1; to the /ws/ location block to ensure that the WebSocket connection uses HTTP/1.1.

Now, I haven’t exactly tested it just because it’s hard to recreate your setup exactly but I think this should do the trick:

http {
    upstream tomcat_server {
        server domain1.com:8080;
    }

    server {
        listen 80;
        server_name domain1.com;

        location /static/ {
            alias /path/to/static/files;
        }

        location / {
            proxy_pass http://tomcat_server;
            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 /api/ {
            proxy_pass http://tomcat_server/api/;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        }
    }

    server {
        listen 80;
        server_name domain2.com;

        location /ws/ {
            proxy_pass http://tomcat_server/ws/;
            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_set_header Upgrade $http_upgrade;
            proxy_set_header Connection "upgrade";
            proxy_http_version 1.1;
        }
    }
}

If you still encounter any issues, please provide more details about the specific errors or problems you’re facing, and we can further help you troubleshoot the issue.

Try DigitalOcean for free

Click below to sign up and get $200 of credit to try our products over 60 days!

Sign up

Get our biweekly newsletter

Sign up for Infrastructure as a Newsletter.

Hollie's Hub for Good

Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.

Become a contributor

Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.

Welcome to the developer cloud

DigitalOcean makes it simple to launch in the cloud and scale up as you grow — whether you're running one virtual machine or ten thousand.

Learn more
DigitalOcean Cloud Control Panel