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

CapRover部署Laravel应用出现Nginx 502错误求助

解决CapRover部署Laravel出现NGINX 502错误的问题

你的核心问题是使用了纯PHP-FPM镜像,但未搭配Web服务器。joseluisq/php-fpm:8.2仅提供PHP-FPM服务(监听9000端口),而CapRover的反向代理默认需要容器提供监听80端口的HTTP服务,导致反向代理无法与PHP-FPM通信,返回502错误。

以下是具体修复方案:

1. 修改Dockerfile,使用包含NGINX+PHP-FPM的镜像

替换基础镜像为集成Web服务器的版本,配置NGINX指向Laravel的public目录(Laravel入口文件所在位置),同时修复不安全的777权限:

FROM richarvey/nginx-php-fpm:8.2
WORKDIR /var/www/html
COPY . /var/www/html

# 给Laravel必要目录设置正确权限(避免777的安全风险)
RUN chown -R www-data:www-data /var/www/html/storage /var/www/html/bootstrap/cache
RUN chmod -R 755 /var/www/html/storage /var/www/html/bootstrap/cache

# 配置NGINX,让请求正确路由到Laravel入口
RUN echo "server {
    listen 80;
    root /var/www/html/public;
    index index.php index.html index.htm;

    location / {
        try_files \$uri \$uri/ /index.php?\$query_string;
    }

    location ~ \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME \$document_root\$fastcgi_script_name;
        include fastcgi_params;
    }
}" > /etc/nginx/sites-available/default

EXPOSE 80

2. 额外排查步骤

  • 查看CapRover应用的容器日志:在CapRover面板进入对应应用,查看"Logs"标签,确认NGINX或PHP-FPM是否有启动报错
  • 确认.env文件中APP_URL配置正确,且已设置APP_KEY(Laravel必须有APP_KEY才能正常运行)
  • 若部署前未生成配置缓存,可在Dockerfile末尾添加命令:RUN php artisan config:cache && php artisan route:cache

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 11:27:06