Nginx + php-fpm 执行 PHP 脚本超时 报错 502 Bad Gateway + 504 Gateway Time-out 的解决办法
2023-09-11 14:17:03 时间
上周写好的发送邮件的计划任务只发送了一部分,检查计划任务日志,发现 502 Bad Gateway 的错误(已经在脚本中设置了 set_time_limit(0))。
后来在网上查找资料,可以通过以下设置来解决脚本超时导致 502 Bad Gateway 的问题(Nginx + php-fpm,CentOs 系统):
① Nginx 设置
修改 nginx.conf:
fastcgi_connect_timeout 300; fastcgi_send_timeout 300; fastcgi_read_timeout 300;
重启 nginx。
② php-fpm 设置
修改 php-fpm.conf:
request_terminate_timeout = 0s
重启 php-fpm:
kill -USR2 `cat /usr/local/php/var/run/php-fpm.pid`
③ 在 php 脚本中设置
ini_set("max_execution_time", 0);
或者在 php.ini 中把 max_execution_time 设为一个比较大的值。
502 问题得到解决。
然而脚本在执行一段时间之后,又报了 504 Gateway Time-out 的错误,再适当把 nginx.conf 的 fastcgi_connect_timeout、fastcgi_send_timeout、fastcgi_read_timeout 等数值调大:
fastcgi_connect_timeout 3000; fastcgi_send_timeout 3000; fastcgi_read_timeout 3000;
35 分钟发 600+ 邮件(PHPMailer)不报 504 错误。
另外最好设置一张数据库表来记录哪些用户的邮件已经发送成功,这样在脚本终止之后重新执行不会重复发送邮件。测试的时候可以用 126 邮箱(同一 QQ 邮箱连续接收 60 封后就不在收件了)。
相关文章
- php中通过Hashids将整数转化为唯一字符串
- PHP如何自动识别第三方Restful API的内容,自动渲染成 json、xml、html、serialize、csv、php等数据
- Windows下图文详解PHP三种运行方式(php_mod、cgi、fastcgi)
- 配置php.ini实现PHP文件上传功能
- PHP如何获取二个日期的相差天数?
- PHP Document 注释标记及规范 && PHP命名规范
- php:PHP解析xml的4种方法
- php:如何使用PHP排序, key为字母+数字的数组(多维数组)
- dns未设置 PHP Warning: file_get_contents():php_network_getaddresses: getaddrinfo failed:
- php分享十一:PHP实现异步调用方法研究
- [获取行数]php读取大文件提供性能的方法,PHP的stream_get_line函数读取大文件获取文件的行数的方...
- php:在linux上用sudo提升权限(centos 8 / PHP 7.4.2)
- php:用数组实现多语言(PHP 7.4.2)
- php类库PHP QR Code 二维码
- 在 Windows 下为 PHP 5.4 安装 PEAR、PHPUnit 及 phpDoc2
- [php-src] 窥探Php内核中的数组与面向对象
- [php-src] 窥探Php内核中的变量
- atitit.窗体静听esc退出本窗体java swing c# .net php
- PHP面试题:你所知道的php数组相关的函数?
- PHP获取文件后缀名
- PHP中冒号、endif、endwhile、endfor使用介绍
- Centos7 安装PHP7版本及php-fpm,redis ,php-redis,hiredis,swoole 扩展安装
- php文字转语音
- 统计php程序运行时间及设置PHP最长运行时间