![是否应从 Nginx 配置中删除 `keepalive_timeout`?](https://linux22.com/image/629437/%E6%98%AF%E5%90%A6%E5%BA%94%E4%BB%8E%20Nginx%20%E9%85%8D%E7%BD%AE%E4%B8%AD%E5%88%A0%E9%99%A4%20%60keepalive_timeout%60%EF%BC%9F.png)
哪种配置/优化更好:明确限制keepalive_timeout
还是允许 Nginx 自行终止保持连接?
我看到过两条关于keepalive_timeout
Nginx 指令的相互矛盾的建议。它们如下:
# How long to allow each connection to stay idle; longer values are better
# for each individual client, particularly for SSL, but means that worker
# connections are tied up longer. (Default: 65)
keepalive_timeout 20;
和
# You should remove keepalive_timeout from your formula.
# Nginx closes keepalive connections when the
# worker_connections limit is reached.
这Nginx 文档因为keepalive_timeout
没有提到自动杀戮,而且我只见过一次这样的建议,但它引起了我的兴趣。
该服务器提供只TLS 安全连接和所有非加密连接都会立即重新路由到https://
相同 URL 的版本。
答案1
当达到 worker_connections 限制时,Nginx 将关闭保持连接。
这是真的。
- src/核心/ngx_connection.c:l843-
ngx_drain_connections();
- src/http/ngx_http_request.c:l3042-
ngx_reusable_connection(c, 1);