# “limit_conn_zone”中的 1024 个区域不够

- **ID:** `nginx/limit-conn-zone-too-small`
- **领域:** nginx
- **类别:** resource_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

limit_conn_zone 指令为每个键分配了固定数量的插槽（默认 1024），在高并发下会耗尽。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| nginx/1.18.0 | active | — | — |
| nginx/1.20.0 | active | — | — |
| nginx/1.24.0 | active | — | — |
| nginx/1.26.0 | active | — | — |

## 解决方案

1. ```
   增加 limit_conn_zone 指令中的区域大小：`limit_conn_zone $binary_remote_addr zone=addr:10m;`（默认是 1m，可容纳约 1024 个区域）
   ```
2. ```
   如果使用多个键，为每个键创建单独的区域以分散负载：`limit_conn_zone $server_name zone=server:5m; limit_conn_zone $binary_remote_addr zone=addr:5m;`
   ```
3. ```
   使用状态模块监控区域使用情况并动态调整：在 location 中添加 `stub_status;` 并检查活跃连接以估算所需的区域大小。
   ```

## 无效尝试

- **** — limit_conn controls the maximum number of connections per key, not the number of zones/slots available for tracking. (80% 失败率)
- **** — This eliminates the error but also removes the intended connection limiting functionality, potentially leading to resource exhaustion. (30% 失败率)
- **** — Large zones consume significant memory; if the system runs out of memory, nginx may crash or fail to start. (50% 失败率)
