Introduction
Laravel applications may return a 404 Not Found error when accessing application routes or API endpoints through Nginx, even though the application homepage works correctly.
This commonly occurs when Nginx is not configured to forward requests to Laravel’s front controller, index.php. Unlike Apache, Nginx does not use .htaccess files for URL rewriting, so the required routing rules must be configured directly in the Nginx server configuration.
In this guide, we will explain how to configure Nginx to correctly handle Laravel routes and API endpoints.
Prerequisites
Before proceeding, make sure you have:
- A Laravel application deployed on the server.
- Nginx installed and running.
- PHP and PHP-FPM installed and configured.
- Root or sudo access to the server.
- The Laravel application’s document root correctly configured.
- For modern Laravel applications, the Nginx document root should normally point to the
publicdirectory.
Implementation
Step 1: Open the Nginx Configuration
Open the Nginx virtual host configuration for your Laravel application:
sudo vi /etc/nginx/sites-enabled/default
If your Laravel application has a separate virtual host, edit that configuration instead.
Step 2: Configure Laravel URL Routing
For a standard Laravel application, configure the location / block as follows:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
This configuration checks whether the requested file or directory exists. If it does not, Nginx forwards the request to Laravel’s index.php.
This allows Laravel to process routes such as:
/login /admin /dashboard /api/users
Step 3: Configure the Laravel Document Root
For Laravel applications, the Nginx document root should point to the public directory.
For example:
root /var/www/example.com/public; index index.php index.html;
A basic server configuration can look like:
server {
listen 80;
server_name example.com;
root /var/www/example.com/public;
index index.php index.html;
location / {
try_files $uri $uri/ /index.php?$query_string;
}
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.3-fpm.sock;
}
}
Replace the application path and PHP-FPM socket according to your server environment.
Step 4: Verify the Nginx Configuration
After making the changes, test the Nginx configuration:
sudo nginx -t
If the configuration test succeeds, reload Nginx:
sudo systemctl reload nginx
Step 5: Test the Laravel Routes
Test the application routes from a browser or using curl:
curl -I https://example.com/login
For an API endpoint:
curl -I https://example.com/api/users
The requests should now be handled by Laravel instead of returning an Nginx 404 response.
Troubleshooting
If the Laravel route still returns 404, check the following.
Check Laravel Routes
List the registered routes:
php artisan route:list
Confirm that the requested route exists.
Clear Laravel Caches
If the route was recently added or modified, clear the application caches:
php artisan optimize:clear
Check Nginx Error Logs
Review the Nginx error log:
sudo tail -f /var/log/nginx/error.log
Check Laravel Logs
Laravel application errors can usually be found under:
storage/logs/
Make sure the web server has the appropriate permissions to access the Laravel application files and write to required directories.
Conclusion
A Laravel route returning 404 on Nginx is commonly caused by an incorrect Nginx routing configuration.
The key configuration is:
location / {
try_files $uri $uri/ /index.php?$query_string;
}
For modern Laravel deployments, also ensure that the Nginx document root points to the application’s public directory.
After updating the configuration, validate it with nginx -t, reload Nginx, and verify the Laravel routes using php artisan route:list.
Frequently Asked Questions
1. Why does Laravel return 404 for routes on Nginx?
Nginx may not be forwarding requests for non-existent files to Laravel’s index.php. The try_files directive is required to route these requests through Laravel’s front controller.
2. Why does the Laravel homepage work but /login return 404?
The homepage may correspond to an actual file or be handled differently, while /login requires Laravel’s routing system. If Nginx does not forward the request to index.php, it can return a 404 before Laravel receives the request.