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.
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!
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.
Heya,
Based on your description and configuration, I have some suggestions to improve your Nginx configuration.
Use
alias
instead ofroot
for the/static/
location block. This is becausealias
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:
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.