Laravel Homestead环境下除默认根路由外其他路由404问题求助
Hey there, let's work through why your custom Laravel routes (like /test or /user) are throwing 404 errors even though they show up when you run php artisan route:list. Here are the most common fixes to try:
1. Verify Nginx Site Configuration
Homestead uses Nginx, so the first thing to check is your site's Nginx config. SSH into your Homestead box (vagrant ssh) and navigate to the Nginx sites directory:
cd /etc/nginx/sites-available/
Open your site's config file (it should match your domain from Homestead.yaml, e.g., your-site.test). Make sure the location / block includes the correct try_files directive—this tells Nginx to pass all requests to Laravel's index.php for routing:
location / { try_files $uri $uri/ /index.php?$query_string; }
If this line is missing or incorrect, Nginx won't forward non-root requests to Laravel, resulting in 404s.
2. Restart Nginx
After updating the Nginx config, restart the service to apply changes:
sudo service nginx restart
3. Clear Laravel Route Cache
Sometimes route caching can cause new routes to not load properly, even if route:list shows them. Run this command in your project directory to clear the cache:
php artisan route:clear
Note: If you ever used php artisan route:cache in the past, you must clear the cache to use dynamic routes (like your closure-based routes).
4. Confirm Site Root Points to /public
Double-check that your Homestead site config (both in Homestead.yaml and Nginx) points to Laravel's public directory, not the project root. In Homestead.yaml, your site entry should look like:
sites: - map: your-site.test to: /home/vagrant/code/your-project/public
If you made changes to Homestead.yaml, reload the box with provisioning to apply them:
vagrant reload --provision
5. Check Local Hosts File
Ensure your local machine's hosts file maps your Homestead domain to the box's IP (usually 192.168.10.10). On Windows, this file is at C:\Windows\System32\drivers\etc\hosts; on macOS/Linux, it's /etc/hosts. Add a line like:
192.168.10.10 your-site.test
Walk through these steps one by one—most likely the issue is with Nginx's try_files directive or incorrect site root configuration.
内容的提问来源于stack exchange,提问作者Luis Martin

