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

Docker Compose下Nginx无法通过PHP容器渲染Laravel站点的404问题

问题:Nginx容器访问返回404,需通过PHP容器渲染Laravel站点

我运行着两个容器:一个是运行PHP与Laravel站点的容器,工作状态正常;另一个是Nginx容器,目前访问时返回404错误,希望通过PHP容器来渲染这个Laravel站点。

目录结构

- app
- bootstrap
- config
- database
- nginx
    - default.conf
    - DockerFile
- php
- public
- resources
- routes
- storage
- vendor
- docker-compose.yml
- docker-production.yml
- DockerFile

各配置文件内容

根目录DockerFile

FROM php:7.4

RUN apt-get update -y && apt-get install -y openssl zip unzip git cron
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
RUN docker-php-ext-install pdo pdo_mysql

WORKDIR /app
COPY . .

RUN composer install

ADD config/laravel_cron /etc/cron.d/cron
RUN chmod 0644 /etc/cron.d/cron

RUN touch /var/log/cron.log
RUN chmod 0777 /var/log/cron.log

RUN crontab /etc/cron.d/cron
RUN service cron start

RUN echo "Europe/London" > /etc/timezone
RUN dpkg-reconfigure -f noninteractive tzdata

EXPOSE 8000

docker-production.yml

version: '3.7'
services:
  horse-racing-api:
    container_name: horse_racing_api
    restart: unless-stopped
    build:
      context: .
      dockerfile: DockerFile
    stdin_open: true
    tty: true
    working_dir: /app
    volumes:
      - ./:/app
  web-server:
    container_name: web_server
    ports:
      - 80:80
    build:
      context: nginx
      dockerfile: DockerFile
    depends_on:
      - horse-racing-api
    links:
      - horse-racing-api
    volumes:
      - ./:/app
volumes:
  app:

nginx/DockerFile

FROM nginx:latest
COPY ./default.conf /etc/nginx/conf.d/default.conf

nginx/default.conf

server {
    listen 80;
    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/public;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass horse-racing-api:8000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}

说实话,这些配置都是我从网上各处拼凑起来的 :/


解决404错误的方案

  • 修正Nginx配置中的根目录路径
    当前Nginx配置里的root /var/www/public;有误,因为你把项目目录挂载到了Nginx容器的/app目录,所以根目录应该改成/app/public:
server {
    listen 80;
    index index.php index.html;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    # 修正根目录路径
    root /app/public;
    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass horse-racing-api:8000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    location / {
        try_files $uri $uri/ /index.php?$query_string;
        gzip_static on;
    }
}
  • 确保PHP容器运行FastCGI服务
    你当前用的php:7.4镜像默认不会启动php-fpm(FastCGI服务),需要修改根目录DockerFile,换成带fpm的镜像:
FROM php:7.4-fpm  # 直接使用内置php-fpm的镜像

RUN apt-get update -y && apt-get install -y openssl zip unzip git cron
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
RUN apt-get clean && rm -rf /var/lib/apt/lists/*
RUN docker-php-ext-install pdo pdo_mysql

WORKDIR /app
COPY . .

RUN composer install

ADD config/laravel_cron /etc/cron.d/cron
RUN chmod 0644 /etc/cron.d/cron

RUN touch /var/log/cron.log
RUN chmod 0777 /var/log/cron.log

RUN crontab /etc/cron.d/cron
RUN service cron start

RUN echo "Europe/London" > /etc/timezone
RUN dpkg-reconfigure -f noninteractive tzdata

# php-fpm默认监听9000端口,修改暴露端口
EXPOSE 9000

同时要修改Nginx配置里的fastcgi_pass端口,匹配php-fpm的默认端口:

location ~ \.php$ {
    try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    fastcgi_pass horse-racing-api:9000;  # 修改为9000端口
    fastcgi_index index.php;
    include fastcgi_params;
    fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
    fastcgi_param PATH_INFO $fastcgi_path_info;
}
  • 重启容器应用配置修改
    执行以下命令重新构建并启动容器:
docker-compose -f docker-production.yml up --build -d
  • 额外检查项
    • 确认Laravel的storage和bootstrap/cache目录权限正确,进入PHP容器执行:
      chmod -R 775 storage bootstrap/cache
      chown -R www-data:www-data storage bootstrap/cache
      
    • 查看Nginx错误日志排查具体问题:
      docker exec web_server tail -f /var/log/nginx/error.log
      

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.23 08:36:11