# nginx：[警报] 1024 个工作连接不足以连接到上游

- **ID:** `nginx/worker-connections-are-not-enough-while-connecting-to-upstream`
- **领域:** nginx
- **类别:** resource_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

同时连接数超过了 worker_connections 限制，导致连接上游服务器失败。

## 版本兼容性

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

## 解决方案

1. ```
   增加 worker_connections 并调整系统限制：
events {
    worker_connections 4096;
    multi_accept on;
}
同时增加系统最大打开文件数：ulimit -n 65536 并在 /etc/security/limits.conf 中设置：
* soft nofile 65536
* hard nofile 65536
然后重新加载 nginx。
   ```
2. ```
   增加 worker_processes 数量以分配连接：
worker_processes auto;
这将使用所有 CPU 核心；每个 worker 有自己的连接池。
   ```
3. ```
   实施速率限制或按 IP 限制连接以防止滥用：
limit_conn_zone $binary_remote_addr zone=addr:10m;
limit_conn addr 100;
这将限制每个客户端的连接数，以保持在 worker_connections 范围内。
   ```

## 无效尝试

- **** — System limits (ulimit -n, fs.file-max) may still cap the total connections; also the OS may have per-process limits. (50% 失败率)
- **** — While it reduces connection duration, it increases connection churn and may still hit the limit under high concurrency. (40% 失败率)
- **** — Multi_accept affects connection acceptance behavior but does not increase the maximum number of connections. (30% 失败率)
