在 Linux + Laravel 环境下出现“超时(Timeout)”,通常不是 Laravel 本身的问题,而是多层机制叠加导致的。下面按最常见原因 → 排查方式 → 解决方案给你系统梳理。
max_execution_timePHP 脚本最大执行时间(默认 30s)
位置
; php.ini
max_execution_time = 30
检查
php -i | grep max_execution_time
解决
max_execution_time = 300
或运行时:
set_time_limit(300);
fastcgi_read_timeout 300;
proxy_read_timeout 300;
Timeout 300
如果你用:
Http::timeout(5)->get(...)
对方响应慢就会超时。
解决
Http::timeout(30)->get(...)
php artisan 命令超时php artisan xxx
可能原因:
排查
php artisan xxx --verbose
// config/queue.php
'timeout' => 60,
解决
'timeout' => 300,
或:
php artisan queue:work --timeout=300
SHOW PROCESSLIST;
Schema::table('orders', function ($table) {
$table->index('user_id');
});
wait_timeout = 28800
ulimit -n
dmesg | grep oom
curl -I https://api.xxx.com
ping api.xxx.com
tail -f storage/logs/laravel.log
tail -f /var/log/nginx/error.log
tail -f /var/log/php-fpm/error.log
| 场景 | 原因 | 解决 |
|---|---|---|
| 页面 502 | PHP 超时 | 调大 timeout |
| API 调用失败 | Http timeout | 增加超时 |
| 队列卡死 | worker timeout | --timeout |
| 后台任务慢 | 脚本超时 | set_time_limit |
| 偶发超时 | DB 慢 | 加索引 |
你可以直接告诉我:
我可以给你精确配置方案,不用瞎猜。