You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Lando+Laravel环境Nginx 504超时:自定义配置无效求助

解决Lando+Nginx环境下Laravel长时间脚本的504 Gateway Time-out问题

核心问题分析

你的测试脚本需要运行250秒,但Nginx默认的FastCGI超时时间远短于这个值,且Lando的Laravel Recipe自带的VHost配置可能覆盖了你自定义的超时设置,同时PHP-FPM的超时限制也可能没同步调整。


分步解决方案

1. 全局配置Nginx超时(避免被VHost覆盖)

修改你的nginx.conf,将超时参数移至http块全局,确保所有VHost都继承该配置:

http {
    # 其他原有配置...
    
    # 全局设置FastCGI和代理超时
    fastcgi_read_timeout 600;
    proxy_read_timeout 600;
    fastcgi_send_timeout 600;
    
    # 保留原有include语句
    include  "/opt/bitnami/nginx/conf/vhosts/*.conf";
    
    # 移除你自定义的server块(避免和Lando生成的VHost冲突)
}

2. 同步调整PHP-FPM超时限制

在.lando.yml中添加PHP服务的自定义配置,确保PHP-FPM允许长时间运行:

name: test
keys: false
recipe: laravel
config:
  php: "7.4"
  via: nginx:1.18
  composer_version: 2.0.8
  database: mariadb:10.4.17
  webroot: ./public
  cache: redis
  server: nginx.conf

proxy:
  appserver_nginx:
    - admin.test
    - admin.test.lndo.site
services:
  appserver_nginx:
    run_as_root:
      - ln -snf /usr/share/zoneinfo/America/Denver /etc/localtime
      - echo "UTC" > /etc/timezone
  db_gui:
    type: phpmyadmin
  # 添加PHP服务的自定义配置
  php:
    config:
      php: php.ini
      fpm: php-fpm.conf

然后在项目根目录创建php.ini:

max_execution_time = 600
max_input_time = 600
memory_limit = 256M

创建php-fpm.conf:

request_terminate_timeout = 600s
request_slowlog_timeout = 600s

3. 优化脚本输出缓冲

修改测试脚本,添加输出缓冲控制,确保浏览器实时接收输出,避免因长时间无响应触发超时:

Route::get('/test', function(){
    set_time_limit(600);
    // 开启隐式刷新,关闭输出缓冲
    ob_implicit_flush(true);
    ob_end_flush();

    for($i=0; $i<250; $i++){
        echo $i . '<br>';
        // 强制刷新输出
        flush();
        sleep(1);
    }
});

4. 验证配置并重建环境

  • 进入Nginx容器验证配置:lando ssh -s appserver_nginx,执行nginx -t确认配置无语法错误
  • 检查PHP配置:lando php -i | grep max_execution_time,确认值为600
  • 执行lando rebuild重新构建环境

内容的提问来源于stack exchange,提问作者habib

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 10:41:59