为什么Nginx proxy_pass关闭我的连接? [英] Why does nginx proxy_pass close my connection?

查看:278
本文介绍了为什么Nginx proxy_pass关闭我的连接?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档中的内容如下

设置用于代理的HTTP协议版本.默认情况下,使用1.0版.建议将1.1版与Keepalive连接和NTLM身份验证配合使用.

Sets the HTTP protocol version for proxying. By default, version 1.0 is used. Version 1.1 is recommended for use with keepalive connections and NTLM authentication.

在我的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 OK \ nContent-Type:文本/html \ nContent-Length:14 \ n连接:keep-alive \ n \ nHello World!

HTTP/1.1 200 OK\nContent-Type:text/html\nContent-Length: 14\nConnection: keep-alive\n\nHello 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"值,实际值无关紧要,保持活动状态还是只是空值)

  • 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

原始服务器应启用了保持活动状态

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连接被远程站点关闭,则本地 必须通知应用程序是正常关闭还是关闭 流产了.

A TCP connection may terminate in two ways: (1) the normal TCP close sequence using a FIN handshake, and (2) an "abort" in which one or more RST segments are sent and the connection state is immediately discarded. If a TCP connection is closed by the remote site, the local application MUST be informed whether it closed normally or was aborted.

更多详细信息可以在其他答案中找到在Stackoverflow上.

A bit more details can be found in the other answer on Stackoverflow.

这篇关于为什么Nginx proxy_pass关闭我的连接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