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

配置默认文件后Laravel网站显示404错误,求解决建议

Laravel网站持续显示404错误排查求助

我搞不懂为什么我的Laravel网站一直显示404错误,已经确认了各项配置,包括站点入口设置root /home/web/my_project/public;,但还是找不到问题。欢迎各位提供建议或修改方案,谢谢。

以下是/etc/nginx/sites-enabled/default文件内容:

server {  
    listen 80 default_server;
    listen [::]:80 default_server;

    root /home/web/my_project/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

    server_name _;

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

    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 unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /\.(?!well-known).* {
        deny all;
    }
}

排查建议

  • 检查Laravel目录权限:确保storage和bootstrap/cache目录对Nginx运行用户(通常是www-data)有读写权限,执行命令:
    sudo chown -R www-data:www-data /home/web/my_project/storage /home/web/my_project/bootstrap/cache
    sudo chmod -R 775 /home/web/my_project/storage /home/web/my_project/bootstrap/cache
    
  • 确认PHP-FPM状态:执行systemctl status php8.1-fpm检查服务是否正常运行,未运行则启动:systemctl start php8.1-fpm
  • 验证Nginx配置:执行nginx -t检测语法错误,修正后重启Nginx:systemctl restart nginx
  • 检查Laravel路由:确认访问的路由存在于routes/web.php中,若开启了路由缓存,执行php artisan route:clear清除缓存
  • 查看Nginx错误日志:打开/var/log/nginx/error.log查看最近的错误信息,定位具体问题
  • 确认项目路径有效性:检查/home/web/my_project/public目录是否存在,且包含index.php文件

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 02:57:55