在 Linux 上运行 Laravel 时“找不到路由(404 / Route not found / 空白页)”是非常常见的问题,通常不是代码写错,而是环境或配置导致的。下面按最常见原因 → 排查方法给你列一遍。
index.phpLaravel 必须通过 public/index.php 入口访问。
❌ 错误访问方式:
http://ip/项目根目录
✅ 正确方式:
http://域名 或 http://ip/public
Nginx 示例(必须):
location / {
try_files $uri $uri/ /index.php?$query_string;
}
Apache 需开启:
AllowOverride All
mod_rewrite
php artisan route:clear
php artisan config:clear
php artisan cache:clear
如果用了:
php artisan route:cache
改路由后必须重新执行。
cp .env.example .env
php artisan key:generate
否则可能出现:
chown -R www-data:www-data /var/www/laravel
chmod -R 755 storage bootstrap/cache
不然后台报错但页面直接 404。
web.php → 浏览器访问api.php → 需 /api/xxx例如:
// routes/web.php
Route::get('/test', fn () => 'ok');
访问:
/test
Route::post('/login', ...)
用浏览器直接访问 → 404
php artisan route:list
✅ 能看到路由 → 代码没问题
❌ 看不到 → 路由文件没加载
server {
root /var/www/laravel/public;
index index.php;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include fastcgi_params;
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
}
}
请贴出:
php artisan route:list 输出我可以直接帮你指出是哪一步错了。