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

Docker部署Laravel生产环境访问指定URL出现404 NotFound问题求助

Troubleshooting Laravel 404 Error in Docker Production Deployment

Let's walk through the most likely causes and fixes for your 404 issue—since your local setup works, the problem is almost certainly a configuration mismatch or accessibility gap in production:

1. Fix Nginx Root Path Mismatch

Looking at your docker-compose.yml, you’re mounting your local ./server directory to /var/www in both PHP and Nginx containers. But your default.conf sets the root to /var/www/laravel/public.

Double-check your project structure:

  • If your Laravel code lives directly inside ./server (not nested under ./server/laravel), the correct root path in Nginx should be /var/www/public, not /var/www/laravel/public. This path mismatch is a common cause of 404s, as Nginx is looking for files in the wrong directory.

Update your default.conf with the corrected path:

server {
  listen 81;
  index index.php index.html;
  root /var/www/public; # Updated to match your volume mount
  location / {
    root /var/www/public; # Updated here too
    index index.html index.php;
    try_files $uri $uri/ /index.php?$query_string; # Note the added question mark (see next point)
  }
}

2. Correct the try_files Directive

Your current try_files line is missing a critical question mark before $query_string:

try_files $uri $uri/ /index.php$query_string;

Without this question mark, query parameters won’t be passed correctly to Laravel’s index.php, breaking route resolution. Fix it with:

try_files $uri $uri/ /index.php?$query_string;

Restart Nginx to apply changes:

docker-compose restart nginx

3. Confirm Domain & Port Access

Your Nginx container is mapped to host port 81. When accessing your domain:

  • Are you including the port in your URL? e.g., http://your-domain.com:81
  • If you want to use the default HTTP port (80), update your docker-compose.yml Nginx ports section to:
    ports:
      - 80:81
    
  • Ensure your domain’s DNS records point to your server’s public IP, and that port 81 (or 80 if you changed it) is open in your server’s firewall.

4. Clear Laravel’s Production Caches

In production, Laravel uses route and config caching for performance. If you updated APP_URL or modified routes after caching, this can lead to 404s. Enter your PHP container to clear caches:

docker exec -it php bash

Then run:

php artisan route:clear
php artisan config:clear

5. Verify File Permissions

Laravel needs write access to the storage and bootstrap/cache directories. Inside the PHP container, run:

chown -R www-data:www-data /var/www/storage /var/www/bootstrap/cache
chmod -R 775 /var/www/storage /var/www/bootstrap/cache

If the issue persists, check Nginx’s error logs for more specific clues:

docker logs nginx

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.13 08:31:27