PHP-FPM与Nginx通信报 502 Bad Gateway或504 Gateway Timeout终极解决方案(适用于PHP执行耗时任务情况下的报错)

这篇具有很好参考价值的文章主要介绍了PHP-FPM与Nginx通信报 502 Bad Gateway或504 Gateway Timeout终极解决方案(适用于PHP执行耗时任务情况下的报错)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

前置条件:

适用于常规请求都没问题,但是执行某些php脚本需要超过一分钟的情况下的502/504,并不是任何请求都502/504的情况(这说明php-fpm或者nginx配置错误)。

出现502/504的原因

502

执行脚本时间太长,期间php没有返回任何的数据。php-fpm超时,nginx没超时。nginx认为php-fpm罢工了,然后抛出了异常。

504

执行脚本时间太长,期间php没有返回任何的数据。php-fpm没超时,nginx超时。nginx认为php-fpm响应太慢,nginx没憋住抛出了异常。

不生效的解决方案(防止各位师傅踩坑):

代码

set_time_limit(0);
ignore_user_abort(true);
ini_set('max_execution_time', 600);

不生效原理剖析

以上代码的作用设置了php代码本身可以更长的时间处理任务并且不报致命错误,但不代表程序一定无限制的可以执行这么久。因为Nginx与PHP进程通信方式是检测到.php的文件交给php-fpm进程处理,php-fpm是一个fastcgi进程管理器,php-fpm一旦超时,php-fpm会强制终结掉这个进程,这就是报502的原因。
这段代码又无法控制nginx fastcgi的一些机制,所以报504的原因。
意味着仅代码层配置还不够,服务器也得配置。

官方文档对这2个函数和1个配置的解释:

##### set_time_limit:
Set the number of seconds a script is allowed to run. If this is reached, the script returns a fatal error. The default limit is 30 seconds or, if it exists, the max_execution_time value defined in the php.ini.

##### ignore_user_abort:
Set whether a client disconnect should abort script execution.When running PHP as a command line script, and the script's tty goes away without the script being terminated then the script will die the next time it tries to write anything, unless enable is set to true.

##### max_execution_time:
This sets the maximum time in seconds a script is allowed to run before it is terminated by the parser. This helps prevent poorly written scripts from tying up the server. The default setting is 30. When running PHP from the command line the default setting is 0.
On non Windows systems, the maximum execution time is not affected by system calls, stream operations etc. Please see the set_time_limit() function for more details.
**Your web server can have other timeout configurations that may also interrupt PHP execution..Apache has a Timeout directive and IIS has a CGI timeout function. Both default to 300 seconds. See your web server documentation for specific details.**
加粗字体意思是说:Web 服务器可以有其它超时配置,这些配置也可能会中断 PHP 执行。Apache 有一个 Timeout 指令,IIS 有一个 CGI 超时功能。 两者都默认为 300 秒。(nginx也有一个fastcgi超时配置,默认60秒)。

502解决方案

再php-fpm.conf中添加request_terminate_timeout = 600即可,如下:

#编辑php-fpm配置
vim /usr/local/php/etc/php-fpm.conf
#添加此配置,单位默认为秒,多少秒请根据情况自行设定
request_terminate_timeout = 600
#保存后重启
service php-fpm restart

504解决方案

再nginx配置中添加 fastcgi_connect_timeout 600; fastcgi_read_timeout 600; fastcgi_send_timeout 600; 即可,如下:文章来源地址https://www.toymoban.com/news/detail-711080.html

#编辑nginx某个站点的配置
vim /usr/local/nginx/conf/vhost/test.conf
#再location中添加以下配置,单位默认为秒,多少秒请根据情况自行设定,完整代码块如下:
location ~ \.php$ {
	fastcgi_pass   127.0.0.1:9000;
	fastcgi_index  index.php;
	fastcgi_param  SCRIPT_FILENAME $document_root/$fastcgi_script_name; 
	include        /usr/local/nginx/conf/fastcgi_params;
	fastcgi_connect_timeout 600; 
	fastcgi_read_timeout 600; 
	fastcgi_send_timeout 600; 
}

#保存后测试配置是否有问题,如果有问题,请修改好后再次尝试。
../../sbin/nginx -t
#确认配置正常,重启
service nginx restart

配置含义官方说明:

PHP-FPM:
request_terminate_timeout
The timeout for serving a single request after which the worker process will be killed. This option should be used when the 'max_execution_time' ini option does not stop script execution for some reason. A value of '0' means 'Off'. Available units: s(econds)(default), m(inutes), h(ours), or d(ays). Default value: 0.


Nginx:
fastcgi_connect_timeout 60s;
Defines a timeout for establishing a connection with a FastCGI server. It should be noted that this timeout cannot usually exceed 75 seconds.

fastcgi_read_timeout 60s;
Defines a timeout for reading a response from the FastCGI server. The timeout is set only between two successive read operations, not for the transmission of the whole response. If the FastCGI server does not transmit anything within this time, the connection is closed.

fastcgi_send_timeout 60s;
Sets a timeout for transmitting a request to the FastCGI server. The timeout is set only between two successive write operations, not for the transmission of the whole request. If the FastCGI server does not receive anything within this time, the connection is closed.

