`
月影无痕
  • 浏览: 1003046 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

nginx php php-fpm Connection reset by peer的原因及解决办法

 
阅读更多

Connection reset by peer

这个错误是在nginx的错误日志中发现的,为了更全面的掌握nginx运行的异常,强烈建议在nginx的全局配置中增加

error_log   logs/error.log notice;

这样,就可以记录nginx的详细异常信息。

 


nginx的错误日志中会出现Connection reset by peer) while reading response header from upstream, client: 1.1.1.1, server: 102.local, request: "GET / HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000

 

后来反复检查,发现这是因为php运行较慢,并超出php-fpm.conf的request_terminate_timeout设置的秒数。

 

request_terminate_timeout用于设置当某个php脚本运行最长时间,若超出php-fpm进程管理器强行中止当前程序,并关闭fastcgi和nginx的网络连接,然后nginx中就会出现Connection reset by peer的错误了。

 

也就是说,产生这个错误的原因是:

php 程序的运行时间超出request_terminate_timeout设置的值。

在php-fpm环境下,在php的安装目录的etc/php-fpm.conf中有此值的设置项,可将其设置为0或更大的值。

 

提示,在php.ini中存在一项max_execution_time,也用于设置php脚本的最长执行时间。但在php-fpm环境下,我发现max_execution_time的设置是无效的,只有request_terminate_timeout产生了作用。

 

总结:请将request_terminate_timeout设置为较大的值或0(请自行承担可能带来的风险),可减少因php脚本执行时行过长导致nginx产生Connection reset by peer错误。

 

归根结底,还是php程序执行性能太差,导致超时退出。

 

开启fpm的slowlog, 及时发现性能问题:

request_slowlog_timeout = 1s

 

slowlog = var/log/slow.log

request_terminate_timeout=30s

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics