Laravel routes not found after nginx install
Switching a PHP stack from Apache to Nginx often feels like trading one set of problems for another. If you are seeing "Route Not Found" errors or 404s for every URL except your index.php file immediately after installing Nginx, the issue is almost certainly how the web server handles static assets and PHP routing logic. The configuration snippets provided show a classic misalignment between the virtual host definition and Laravel's expectations.
The primary culprit lies in the location /home block within your specific site configuration:
location /home {
root /home/public;
try_files $uri $uri/ /index.php?$query_string;
}
Here, you have defined a distinct location for /home. When a request hits http://192.168.178.71/api/users (assuming your Laravel app is mounted at the root or /home), Nginx strips that prefix and looks for files inside /home/public. If that file doesn't exist, it falls back to /index.php?$query_string.
The critical failure point here is the root directive. When you use location /home, setting root /home/public tells Nginx to resolve $uri relative to /home/public. Consequently, a request for /api/users becomes a file system lookup for /home/public/api/users. If your Laravel public folder structure expects requests to be routed through the app root without stripping the prefix in this manner, you end up with 404s.
Furthermore, the try_files directive logic is slightly off for a standard Laravel setup mounted at the document root. The standard approach relies on falling back to the application's entry point only when a file or directory is not found. Your current config attempts to serve static files from /home/public while routing dynamic requests to /index.php. If your actual Laravel public folder is located at /var/www/home/public (based on your root /var/www/home in the default block), you have conflicting root directives causing Nginx to look in the wrong directories.
Correcting the Virtual Host Configuration
To resolve this, you should consolidate your configuration into a single, clean virtual host that aligns with Laravel's standard document root structure. Remove the specific location /home block if your application is served directly at the domain root defined in the server block.
Here is a corrected configuration tailored for /etc/nginx/sites-enabled/laravel:
server {
listen 80;
server_name 192.168.178.71;
# Ensure this points to your actual Laravel public directory
root /var/www/home/public;
index index.php index.html;
access_log /var/www/home/storage/logs/laravel-nginx-access.log;
error_log /var/www/home/storage/logs/laravel-nginx-error.log error;
# Handle static assets and fallback to Laravel router
location / {
try_files $uri $uri/ /index.php?$query_string;
}
# Serve images, CSS, JS directly from the public folder
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 30d;
add_header Cache-Control "public, no-transform";
}
# Block access to hidden files like .env or .htaccess
location ~ /\. {
deny all;
access_log off;
log_not_found off;
}
# Handle PHP scripts
location ~ \.php$ {
try_files $uri =404;
fastcgi_split_path_info ^(.+\.php)(/.+)$;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_pass unix:/var/run/php5-fpm.sock;
# Ensure error logs are captured correctly
fastcgi_param DOCUMENT_ROOT $document_root;
}
# Redirect 404s to index.php only if necessary,
# but usually standard try_files logic is sufficient.
error_page 404 /index.php;
}
Key Technical Decisions
- Document Root Alignment: The
rootdirective must point directly to the folder containing your compiled assets (usuallypublic). If you setroot /var/www/home, Nginx looks for files at/var/www/home/index.php. If your actual entry point is/var/www/home/public/index.php, you must adjust the root or use a rewrite rule. The configuration above assumes the standard Laravel structure where the web server serves directly from thepublicfolder. - The
try_filesLogic: This directive attempts to serve the URI as a file, then a directory. If neither exists, it rewrites the request to/index.php. The$query_stringis crucial here because Laravel routes depend on query parameters. Omitting it (as seen in some of your attempts liketry_files $uri $uri/ /index.php;) breaks route resolution for authenticated or parameterized requests. - Permissions vs. Configuration: You mentioned changing ownership between
www-data:www-dataandroot:root. While permissions are vital for PHP-FPM to write logs, they do not fix routing logic. If Nginx is configured to pass the request to PHP but the script itself returns a 404 due to incorrect path resolution, changing file owners will not help. Ensurestorageandbootstrap/cachefolders are writable bywww-data, but focus on the Nginxrootdirective first.
Final Checks
Before reloading Nginx (sudo nginx -t && sudo systemctl reload nginx), verify that your fastcgi_pass socket matches your PHP-FPM configuration. The default config shows unix:/var/run/php5-fpm.sock, which is standard for Debian/Ubuntu setups, but ensure the socket file actually exists and has the correct permissions (660).
If you are still encountering issues after correcting the virtual host, check your .htaccess files. While Nginx ignores them by default, some configurations might attempt to pass control back to Apache or create loops if mixed environments exist. Stick to pure Nginx directives for this stack.