When setting up Nginx as a reverse proxy for Jetty, you might encounter a situation where Nginx appears to be running (confirmed via netstat
), but fails to serve your application. The symptoms typically include:
- Seeing the default Nginx welcome page instead of your application
- Getting connection errors when trying to access your domain
- Configuration changes not taking effect despite reloading Nginx
The initial configuration attempt had several potential issues:
server {
listen 80;
server_name nomilkfor.me;
rewrite ^(.+?)/?$ http://nomilkfor.me$1 permanent;
}
server {
listen 80;
server_name www.nomilkfor.me;
root /usr/share/nginx/html;
location / {
try_files $uri @my-webapp;
}
location @my-webapp {
proxy_pass http://localhost:8080;
}
}
1. Multiple Server Blocks: The configuration has two server blocks handling different domains, which might cause conflicts.
2. Proxy Configuration: The proxy setup might not properly handle the context path from Jetty (/my-webapp-0.1.0-standalone/
).
3. Configuration Loading: Nginx might be loading a different configuration file than expected.
Here's a corrected configuration that should work:
server {
listen 80;
server_name nomilkfor.me www.nomilkfor.me;
location / {
proxy_pass http://localhost:8080/my-webapp-0.1.0-standalone/;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
# Optional: Serve static files directly
location /static/ {
root /usr/share/nginx/html;
}
}
1. Check which configuration files Nginx is actually using:
nginx -T
2. Verify the syntax of your configuration:
nginx -t
3. Check the access and error logs for troubleshooting:
tail -f /var/log/nginx/error.log
tail -f /var/log/nginx/access.log
Based on your Jetty configuration (contextPath=/test
), you'll need to adjust the Nginx proxy_pass accordingly:
proxy_pass http://localhost:8080/test/;
Ensure your Nginx configuration:
- Points to the correct Jetty port (8080)
- Includes the correct context path (/test)
- Has proper proxy headers
- Is actually being loaded by Nginx
Remember to restart both Nginx and Jetty after making configuration changes:
sudo systemctl restart nginx
sudo systemctl restart jetty
When setting up Nginx as a reverse proxy for Jetty applications, developers often encounter a situation where Nginx appears to be running (showing in netstat and displaying the welcome page) but fails to properly proxy requests to the backend Jetty server. This typically manifests as either connection errors or serving static content instead of the proxied application.
The most frequent issues stem from:
1. Incorrect server_name directives
2. Missing or improper proxy_pass configuration
3. Conflicting root directives
4. Nginx loading unexpected configuration files
5. Jetty context path mismatches
First, verify which configuration files Nginx is actually using:
nginx -t # Tests configuration syntax
nginx -T # Shows complete configuration including included files
find /etc/nginx -name "*.conf" -exec grep -l "proxy_pass" {} \; # Finds all configs with proxy settings
Here's a working configuration that properly proxies to a Jetty application:
server {
listen 80;
server_name nomilkfor.me www.nomilkfor.me;
location / {
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/my-webapp-0.1.0-standalone/;
}
# Optional: Serve static files directly
location /static/ {
root /usr/share/nginx/html;
}
}
Ensure your Jetty configuration matches the proxy paths. For the example case, either:
1. Change Jetty's contextPath to "/" instead of "/test"
2. Or modify the Nginx proxy_pass to include "/test"
Add detailed logging to your Nginx configuration:
log_format proxy_log '$remote_addr - $remote_user [$time_local] '
'"$request" $status $body_bytes_sent '
'"$http_referer" "$http_user_agent" "$upstream_addr"';
server {
...
access_log /var/log/nginx/proxy.access.log proxy_log;
error_log /var/log/nginx/proxy.error.log debug;
}
After making changes:
sudo systemctl restart nginx
curl -I http://nomilkfor.me
tail -f /var/log/nginx/proxy.error.log