php执行脚本时间长会导致脚本超时,报错504 Gateway Time-out,设置nginx fastcgi_read_timeout 时间和php脚本request_terminate_timeout 时间解决脚本超错误。
一、nginx配置
vim nginx.conf
#增加 fastcgi_read_timeout 300;
location ~ .php$ {
root html;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param script_FILENAME /usr/local/nginx/html$fastcgi_script_name;
include fastcgi_params;
fastcgi_read_timeout 300;
}fastcgi更多设置:
fastcgi_connect_timeout 300; #连接(很快) fastcgi_send_timeout 300; #发送(很快) fastcgi_read_timeout 300; #读取(php脚本运行并返回数据)
反向代理到php方式的超时设置:
#增加 proxy_send_timeout 600;
location / {
proxy_pass http://127.0.0.1/test.php
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_send_timeout 600;
}反向代理到php方式更多的超时设置:
proxy_connect_timeout 600; \跟后端服务器连接的超时时间,发起握手等候响应超时时间 proxy_read_timeout 600;\连接成功 _等候后端服务器的响应时间_其实已经进入后端的排队中等候处理 proxy_send_timeout 600; \后端服务器回传时间_就是在规定时间内后端服务器必须传完所有的数据。设置代理服务器转发请求的超时时间,同样指完成两次握手后的时间,如果超过这个时间代理服务器没有数据转发到后端服务器,nginx将关闭连接。
二、php配置
vim www.conf #配置 ;request_terminate_timeout = 30 request_terminate_timeout = 300
重启nginx,重启php-fpm
php代码超时设置:
ini_set("max_execution_time", "120"); //ini_set配置方式
set_time_limit(120);
set_time_limit(0); //不超时


