# connect() failed (111: Connection refused) while connecting to upstream, client: 192.168.1.1, server: example.com, upstream: "http://127.0.0.1:8080"

- **ID:** `nginx/upstream-server-refused-connection`
- **Domain:** nginx
- **Category:** network_error
- **Error Code:** `111`
- **Verification:** ai_generated
- **Fix Rate:** 95%

## Root Cause

The upstream server is not listening on the specified port or IP, either because it is not running or is bound to a different address.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| nginx 1.24.0 | active | — | — |
| nginx 1.22.1 | active | — | — |
| nginx 1.20.2 | active | — | — |

## Workarounds

1. **Verify the upstream service is running: `sudo systemctl status myapp` or `netstat -tlnp | grep 8080`. Start it if needed: `sudo systemctl start myapp`.** (95% success)
   ```
   Verify the upstream service is running: `sudo systemctl status myapp` or `netstat -tlnp | grep 8080`. Start it if needed: `sudo systemctl start myapp`.
   ```
2. **Check if the upstream is listening on a different interface (e.g., 0.0.0.0 vs 127.0.0.1). Update the proxy_pass directive to match the actual listening address.** (90% success)
   ```
   Check if the upstream is listening on a different interface (e.g., 0.0.0.0 vs 127.0.0.1). Update the proxy_pass directive to match the actual listening address.
   ```
3. **If using a Unix socket, ensure the socket file exists and has correct permissions. Example: `ls -la /var/run/myapp.sock` and adjust nginx upstream to `server unix:/var/run/myapp.sock;`** (85% success)
   ```
   If using a Unix socket, ensure the socket file exists and has correct permissions. Example: `ls -la /var/run/myapp.sock` and adjust nginx upstream to `server unix:/var/run/myapp.sock;`
   ```

## Dead Ends

- **** — Connection refused is an immediate TCP error; timeout does not help. (90% fail)
- **** — The error is about TCP connection, not URI path issues. (70% fail)
- **** — Nginx is working fine; the upstream service is down or misconfigured. (80% fail)
