Docker化Nginx与Laravel无法加载public静态资源求助
Laravel Docker Compose部署:Public目录静态资源404问题
我通过Docker Compose搭建了PHP Laravel应用,创建了包含Nginx在内的多个容器并完成配置,目前除public文件夹内的静态资源外,其他功能均正常运行。尝试过多种nginx.conf配置方案,但均未解决问题。本地部署环境下,.env文件中APP_URL=localhost,访问静态资源时返回404错误,恳请帮助。
当前配置文件
nginx.conf
events {} http { server { listen 80 default_server; root /var/www/public; index index.html index.php; add_header X-Frame-Options "SAMEORIGIN"; add_header X-Content-Type-Options "nosniff"; charset utf-8; location / { try_files $uri /index.php?$query_string; } location ~* \.(js|css|png|jpg|jpeg|gif|ico|html|json|svg)$ { try_files $uri =404; expires max; add_header Pragma public; add_header Cache-Control "public, must-revalidate, proxy-revalidate"; } location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } error_page 404 /index.php; location ~ \.php$ { fastcgi_pass php:9000; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.(?!well-known).* { deny all; } } }
docker-compose.yml
version: '3.8' services: nginx: container_name: nginx image: nginx:alpine ports: - "80:80" volumes: - ./nginx.conf:/etc/nginx/nginx.conf depends_on: - php php: build: context: . dockerfile: Dockerfile container_name: php image: php:8.2-fpm working_dir: /var/www volumes: - ./:/var/www - ./.env:/var/www/.env depends_on: db: condition: service_healthy db: platform: linux/x86_64 container_name: db image: mysql:8.0 restart: unless-stopped environment: - MYSQL_DATABASE='${DB_DATABASE}' - MYSQL_USER='${DB_USERNAME}' - MYSQL_PASSWORD='${DB_PASSWORD}' - MYSQL_ALLOW_EMPTY_PASSWORD=1 volumes: - dbdata:/var/lib/mysql ports: - "3306:3306" healthcheck: test: ["CMD", "mysqladmin" ,"ping", "-h", "localhost"] timeout: 120s retries: 60 node: platform: linux/x86_64 build: context: . dockerfile: Dockerfile.node container_name: node image: node:20 ports: - "3000:3000" working_dir: /var/www volumes: - ./:/var/www - /var/www/node_modules volumes: dbdata: driver: local
Dockerfile
# Set the base image for subsequent instructions FROM php:8.2-fpm USER root # Install dependencies RUN apt-get update && apt-get install -y \ build-essential \ libpng-dev \ libonig-dev \ libxml2-dev \ zip \ curl \ unzip \ git \ libzip-dev \ libfreetype6-dev \ libjpeg62-turbo-dev \ libpng-dev && \ docker-php-ext-install mysqli pdo_mysql mbstring exif pcntl bcmath gd zip # Clear cache RUN apt-get clean && rm -rf /var/lib/apt/lists/* # Install Composer COPY --from=composer:latest /usr/bin/composer /usr/bin/composer # Set working directory WORKDIR /var/www # Remove default server definition RUN rm -rf /var/www/html # Copy existing application directory contents COPY . /var/www # Copy existing application directory permissions COPY --chown=www-data:www-data . /var/www # Expose port 9000 and start php-fpm server EXPOSE 9000 ENTRYPOINT ["sh", "./entrypoint-php.sh"] CMD ["php-fpm"]
解决方案
核心问题:Nginx容器未挂载Laravel项目目录
你的Nginx容器仅挂载了nginx.conf,但没有挂载本地的Laravel项目目录到容器内的/var/www,导致Nginx无法访问/var/www/public下的静态资源,这是404的根本原因。
修复步骤:
修改docker-compose.yml的Nginx服务配置,添加项目目录挂载:
在nginx服务的volumes节点下新增一行:- ./:/var/www修改后的Nginx服务配置片段:
nginx: container_name: nginx image: nginx:alpine ports: - "80:80" volumes: - ./nginx.conf:/etc/nginx/nginx.conf - ./:/var/www # 新增项目目录挂载 depends_on: - php验证容器内静态资源路径:
重启容器后,进入Nginx容器检查静态资源是否存在:docker exec -it nginx ls /var/www/public确认能看到css、js、img等静态文件目录。
重启容器生效:
docker-compose down && docker-compose up -d
额外排查点:
- 检查Nginx日志确认404请求的实际路径:
docker logs nginx - 确保本地
public目录下的静态文件权限正确,容器内www-data用户能读取文件(Dockerfile中已设置--chown=www-data:www-data,一般无需额外调整)。
内容的提问来源于stack exchange,提问作者metakorshik
相关产品推荐
相关产品推荐

