104:从上游(Nginx)读取响应头时对等方重置连接

Nig*_*ton 58 php http nginx

我有一台服务器一直正常工作,直到 2013 年 10 月 3 日上午 10:50,它开始间歇性地向客户端返回“502 Bad Gateway”错误。

大约五分之四的浏览器请求成功,但大约五分之一的请求失败并显示 502。

nginx 错误日志包含数百个这样的错误;

2013/10/05 06:28:17 [error] 3111#0: *54528 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 66.249.66.75, server: www.bec-components.co.uk  request: ""GET /?_n=Fridgefreezer/Hotpoint/8591P;_i=x8078 HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.bec-components.co.uk"
Run Code Online (Sandbox Code Playgroud)

但是 PHP 错误日志不包含任何匹配的错误。

有没有办法让 PHP 给我更多关于它为什么要重置连接的信息?

这是nginx.conf;

user              www-data;
worker_processes  4;
error_log         /var/log/nginx/error.log;
pid               /var/run/nginx.pid;

events {
   worker_connections  1024;
}

http {
  include          /etc/nginx/mime.types;
  access_log       /var/log/nginx/access.log;

  sendfile               on;
  keepalive_timeout      30;
  tcp_nodelay            on;
  client_max_body_size   100m;

  gzip         on;
  gzip_types   text/plain application/xml text/javascript application/x-javascript text/css;
  gzip_disable "MSIE [1-6]\.(?!.*SV1)";

  include /gvol/sites/*/nginx.conf;

}
Run Code Online (Sandbox Code Playgroud)

这是.conf本网站的内容;

server {

  server_name   www.bec-components.co.uk bec3.uk.to bec4.uk.to bec.home;
  root          /gvol/sites/bec/www/;
  index         index.php index.html;

  location ~ \.(js|css|png|jpg|jpeg|gif|ico)$ {
    expires        2592000;   # 30 days
    log_not_found  off;
  }

  ## Trigger client to download instead of display '.xml' files.
  location ~ \.xml$ {
    add_header Content-disposition "attachment; filename=$1";
  }

   location ~ \.php$ {
      fastcgi_read_timeout  3600;
      include               /etc/nginx/fastcgi_params;
      keepalive_timeout     0;
      fastcgi_param         SCRIPT_FILENAME  $document_root$fastcgi_script_name;
      fastcgi_pass          127.0.0.1:9000;
      fastcgi_index         index.php;
   }
}

## bec-components.co.uk ##
server {
   server_name   bec-components.co.uk;
   rewrite       ^/(.*) http://www.bec-components.co.uk$1 permanent;
}
Run Code Online (Sandbox Code Playgroud)

tha*_*ere 29

如果我的网络服务器告诉我: 502 Bad Gateway

  • 您的 fastcgi/nginx - 进程的正常运行时间是多少?
  • 你监控网络连接吗?
  • 你能确认/否认当天访问人数的变化吗?

这是什么意思:

  • nginx 无法访问您的 fastcgi 进程;要么变慢,要么根本不对应。坏网关意味着:nginx 不能 fastcgi_pass 到定义的资源 127.0.0.1:9000;在那个非常特殊的时刻

  • 您的初始错误日志说明了一切:

.

recv() failed 
    -> nginx failed

(104: Connection reset by peer) while reading response header from upstream, 
    -> no complete answer, or no answer at all
upstream: "fastcgi://127.0.0.1:9000", 
    -> who is he, who failed???
Run Code Online (Sandbox Code Playgroud)

从我有限的观点来看,我建议:

  • 重新启动您的 fastcgi_process / 服务器
  • 检查您的访问日志
  • 启用调试日志

  • 我明白了,所以在这种情况下的“网关”是 PHP 服务器。谢谢你。 (2认同)
  • `重新启动你的 fastcgi_process / server` 对我有帮助,thans (2认同)

小智 17

我知道这个话题很老了,但偶尔还是会继续弹出,于是上网找答案,想到了以下三种可能:

  1. 一个编程错误有时是segfaulting php-fpm,这反过来意味着与nginx的连接将被切断。这通常会留下至少一些日志和/或核心转储,可以进一步分析。
  2. 出于某种原因,PHP 无法编写会话文件(通常为: session.save_path = "/var/lib/php/sessions")。这可能是错误的权限、错误的所有权、错误的用户/组,或者更深奥/晦涩的问题,例如该目录上的 inode 耗尽(甚至是一个完整的磁盘!)。这通常不会留下许多核心转储,甚至可能不会在 PHP 错误日志中留下任何内容。
  3. 调试起来更棘手:扩展程序行为不端(偶尔会遇到某种内部限制,或一直未触发的错误)、段错误,并使 php-fpm 进程随之关闭——从而关闭与 nginx 的连接. 通常的罪魁祸首是 APC、memcache/d 等(在我的例子中是 New Relic 扩展),所以这里的想法是关闭每个扩展,直到错误消失。


小智 8

一直得到这个。通过增加opcache内存限制来解决它,如果你使用它(APC 的替代品)。每当缓存太满时,PHP-FPM 似乎都会断开连接。这也是 shgnInc 的回答在短时间内修复它的原因。

因此,找到该文件/etc/php5/fpm/php.ini(或您的发行版中的等效文件)并增加到memory_consumption您的站点需要的任何级别。禁用opcache也可能有效。

[opcache]
opcache.memory_consumption = 196 
Run Code Online (Sandbox Code Playgroud)


shg*_*Inc 6

在我遇到同样问题的情况下,我只需重新启动php-fpm服务即可解决。

sudo service php5-fpm restart
Run Code Online (Sandbox Code Playgroud)

或者有时这个问题会因为大量的请求而发生。默认情况下pm.max_requests,php5-fpm 可能是 100 或更低。

要解决它,请根据您站点的请求增加其值,例如 500。

之后你必须重新启动服务