# 连接到上游时连接被拒绝（111），客户端：192.168.1.1，服务器：example.com，上游："http://127.0.0.1:8080"

- **ID:** `nginx/upstream-server-refused-connection`
- **领域:** nginx
- **类别:** network_error
- **错误码:** `111`
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

上游服务器未在指定端口或 IP 上监听，可能是因为它未运行或绑定到了不同的地址。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| nginx 1.24.0 | active | — | — |
| nginx 1.22.1 | active | — | — |
| nginx 1.20.2 | active | — | — |

## 解决方案

1. ```
   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.
   ```
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;`
   ```

## 无效尝试

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