到了这里,关于PHP-FPM与Nginx通信报 502 Bad Gateway或504 Gateway Timeout终极解决方案(适用于PHP执行耗时任务情况下的报错)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 解决504 GATEWAY TIMEOUT Nginx网关超时

    最近遇到一个问题504 GATEWAY TIMEOUT的问题,在浏览器的NetWork里面看是这个效果。时间大概是60s。 目前后端技术架构主要是nginx和php-fpm,前端主要是vue框架打包发布。 于是首先想到了是nginx超时时间或者与php的超时时间设置的过段,然后配置nginx.conf设置了这些参数。 然后发现

    2024年02月02日
    浏览(48)
  • 【面试题28】什么是PHP-FPM?它与PHP和Nginx有什么关系

    本文已收录于PHP全栈系列专栏:PHP面试专区。 计划将全覆盖PHP开发领域所有的面试题, 对标资深工程师/架构师序列 ,欢迎大家提前关注锁定。 PHP-FPM(FastCGI Process Manager)是PHP的FastCGI进程管理器,它是PHP 5.3.3及更高版本的一部分。它通过为每个请求分配一个独立的进程来提

    2024年02月13日
    浏览(47)
  • PHP+Nginx经常出现502、504原因与解决方法

    很多新手刚开始做网站可能感觉不到502,504的问题,当等你网站到达了一定水平的时候,流量起来的时候,你会发现经常会遇到502、504类似的问题。 502 Bad Gateway:作为网关或者代理工作的服务器尝试执行请求时,从上游服务器接收到无效的响应。 将请求提交给网关如php-fpm执

    2024年02月15日
    浏览(34)
  • Nginx错误502 Bad Gateway

    使用Nginx配置的反向代理,浏览器访问的时候出现 “502 Bad Gateway” 错误,检查了一下后台error文件,发现有类似下面的错误 其中 “upstream sent too big header while reading response header from upstream” 说明可能是nginx代理的缓冲区不够,因此需要调整一下缓冲区的配置,主要包括下面几

    2024年02月19日
    浏览(37)
  • 在Linux上使用PHP-FPM与Nginx实现高效的HTTP处理

    当谈到高效的HTTP处理时,PHP-FPM(FastCGI进程管理器)与Nginx的结合是许多web开发者的首选。这种组合提供了出色的性能、可扩展性和稳定性,尤其适用于高流量的网站和应用程序。 1. 为什么选择PHP-FPM与Nginx? 性能优化 :PHP-FPM通过进程管理和缓存机制,显著提高了PHP脚本的执

    2024年01月17日
    浏览(35)
  • nginx反向代理502-Bad Gateway问题解决

    配置nginx反向代理时出现502 通过nginx -t检查配置以成功 通过nginx -s reload重新加载 通过cat /var/log/nginx/error.log查看错误日志发现错误信息,这里的错误信息是“connecting to upstream ”。这里怀疑是selinux拒绝nginx 转发 8080端口。 关闭selinux重新测试 关闭后重新测试正常,可以通过反向

    2024年01月19日
    浏览(62)
  • 【已解决】nginx 502 Bad Gateway 问题排查

    访问网站或请求接口时,出现: 日志一般放在/var/log/nginx下面。 跑流水线的话一般部署日志在控制台可以直接看到(我遇到的一次就是构建包下载下来大小为0kb,md5校验也不通过) 源码安装的nginx配置文件一般在 /usr/local/nginx/conf/nginx.conf/ 不是源码安装的一般在 /etc/nginx/ngi

    2024年02月15日
    浏览(51)
  • Linux系统下配置Nginx使部分URL使用多套自定义的PHP-FPM配置

    1. Tcp默认的9000端口通信: php-fpm配置:listen = 127.0.0.1 与nginx进程通信:fastcgi_pass 127.0.0.1:9000; 优点: 使用网络传输,可以跨服务器。 TCP通信有一些校验机制,具有更高的稳定性。 缺点: 性能略微比socket差。 2. Unix Socket(套接字)通信: php-fpm配置 :listen = /tmp/php-cgi.sock 与

    2024年02月08日
    浏览(43)
  • nginx反向代理502-Bad Gateway问题解决方法

    用nginx反向代理 localhost:80 域名到服务器 localhost:8080 端口服务时,访问出现502 bad gateway 原因分析: 1.查看8080端口服务启动 2.查看错误日志:error.log,以centos7.x为例: 192.168.10.202 - - [08/May/2023:20:53:43 +0800] \\\"GET /jenkinsx/ HTTP/1.1\\\" 502 3693 \\\"-\\\" \\\"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/53

    2024年02月06日
    浏览(90)
  • Nginx反向代理的一个算法API的接口调用超时:504,GateWay Timeout,怎么破?

    服务端由第三方部署了一个基于 darknet (一个较为轻型的完全基于C与CUDA的开源深度学习框架)的识别算法服务,通过 Flask 的 Web 服务对业务服务暴露 API 接口。作为测试,一开始是直接通过 python3 app.py 的命令行启动的服务,然后在 Nginx 处通过反向代理过来的。 可是在通过前

    2023年04月08日
    浏览(55)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包