PHP接口调用超过5s的请求直接返回如下信息:

  1. Response
  2. <html> <head><title>502 Bad Gateway</title></head> <body bgcolor="white"> <center><h1>502 Bad Gateway</h1></center> <hr><center>nginx</center> </body> </html> <!-- a padding to disable MSIE and Chrome friendly error page --> <!-- a padding to disable MSIE and Chrome friendly error page --> <!-- a padding to disable MSIE and Chrome friendly error page --> <!-- a padding to disable MSIE and Chrome friendly error page --> <!-- a padding to disable MSIE and Chrome friendly error page --> <!-- a padding to disable MSIE and Chrome friendly error page -->

查了Nginx的错误日志如下:

  1. 2019/01/22 16:40:45 [error] 14291#0: *296842388 recv() failed (104: Connection reset by peer) while reading response header from upstream, client: 113.96.219.247, server: , request: "POST XXX HTTP/1.1", upstream: "fastcgi://unix:/dev/shm/php7-cgi.sock:", ...

原因是由于php_fpm的执行时间受request_terminate_timeout这个参数的配置影响,这个参数我配置的是5s,那么5s后,这个php-fpm处理进程会被直接杀掉,导致返回502

  • 官方备注:
    request_terminate_timeout mixed
    设置单个请求的超时中止时间。该选项可能会对 php.ini 设置中的 ‘max_execution_time’ 因为某些特殊原因没有中止运行的脚本有用。设置为 ‘0’ 表示 ‘Off’。可用单位:s(秒),m(分),h(小时)或者 d(天)。默认单位:s(秒)。默认值:0(关闭)。