问题描述
文档说明如下
设置代理的 HTTP 协议版本.默认情况下,使用 1.0 版.建议将 1.1 版与 keepalive 连接和 NTLM 身份验证一起使用.
在我的 nginx 配置中
In my nginx config I have
location / {
proxy_http_version 1.1;
proxy_pass http://127.0.0.1:1980;
}
直接执行 http://127.0.0.1:1980 我可以看到我的应用收到很多请求(当我刷新)在一个连接上.这是我发送的回复
Doing http://127.0.0.1:1980 directly I can see my app get many request (when I refresh) on one connection. This is the response I send
HTTP/1.1 200 OKContent-Type:text/htmlContent-Length: 14Connection: keep-aliveHello World!
然而,nginx 发出一个请求并关闭它.什么?我可以看到 nginx 发送Connection: keep-alive"标头.我可以看到它添加了服务器和日期标头.我尝试添加 proxy_set_header Connection "keep-alive";
但这没有帮助.
However nginx makes one request and closes it. WTH? I can see nginx sends the "Connection: keep-alive" header. I can see it added the server and date header. I tried adding proxy_set_header Connection "keep-alive";
but that didn't help.
如何让 nginx 不关闭每个线程的连接?
How do I get nginx to not close the connection every thread?
推荐答案
为了让Nginx保持连接活跃,需要进行如下配置:
In order Nginx to keep connection alive, the following configuration is required:
配置合适的头部(HTTP 1.1 和 Connection 头部不包含Close"值,实际值无所谓,Keep-alive 或只是一个空值)
Configure appropriate headers (HTTP 1.1 and Connection header does not contain "Close" value, the actual value doesn't matter, Keep-alive or just an empty value)
使用带有 keepalive 指令的上游块,只是 proxy_pass url 不起作用
Use upstream block with keepalive instruction, just proxy_pass url won't work
源服务器应该启用keep-alive
Origin server should have keep-alive enabled
因此,以下 Nginx 配置使 keepalive 为您工作:
So the following Nginx configuration makes keepalive working for you:
upstream {
server 127.0.0.1:1980;
keepalive 64;
};
server {
location / {
proxy_pass http://upstream;
proxy_set_header Connection "";
proxy_http_version 1.1;
}
}
根据 RFC-793,确保您的源服务器没有完成连接第 3.5 节:
一个 TCP 连接可以通过两种方式终止: (1) 正常的 TCP 关闭使用 FIN 握手的序列,以及 (2) 中止",其中一个或更多的 RST 段被发送,连接状态立即丢弃.如果远程站点关闭了 TCP 连接,则本地必须通知应用程序是正常关闭还是被关闭中止.
可以在另一个答案中找到更多详细信息在 Stackoverflow 上.
A bit more details can be found in the other answer on Stackoverflow.
这篇关于为什么 nginx proxy_pass 关闭我的连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!