Skip to main content
nginxintermediatenetworkingVerified

Nginx Connection Refused: Causes and Fixes

Last reviewed: 9/14/2026
3 solutions

Exact Error Message

connect() failed (111: Connection refused) while connecting to upstream

Quick Fix

Check if upstream service is running and listening on the correct port.

What This Error Means

Connection refused occurs when Nginx tries to connect to an upstream server but the server is not running, not listening on the expected port, or actively refusing connections.

Common Symptoms
  • 502 Bad Gateway errors
  • Connection refused in Nginx logs
  • Upstream unreachable
  • Proxy requests fail
Common Causes
  • Upstream service not running
  • Wrong port in upstream configuration
  • Firewall blocking connection
  • Service binding to wrong interface
  • Upstream overloaded or rejecting connections
Diagnostic Steps
  1. 1Check Nginx error logs
  2. 2Verify upstream service is running
  3. 3Check which port upstream is listening on
  4. 4Test connectivity from Nginx server
  5. 5Check firewall rules

Solutions

Solution 1: Start upstream service
  1. 1Check if upstream service is running
  2. 2Start the upstream service
  3. 3Enable service to start on boot
  4. 4Verify service is listening

Commands to Run

Service name varies by application

sudo systemctl start SERVICE_NAME

Requires sudo access

sudo systemctl status SERVICE_NAME
netstat -tlnp | grep PORT
Solution 2: Fix upstream configuration
  1. 1Check Nginx upstream configuration
  2. 2Verify port is correct
  3. 3Check host or IP address
  4. 4Reload Nginx configuration

Commands to Run

[

sudo nginx -t

'

sudo systemctl reload nginx
Solution 3: Fix firewall rules
  1. 1Check firewall status
  2. 2Allow required ports
  3. 3Verify firewall rules
  4. 4Test connection

Commands to Run

Opening ports affects security

sudo ufw allow PORT/tcp

Only allow from trusted networks

sudo firewall-cmd --add-port=PORT/tcp --permanent
Prevention Tips
  • Use health checks for upstream services
  • Monitor upstream service health
  • Use load balancing for high availability
  • Configure proper timeouts
  • Monitor Nginx error logs

Version Notes: Applies to Nginx 1.18+

Was this helpful?