W3docs

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

This error message can have several causes.

This error message can have several causes. Here are some things you can try to troubleshoot the issue:

  1. Check if the upstream server (e.g., a backend server or a load balancer) is running and reachable. Instead of ping (which may be blocked by ICMP rules), use curl http://<upstream_ip>:<port> or nc -zv <upstream_ip> <port> to verify the port is open and accepting connections.
  2. Make sure that the hostname and port specified in the upstream directive in your NGINX configuration file are correct. For example:
    upstream backend {
        server 127.0.0.1:8080;
    }
  3. Check if there is a firewall blocking the connection. If you have a firewall configured on your server or on the network, make sure that it is not blocking the connection to the upstream server.
  4. Check the logs for any additional error messages that may provide more context about the problem. The NGINX error log is usually located at /var/log/nginx/error.log.
  5. If you are using a Unix-based operating system, you can also use the lsof command to see if there are any processes listening on the specified port.
  6. If you have recently made changes to your NGINX configuration, double-check the changes to make sure they are correct and restart NGINX to apply the changes. Additionally, verify that SELinux policies or container networking (e.g., Docker bridge networks) are not restricting outbound connections to the upstream service.

I hope these suggestions help you troubleshoot the issue! If you are still having problems, please provide more information about your setup and any additional error messages you are seeing.