nginx返回504的三种排查思路

首页 linux nginx返回504的三种排查思路

nginx返回504一般代表gateway timeout,也就是接口请求超时。

相关参数

NGINX

涉及到请求超时的参数,在nginx方面有

proxy_connect_timeout :后端服务器连接的超时时间_发起握手等候响应超时时间

proxy_read_timeout:它决定了nginx会等待多长时间来获得请求的响应。

proxy_send_timeout :后端服务器数据回传时间_就是在规定时间之内后端服务器必须传完所有的数据,也就是php请求接口必须在该时间范围内返回,如果php请求接口超过了该时间,也就会报504了。

修改了上面的参数还不行,还的修改fastcgi参数

fastcgi_connect_timeout 75; 链接

fastcgi_read_timeout 600; 读取

fastcgi_send_timeout 600; 发请求

这两个选项.
fastcgi_read_timeout是指fastcgi进程向nginx进程发送response的整个过程的超时时间
fastcgi_send_timeout是指nginx进程向fastcgi进程发送request的整个过程的超时时间

线上配置

    proxy_connect_timeout 15s;
    proxy_read_timeout 24s;
    proxy_send_timeout 10s;
    fastcgi_connect_timeout 3;
    fastcgi_send_timeout 20;
    fastcgi_read_timeout 20;

如果nginx请求超时,则会写到access_log中。看到请求时间是超过了10s,此时就会报504了。

remote_addr=10.189.241.23 remote_user=- [02/Jul/2021:10:33:58 +0800] logid=16251932287582242 request="POST /sdsapi/commit/mq?_mq_transid=group-sgs-send-cx$sdssgs$14$315$432522942&_retry_cnt=0 HTTP/1.1" status=504 body_bytes_sent=183 http_referer="-" http_cookie="-" http_user_agent="Go-http-client/1.1"http_clientip=- server_addr=10.189.240.166 server_port=8107 upstream_addr=unix:/home/work/odp_sds/var/php-cgi.sock host=10.189.240.166 "-" product=odp subsys=newapp spanid=- force_sampling=- msec=1625193238.796 upstream_header_time=10.000 upstream_response_time=10.000 request_time=10.000

PHP

在php方面也会有请求超时相关参数

  • 打开php.ini文件,找到 max_execution_time,将后面的数字改成你想要的时间,单位是秒。

  • 使用PHP的ini_set()函数设置,设置方法:
    ini_set("max_execution_time",1800);设置语句必须是php文件的第一行。

  • 使用PHP的set_time_limit()函数设置,设置方法:set_time_limit(1800);设置语句必须是php文件的第一行。

第二种和第三种用的比较少,线上配置,也就是php接口最长执行30s,超过30s之后,php将报超时信息

可以看出php出现了fatal错误,报错日志在php-error.log日志中:

PHP Fatal error:  log_id[638330865691284946] Maximum execution time of 30 seconds exceeded in /home/work/odp_sds/vendor/sftcwl/rpc/src/Protocol/Http.php on line 356"

排查思路

造成504,请求超时的原因无非下面几种

  • mysql,redis请求超时

  • 第三方接口请求超时

  • 代码有死循环或者大递归

针对前俩种问题,都归结于rpc问题,所有直接查询rpc.log.wf日志即可

WARNING: 07-02 10:33:55 sf-rpc [/home/work/odp_sds/vendor/sftcwl/log/src/RpcLog.php:35][module=sdsapi pid=20773 caller_uri=/sdsapi/commit/mq user_ip=10.189.241.23 prot=Http retry=0/1 remote_addr=gis-int.int.sfdc.com.cn:1080 cost=7001 talk=0 connect=0 read=0 err_no=28 err_info=Operation+timed+out+after+7001+milliseconds+with+0+bytes+received optime=1625193235.8059 logid=16251932287582242 SFID=0.2 msg=sf+rpc+http+request+failed+server[{"host":"gis-int.int.sfdc.com.cn:1080"}] from=/home/work/odp_sds/vendor/sftcwl/rpc/src/Protocol/Http.php:244 caller=\Sftcwl\Rpc\Protocol\Http url=/rdsaoi/api/nearestAoi all_blacklisted=0 stat_path=0 service=http/gisroute idc=gz method=POST uri=/rdsaoi/api/nearestAoi http_status=0 current_id=0FC0800153A763268570F69D872A49EC]

可以看到其中的关键日志

Operation+timed+out+after+7001+milliseconds

对于第三种情况,属于是业务代码代码,我们需要查看具体的业务代码查看问题原因


 
59    2024-08-06 17:38:42