温馨提示×

linux laravel为何超时

小樊
48
2026-10-27 19:39:36
栏目: 智能运维

在 Linux + Laravel 环境下出现“超时(Timeout)”,通常不是 Laravel 本身的问题,而是多层机制叠加导致的。下面按最常见原因 → 排查方式 → 解决方案给你系统梳理。


一、最常见超时原因分类

1️⃣ PHP / Web 服务器超时(最常见)

① max_execution_time

PHP 脚本最大执行时间(默认 30s)

位置

; php.ini
max_execution_time = 30

检查

php -i | grep max_execution_time

解决

max_execution_time = 300

或运行时:

set_time_limit(300);

② Nginx / Apache 超时

Nginx
fastcgi_read_timeout 300;
proxy_read_timeout 300;
Apache
Timeout 300

2️⃣ Laravel 请求超时(HTTP Client / API)

如果你用:

Http::timeout(5)->get(...)

对方响应慢就会超时。

解决

Http::timeout(30)->get(...)

3️⃣ Laravel Queue / Command 超时

① php artisan 命令超时

php artisan xxx

可能原因:

  • 死循环
  • 数据库锁
  • 外部 API 卡死

排查

php artisan xxx --verbose

② Queue Worker 超时

// config/queue.php
'timeout' => 60,

解决

'timeout' => 300,

或:

php artisan queue:work --timeout=300

4️⃣ 数据库超时(非常常见)

① 慢查询

SHOW PROCESSLIST;

② 缺少索引

Schema::table('orders', function ($table) {
    $table->index('user_id');
});

③ MySQL 连接超时

wait_timeout = 28800

5️⃣ Linux 系统层限制

① 文件描述符限制

ulimit -n

② OOM(内存不足)

dmesg | grep oom

6️⃣ 网络问题(服务器访问外网慢)

curl -I https://api.xxx.com
ping api.xxx.com

二、快速定位方法(强烈推荐)

✅ 看错误日志

tail -f storage/logs/laravel.log

✅ Nginx 错误

tail -f /var/log/nginx/error.log

✅ PHP-FPM

tail -f /var/log/php-fpm/error.log

三、典型场景速查表

场景 原因 解决
页面 502 PHP 超时 调大 timeout
API 调用失败 Http timeout 增加超时
队列卡死 worker timeout --timeout
后台任务慢 脚本超时 set_time_limit
偶发超时 DB 慢 加索引

四、如果你愿意

你可以直接告诉我:

  1. 具体在哪一步超时(页面 / API / artisan / queue)
  2. 报错原文
  3. Linux 发行版 + PHP 版本

我可以给你精确配置方案,不用瞎猜。

0 踩