php5-fpm + nginx + google bot = 对等方重置连接

Posted

技术标签:

【中文标题】php5-fpm + nginx + google bot = 对等方重置连接【英文标题】:php5-fpm + nginx + google bot = connection reset by peer 【发布时间】:2013-06-02 22:08:33 【问题描述】:

所以我有一个疯狂的小时试图弄清楚为什么我的日志在过去的几个小时里每分钟都会出现几次缓慢的 php 脚本警告。

我最初专注于 php 慢日志和 php 错误日志,我吓坏了,以为这是我的代码。碰巧我正在实施一些 DNS 调整,这就是为什么我走错了路。

我最终检查了 nginx 错误日志,该日志显示从几乎相同的 IP 对等方重置了一行又一行的连接。

我用谷歌搜索了这些 IP,发现它属于谷歌,所以这显然是一个谷歌机器人/蜘蛛访问该网站。

这是错误日志的片段

2013/06/06 14:04:05 [error] 12313#0: *7435269 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 209.85.238.187, server: www.domain.com, request: "GET /c.html?q=xyz HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.domain.com"
2013/06/06 14:04:05 [error] 12308#0: *7435135 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 209.85.238.167, server: www.domain.com, request: "GET /c.html?q=xyz HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.domain.com"
2013/06/06 14:04:05 [error] 12308#0: *7435994 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 209.85.238.199, server: www.domain.com, request: "GET /c.html?q=xyz HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.domain.com"
2013/06/06 14:04:12 [error] 12309#0: *7436209 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 209.85.238.168, server: www.domain.com, request: "GET /c.html?q=xyz HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.domain.com"
2013/06/06 14:05:12 [error] 12309#0: *7441608 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 209.85.238.177, server: www.domain.com, request: "GET /c.html?q=xyz HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.domain.com"
2013/06/06 14:05:15 [error] 12310#0: *7440634 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 209.85.238.219, server: www.domain.com, request: "GET /c.html?q= xyz HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.domain.com"
2013/06/06 14:05:15 [error] 12313#0: *7441634 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 209.85.238.194, server: www.domain.com, request: "GET /c.html?q=xyz HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.domain.com"
2013/06/06 14:06:02 [error] 12310#0: *7444721 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 209.85.238.221, server: www.domain.com, request: "GET /c.html?q=xyz HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.domain.com"
2013/06/06 14:06:05 [error] 12308#0: *7443911 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 209.85.238.203, server: www.domain.com, request: "GET /c.html?q=xyz HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.domain.com"
2013/06/06 14:06:05 [error] 12309#0: *7445423 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 209.85.238.164, server: www.domain.com, request: "GET /c.html?q=xyz HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.domain.com"
2013/06/06 14:06:05 [error] 12310#0: *7445640 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 209.85.238.222, server: www.domain.com, request: "GET /c.html?q=xyz HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.domain.com"

peer 重置连接的原因是什么。谷歌机器人真的会访问页面并终止请求,只是为了检查它是否还活着?

这不是很好,因为它调用了我的 curl 请求,然后由于客户端退出,这些请求属于孤立线程。这意味着他们只是超时导致缓慢的 php 脚本。

还是我读错了?

【问题讨论】:

Goggle bot 在正常情况下不会终止请求。他们以正常方式访问页面。有时它们的命中率非常高,但它们不会终止连接。 【参考方案1】:

如果您查看错误消息,它会显示

从上游读取响应头时

这意味着问题不在于 google 正在终止请求,而是 nginx 的上游(恰好是 php-fpm)正在终止请求。通常,这是由正在运行的 php 代码错误引起的。

鉴于我们没有代码,以下是一些常规故障排除步骤:

在php-fpm的配置中,增加request_terminate_timeoutmax_input_timemax_execution_time的值。 在 php.ini 或 pool .conf 配置文件中激活错误日志记录(但不是“display_error”,如果是生产网站)。 尝试在正在运行的代码上运行调试器(xdebug 非常有用)来单步调试代码,您会发现大多数问题。

【讨论】:

以上是关于php5-fpm + nginx + google bot = 对等方重置连接的主要内容,如果未能解决你的问题,请参考以下文章

Debian 8 上的 nginx 1.6 别名 + php5-fpm = 404

在 ubuntu 上使用 nginx 和 php5-fpm 进行配置帮助

nginx 和 php5-fpm 仅适用于 ip 地址

又一个 FastCGI Primary Script Unknown 错误(nginx、php5-fpm)

关于 PHP5-FPM 和 NginX 的 MediaWiki PHP 会话

php5-fpm 的 display_errors 不适用于 nginx