简体   繁体   English

Nginx uwsgi (104: Connection reset by peer) 同时从上游读取响应头

[英]Nginx uwsgi (104: Connection reset by peer) while reading response header from upstream

Environment is Nginx + uwsgi.环境是Nginx + uwsgi。

Getting a 502 bad gateway error from Nginx on certain GET requests.在某些 GET 请求上从 Nginx 获取 502 bad gateway 错误。 Seems to be related to the length of the URL.好像跟网址的长度有关。 In our particular case, it was a long list of GET parameters.在我们的特殊情况下,它是一长串 GET 参数。 Shorten the GET parameters and no 502 error.缩短GET参数,没有502错误。

From the nginx/error.log从 nginx/error.log

[error] 22113#0: *1 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 192.168.1.100, server: server.domain.com, request: "GET <long_url_here>"

No information in the uwsgi error log. uwsgi 错误日志中没有信息。

After spending a lot of time on this, I finally figured it out.在花了很多时间之后,我终于弄明白了。 There are many references to Nginx and connection reset by peer.有很多对 Nginx 和对等连接重置的参考。 Most of them seemed to be related to PHP.他们中的大多数似乎都与 PHP 相关。 I couldn't find an answer that was specific to Nginx and uwsgi.我找不到特定于 Nginx 和 uwsgi 的答案。

