新Laravel项目/login路由返回403 Forbidden错误求助
Hey there, let's work through this frustrating 403 issue with your new Laravel project's login route. Since your /register route works perfectly and an older Laravel project (with the same /login setup, no .htaccess included) runs fine on Homestead, here are some focused steps to debug:
1. Verify Homestead's Nginx Site Configuration
Since you're seeing an nginx/1.13.3 403 response, the problem likely lies in your Nginx setup for the new project:
- SSH into your Homestead box with
homestead ssh - Navigate to the Nginx sites directory:
cd /etc/nginx/sites-available/ - Open your project's config file (e.g.,
your-project.test) and check two key things:- Ensure the
rootdirective points to your project's public folder:root /home/vagrant/code/your-project/public;
If it's pointing to the project root instead, Nginx will try to serve files directly without passing requests to Laravel'sindex.php, causing 403s. - Confirm the
try_filesline is correctly set to route all requests through Laravel:try_files $uri $uri/ /index.php?$query_string;
- Ensure the
2. Fix Directory Permissions
New Laravel projects often run into permission issues on Homestead, which can trigger 403 errors. Run these commands inside your project directory (after SSHing into Homestead):
sudo chown -R www-data:www-data /home/vagrant/code/your-project sudo chmod -R 755 /home/vagrant/code/your-project/storage /home/vagrant/code/your-project/bootstrap/cache
Your older project probably had these permissions set correctly already, which is why it works without issues.
3. Remove the Custom .htaccess File
Homestead uses Nginx, not Apache—so .htaccess files don't do anything here. The file you added might be causing unintended conflicts. Delete it, then restart Nginx to apply changes:
sudo service nginx restart
4. Clear Laravel Route Cache
Occasionally, cached routes can lead to unexpected routing behavior. Clear the cache with:
php artisan route:clear
Then try accessing /login again.
5. Check Nginx Error Logs for Exact Details
If none of the above works, the Nginx error log will tell you exactly what's causing the 403. Run this command and keep it running while you try to access /login:
sudo tail -f /var/log/nginx/error.log
Look for lines that appear when you hit the login route—they'll specify if it's a permission denied error, incorrect file path, or another issue.
If you still can't resolve it, share the contents of your Nginx site config and any relevant error log lines, and we can dive deeper!
内容的提问来源于stack exchange,提问作者Ognjen Stefanovic

