本文介绍了Nginx + Php-fpm fastcgi 上游超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用长时间运行的 PHP 脚本时遇到问题:

I am having issues with a long-running PHP script:

<?php
sleep(70); # extend 60s
phpinfo();

每次在 60 秒后终止,并从 Nginx 响应 504 Gateway Time-out.

Which gets terminated every time after 60 seconds with a response 504 Gateway Time-out from Nginx.

当我检查 Nginx 错误时,我可以看到请求超时:

When I inspect the Nginx errors I can see that the request times out:

... [error] 1312#1312: *2023 upstream timed out (110: Connection timed out) while reading response header from upstream, ... , upstream: "fastcgi://unix:/run/php/php7.0-fpm.sock", ...

我浏览了相关问题并尝试增加超时时间,创建一个 /etc/nginx/conf.d/timeout.conf 文件,其内容如下:

I went through the related questions and tried increasing the timeouts creating a /etc/nginx/conf.d/timeout.conf file with the following content:

proxy_connect_timeout 600;
proxy_send_timeout 600;
proxy_read_timeout 600;
send_timeout 600;
fastcgi_read_timeout 600;
fastcgi_send_timeout 600;
fastcgi_connect_timeout 600;

我还通读了 fastcgi 的 Nginx 文档href="http://nginx.org/en/docs/http/ngx_http_core_module.html" rel="nofollow noreferrer">core 模块,搜索默认设置为 60 秒的任何配置.

I also read through the Nginx documentation for both fastcgi and core modules, searching for any configurations with defaults set to 60 seconds.

我排除了 client_* 超时,因为它们返回 HTTP 408 而不是 HTTP 504 响应.

I ruled out the client_* timeouts because they return HTTP 408 instead of HTTP 504 responses.

这是我的 FastCGI 的 Nginx 服务器配置部分:

This is my Nginx server config portion of FastCGI:

location ~ \.php$ {
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
    include fastcgi_params;
}

从我目前所读到的,这似乎不是 PHP 的问题,而是 Nginx 是超时的罪魁祸首.尽管如此,我也尝试修改 PHP 中的限制:

From what I read so far this doesn't seem to be an issue with PHP rather Nginx is to blame for the timeout. Nonetheless, I tried modifying the limits in PHP as well:

来自 phpinfo() 的值:

default_socket_timeout=600
max_execution_time=300
max_input_time=-1
memory_limit=512M

php-fpm 池配置还启用了以下功能:

The php-fpm pool config also has the following enabled:

catch_workers_output = yes
request_terminate_timeout = 600

php-fpm 日志中没有任何内容.

There is nothing in the php-fpm logs.

我也是用亚马逊的Load Balancer路由到服务器,但是超时配置也从默认 60 秒.

I am also using Amazon's Load Balancer to route to the server, but the timeout configuration is also increased from the default 60 seconds.

我不知道还能在哪里查看,在所有更改期间,我都重新启动了 php-fpm 和 nginx.

I don't know where else to look, during all the changes I restarted both php-fpm and nginx.

谢谢

推荐答案

在这些情况下,我实际上是在编辑一个错误的配置文件,该文件没有被 Nginx 加载.

As it happens in these cases, I was actually editing a wrong configuration file that didn't get loaded by Nginx.

将以下内容添加到正确的文件即可:

Adding the following to the right file did the trick:

fastcgi_read_timeout 600;
fastcgi_send_timeout 600;
fastcgi_connect_timeout 600;

这篇关于Nginx + Php-fpm fastcgi 上游超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-17 12:16
查看更多