Fixing the Connect() Failed (111: Connection Refused) Error While Connecting to Upstream: A Step-By-Step Guide

If you've encountered the "Connect() Failed (111: Connection Refused) Error While Connecting to Upstream" issue while working with Nginx, you're not alone. This error often occurs when Nginx is unable to establish a connection with the upstream server. In this guide, we'll explain the root causes of this error and walk you through the process of resolving it step-by-step.

Table of Contents

Understanding the Error

The connect() failed (111: Connection refused) error while connecting to upstream error occurs when Nginx is unable to establish a connection with the specified upstream server. This can be caused by several factors, including:

  • The upstream server is down or not responding
  • Incorrect configuration settings in Nginx
  • Firewall or security group rules blocking the connection
  • Network issues between Nginx and the upstream server

Step 1: Check the Upstream Server

Before diving into the Nginx configuration, first, make sure the upstream server is running and responding to requests. You can use the following command to check if the upstream server is listening on the expected port:

sudo netstat -tuln | grep LISTEN

If the upstream server is not running or not responding, restart it and ensure it's running correctly.

Step 2: Verify Nginx Configuration

Next, examine your Nginx configuration files to ensure you've specified the correct upstream server settings. Look for the upstream and proxy_pass directives in your Nginx configuration file (usually located at /etc/nginx/nginx.conf or /etc/nginx/sites-available/default):

http {
  upstream backend {
    server backend_server_ip:backend_server_port;
  }

  server {
    ...
    location / {
      proxy_pass http://backend;
    }
    ...
  }
}

Ensure that the backend_server_ip and backend_server_port values are correct, and the upstream server is listening on the specified port.

After making any necessary changes, reload the Nginx configuration using:

sudo nginx -t && sudo nginx -s reload

Step 3: Check Firewall and Security Group Rules

Firewalls or security group rules might be blocking Nginx from connecting to the upstream server. Check your firewall settings to ensure that the required ports are open:

  • On Linux systems, use iptables or firewalld to check and update firewall rules.
  • On cloud platforms like AWS or GCP, verify the security group rules associated with your instances.

Ensure that the rules allow incoming connections on the port used by the upstream server.

Step 4: Monitor Logs

Keep an eye on both the Nginx and upstream server logs to identify any issues. The Nginx error log is usually located at /var/log/nginx/error.log. Tail the log in real-time using:

sudo tail -f /var/log/nginx/error.log

For the upstream server, check the relevant logs, depending on the application or service you're using.

Step 5: Test Connectivity

After completing the previous steps, test the connectivity between Nginx and the upstream server using tools like curl, telnet, or nc. For example:

curl http://backend_server_ip:backend_server_port

If the connection is successful, you should no longer encounter the "Connect() Failed (111: Connection Refused) Error While Connecting to Upstream" error.

FAQs

Q1: Can SELinux cause the "Connect() Failed (111: Connection Refused) Error While Connecting to Upstream"?

Yes, SELinux can be a source of this error. If you suspect that SELinux is the cause, you can temporarily disable it using the command sudo setenforce 0 and retest the connection. If the error disappears, you'll need to properly configure SELinux to allow the connection.

Q2: How can I check if my Nginx configuration has errors?

You can use the nginx -t command to test your Nginx configuration for any syntax errors:

sudo nginx -t

Q3: How do I restart Nginx after making changes to the configuration files?

To restart Nginx, use the following command:

sudo systemctl restart nginx

Q4: Can I use multiple upstream servers in my configuration?

Yes, Nginx supports load balancing and failover using multiple upstream servers. You can specify multiple server directives within the upstream block:

http {
  upstream backend {
    server backend_server1_ip:backend_server1_port;
    server backend_server2_ip:backend_server2_port;
  }
  ...
}

Q5: How can I enable SSL between Nginx and the upstream server?

To enable SSL between Nginx and the upstream server, you'll need to configure Nginx with the necessary SSL certificates and update the proxy_pass directive to use https. Check out this SSL configuration guide for detailed instructions.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Lxadm.com.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.