在调试一个php脚本时遇到504超时错误,架构是nginx+PHP-FPM,这里备忘一下超时设置。
错误:
Fatal error: Maximum execution time of 30 seconds exceeded
nginx设置
nginx层面是通过fastcgi_read_timeout这个参数来决定的。PHP FastCGI Server(PHP-FPM)的响应时间限制。这里改为300秒,具体根据需求设定。
location ~ \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_read_timeout 300;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
php设置
php可以使用set_time_limit设置允许脚本运行的时间,单位为秒。如果超过了此设置,脚本返回一个致命的错误。默认值为30秒(设置0为不限制时间),或者是在php.ini的max_execution_time被定义的值,如果此值存在。
使用ini_set('max_execution_time', '300');设置执行时间,或修改php.ini
; Maximum execution time of each script, in seconds
; http://php.net/max-execution-time
; Note: This directive is hardcoded to 0 for the CLI SAPI
max_execution_time = 300
参考文档: