java nginx 502_Nginx 中 502 和 504 错误详解

在使用Nginx时,经常会碰到 502 Bad Gateway 和 504 Gateway Time-out 错误,下面以 Nginx+PHP-FPM 来分析下这两种常见错误的原因和解决方案。

1. 502 Bad Gateway 错误

在 php.ini 和 php-fpm.conf 中分别有这样两个配置项:max_execution_time 和 request_terminate_timeout。

这两项都是用来配置一个 PHP 脚本的最大执行时间的。当超过这个时间时,PHP-FPM不只会终止脚本的执行,还会终止执行脚本的Worker进程。所以Nginx会发现与自己通信的连接断掉了,就会返回给客户端502错误。

以PHP-FPM的 request_terminate_timeout=30 秒时为例,报 502 Bad Gateway 错误的具体信息如下:

1)Nginx错误访问日志:

2013/09/19 01:09:00 [error] 27600#0: *78887 recv() failed (104: Connection reset by peer) while reading response header from upstream,

client: 192.168.1.101, server: test.com, request: "POST /index.php HTTP/1.1", upstream: "fastcgi://unix:/dev/shm/php-fcgi.sock:",

host: "test.com", referrer: "http://test.com/index.php"

2)PHP-FPM报错日志:

WARNING: child 25708 exited on signal 15 (SIGTERM) after 21008.883410 seconds from start

所以只需将这两项的值调大一些就可以让PHP脚本不会因为执行时间长而被终止了。request_terminate_timeout 可以覆盖 max_execution_time,所以如果不想改全局的php.ini,那只改PHP-FPM的配置就可以了。

此外要注意的是Nginx的upstream模块中的 max_fail 和 fail_timeout 两项。有时Nginx与上游服务器(如Tomcat、FastCGI)的通信只是偶然断掉了,但 max_fail 如果设置的比较小的话,那么在接下来的 fail_timeout时间内,Nginx都会认为上游服务器挂掉了,都会返回502错误。

所以可以将 max_fail 调大一些,将 fail_timeout 调小一些。

2. 504 Gateway Time-out 错误

PHP-FPM设置的脚本最大执行时间已经够长了,但执行耗时PHP脚本时,发现Nginx报错从502变为504了。这是为什么呢?

因为我们修改的只是PHP的配置,Nginx中也有关于与上游服务器通信超时时间的配置factcgi_connect/read/send_timeout。

以Nginx超时时间为90秒,PHP-FPM超时时间为300秒为例,报504 Gateway Timeout错误时的Nginx错误访问日志如下:

2013/09/19 00:55:51 [error] 27600#0: *78877 upstream timed out (110: Connection timed out) while reading response header from upstream,

client: 192.168.1.101, server: test.com, request: "POST /index.php HTTP/1.1", upstream: "fastcgi://unix:/dev/shm/php-fcgi.sock:",

host: "test.com", referrer: "http://test.com/index.php"

调高这三项的值(主要是read和send两项,默认不配置的话Nginx会将超时时间设为60秒)之后,504错误也解决了。

而且这三项配置可以配置在http、server级别,也可以配置在location级别。担心影响其他应用的话,就配置在自己应用的location中吧。

要注意的是 factcgi_connect/read/send_timeout 是对 FastCGI 生效的,而 proxy_connect/read/send_timeout 是对 proxy_pass 生效的。

配置举例:

location ~ \.php$ {

root /home/cdai/test.com;

include fastcgi_params;

fastcgi_connect_timeout 180;

fastcgi_read_timeout 600;

fastcgi_send_timeout 600;

fastcgi_pass unix:/dev/shm/php-fcgi.sock;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /home/cdai/test.com$fastcgi_script_name;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值