在 Linux + Laravel 环境下出现 404,通常不是代码问题,而是服务器/路由配置问题。下面按最常见原因给你排查清单(从概率高到低):
try_files如果你用的是 Nginx,站点配置里一定要有:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
否则访问 /user/1 会直接 404。
修改后执行:
nginx -t
systemctl reload nginx
a2enmod rewrite
systemctl restart apache2
并且 public/.htaccess 必须存在(Laravel 自带)。
❌ 错误方式:
http://ip/laravel-project/
✅ 正确方式(入口是 public):
http://ip/laravel-project/public/
或
绑定域名到 public 目录
推荐做法:
Nginx root 指向:
root /var/www/laravel/public;
php artisan route:list
php artisan route:clear
php artisan config:clear
php artisan cache:clear
.env:
APP_URL=http://你的域名
改完:
php artisan config:clear
chown -R www-data:www-data storage bootstrap/cache
chmod -R 775 storage bootstrap/cache
.env
APP_DEBUG=false
APP_ENV=production
Linux 上 Laravel 404,90% 是 Nginx 没配
try_files,或访问的不是public目录
如果你愿意,可以把下面信息发我,我直接帮你改:
php artisan route:list 输出我可以直接给你一份可用配置。