I finally found a reference to fastcgi and a 502 bad gateway error ( https://support.plesk.com/hc/en-us/articles/213903705 ).我终于找到了对 fastcgi 的引用和 502 bad gateway 错误( https://support.plesk.com/hc/en-us/articles/213903705 )。 That lead me to look for a buffer size limit in the uwsgi configuration which exists as buffer-size .这导致我在 uwsgi 配置中寻找缓冲区大小限制,该限制作为buffer-size存在。 The default value is 4096. From the documentation, it says:默认值为 4096。从文档中,它说:

If you plan to receive big requests with lots of headers you can increase this value up to 64k (65535).如果您计划接收带有大量标头的大型请求,您可以将此值增加到 64k (65535)。

There are many ways to configure uwsgi, I happen to use a .ini file.配置uwsgi的方法有很多,我正好用了一个.ini文件。 So in my .ini file I tried:所以在我的 .ini 文件中我试过:

buffer-size=65535

This fixed the problem.这解决了问题。 You can adjust that to taste.你可以根据口味调整它。 Maybe start with the max and work back until you have an acceptable value, or just leave it at the max.也许从最大值开始并返回,直到获得可接受的值,或者将其保留在最大值。

This was frustrating to track down because there was no error on the uwsgi side of things.这令人沮丧,因为在 uwsgi 方面没有错误。

I was getting the same nginx error and also there was no information in uwsgi log.我收到了同样的 nginx 错误,而且 uwsgi 日志中也没有信息。 The problem was that in some cases the application was not consuming the whole request body as advised in http://uwsgi-docs.readthedocs.org/en/latest/ThingsToKnow.html :问题是,在某些情况下,应用程序没有按照http://uwsgi-docs.readthedocs.org/en/latest/ThingsToKnow.html 中的建议使用整个请求正文:

If an HTTP request has a body (like a POST request generated by a form), you have to read (consume) it in your application.如果 HTTP 请求有正文(如表单生成的 POST 请求),则必须在应用程序中读取(使用)它。 If you do not do this, the communication socket with your webserver may be clobbered.如果您不这样做,与您的网络服务器的通信套接字可能会被破坏。 If you are lazy you can use the post-buffering option that will automatically read data for you.如果你很懒惰,你可以使用后缓冲选项,它会自动为你读取数据。 For Rack applications this is automatically enabled.对于机架应用程序,这是自动启用的。

Of course, this is not a problem in your case, but it may be useful for others who are getting the same nginx error.当然,这在您的情况下不是问题,但对于遇到相同 nginx 错误的其他人来说可能有用。

我们只需要将 php.ini 中的属性“output_buffering”值增加到更大的值,例如 65535 或其他合适的值。

When we receive a message like (104: Connection reset by peer) while reading response header from upstream , most often, we could blame the upstream side of this kind of error.当我们(104: Connection reset by peer) while reading response header from upstream时收到类似(104: Connection reset by peer) while reading response header from upstream ,大多数情况下,我们可以将这种错误归咎于上游。

As described, the connection was reset by the upstream peer, not by nginx itself.如上所述,连接是由上游对等方重置的,而不是由 nginx 本身重置的。 Nginx as a client can barely do anything to make it right. Nginx 作为客户端几乎无法做任何事情来使它正确。

I'm suspecting if modifying buffer-size will do the magic.我怀疑修改缓冲区大小是否会起作用。 Basically the command changes the buffer size where response headers are cached.基本上,该命令会更改缓存响应标头的缓冲区大小。 This would take effect when the response header is too big, of which case we receive a message saying upstream sent too big header while reading response header from upstream , and that is totally different thing from connection reset by peer .这会在响应头太大时生效,在这种情况下,我们upstream sent too big header while reading response header from upstream收到一条消息说upstream sent too big header while reading response header from upstream ,这与connection reset by peer完全不同。

Since this kind of error is trigger randomly, I would suggest you check whether nginx uses keepalive when talking to upstreams.由于这种错误是随机触发的,我建议您在与上游对话时检查nginx是否使用keepalive If this was the case, the connection might be reset by upstream server when the idle timed out whereas nginx had no idea that the connection had been dropped, hence forwarding the request using the same connection.如果是这种情况,当空闲超时时上游服务器可能会重置连接,而 nginx 不知道连接已断开,因此使用相同的连接转发请求。

There's no elegant solution to fix it as far as I know.据我所知,没有优雅的解决方案来修复它。 You could do retry or set a keepalive_timeout value to the upstream connection pool in nginx to avoid the problem.您可以重试或为 nginx 中的上游连接池设置keepalive_timeout值以避免该问题。

referencing:参考:

Apache HttpClient Interim Error: NoHttpResponseException Apache HttpClient 临时错误:NoHttpResponseException

http://tengine.taobao.org/document/http_upstream_keepalive_timeout.html http://tengine.taobao.org/document/http_upstream_keepalive_timeout.html

--post-buffering 32768 worked for me as suggested (and discouraged) here NGINX + uWSGI Connection Reset by Peer --post-buffering 32768按照建议(和不鼓励)在此处NGINX + uWSGI Connection Reset by Peer为我工作

I don't have time to investigate it further at the moment (quick prototyping mode :), but since it took me a lot of time to find this hack, it might be worth posting here.我目前没有时间进一步研究它(快速原型模式:),但由于我花了很多时间来找到这个 hack,所以可能值得在这里发布。

It doesn't come up occasionally.它不会偶尔出现。

I guess the most possible reason of that is the size of your php-fpm.log is oversize.我想最可能的原因是你的php-fpm.log的大小过大。 Try to change your log_level to upper level in php-fpm.conf and clear the logs.尝试在php-fpm.conf log_level更改为上层并清除日志。

Anyway, it works for me.无论如何,它对我有用。

This can happen if your request/response headers are quite large.如果您的请求/响应标头非常大,就会发生这种情况。

To fix it, in /etc/uwsgi/apps-available/your-app.ini add buffer-size=65535要修复它,在/etc/uwsgi/apps-available/your-app.ini添加buffer-size=65535

您需要重新安装 PHP:

apt-get install --reinstall php5-fpm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从上游读取响应 header 时,仅 POST API 给出 502 badgatway =&gt; recv() failed (104: Connection reset by peer) - Only POST API giving 502 badgatway => recv() failed (104: Connection reset by peer) while reading response header from upstream Django gunicorn nginx“读取响应头时由同行重置连接” - Django gunicorn nginx “connection reset by peer while reading response header” * 10上游超时(110:连接超时),同时使用uwsgi从上游读取响应头 - *10 upstream timed out (110: Connection timed out) while reading response header from upstream with uwsgi Nginx连接重置,来自uWsgi的响应丢失 - Nginx connection reset, response from uWsgi lost connectionreseterror: (errno 104) 连接被对等方重置 - connectionreseterror: (errno 104) connection reset by peer django errno 104 连接由对等方重置 - django errno 104 Connection reset by peer 连接中止。,Django 中的错误(104,“对等连接重置”) - Connection aborted., error(104, 'Connection reset by peer') in Django 结构rsync:读取错误:对等连接重置(104) - Fabric rsync: read error: Connection reset by peer (104) Django S3 [Errno 104]对等重置连接 - Django S3 [Errno 104] Connection reset by peer 错误:[Errno 104]由对等方重置连接-开发服务器IE 9 - error: [Errno 104] Connection reset by peer - dev server IE 9
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM