{"id":1684,"date":"2017-06-27T16:18:53","date_gmt":"2017-06-27T10:48:53","guid":{"rendered":"https:\/\/pheonixsolutions.com\/blog\/?p=1684"},"modified":"2026-08-27T17:44:44","modified_gmt":"2026-08-27T12:14:44","slug":"laravel-route-gives-404-error-nginx","status":"publish","type":"post","link":"https:\/\/pheonixsolutions.com\/blog\/laravel-route-gives-404-error-nginx\/","title":{"rendered":"How to Fix Laravel Route 404 Error on Nginx"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Laravel applications may return a <strong>404 Not Found<\/strong> error when accessing application routes or API endpoints through Nginx, even though the application homepage works correctly.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This commonly occurs when Nginx is not configured to forward requests to Laravel&#8217;s front controller, <code>index.php<\/code>. Unlike Apache, Nginx does not use <code>.htaccess<\/code> files for URL rewriting, so the required routing rules must be configured directly in the Nginx server configuration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">In this guide, we will explain how to configure Nginx to correctly handle Laravel routes and API endpoints.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Prerequisites<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before proceeding, make sure you have:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A Laravel application deployed on the server.<\/li>\n\n\n\n<li>Nginx installed and running.<\/li>\n\n\n\n<li>PHP and PHP-FPM installed and configured.<\/li>\n\n\n\n<li>Root or sudo access to the server.<\/li>\n\n\n\n<li>The Laravel application&#8217;s document root correctly configured.<\/li>\n\n\n\n<li>For modern Laravel applications, the Nginx document root should normally point to the <code>public<\/code> directory.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Implementation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Step 1: Open the Nginx Configuration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Open the Nginx virtual host configuration for your Laravel application:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo vi \/etc\/nginx\/sites-enabled\/default<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If your Laravel application has a separate virtual host, edit that configuration instead.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 2: Configure Laravel URL Routing<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For a standard Laravel application, configure the <code>location \/<\/code> block as follows:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">location \/ {\n    try_files $uri $uri\/ \/index.php?$query_string;\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">This configuration checks whether the requested file or directory exists. If it does not, Nginx forwards the request to Laravel&#8217;s <code>index.php<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">This allows Laravel to process routes such as:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">\/login\n\/admin\n\/dashboard\n\/api\/users<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 3: Configure the Laravel Document Root<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For Laravel applications, the Nginx document root should point to the <code>public<\/code> directory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">root \/var\/www\/example.com\/public;\nindex index.php index.html;<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A basic server configuration can look like:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">server {\n    listen 80;\n    server_name example.com;\n\n    root \/var\/www\/example.com\/public;\n    index index.php index.html;\n\n    location \/ {\n        try_files $uri $uri\/ \/index.php?$query_string;\n    }\n\n    location ~ \\.php$ {\n        include snippets\/fastcgi-php.conf;\n        fastcgi_pass unix:\/run\/php\/php8.3-fpm.sock;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Replace the application path and PHP-FPM socket according to your server environment.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Step 4: Verify the Nginx Configuration<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">After making the changes, test the Nginx configuration:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo nginx -t<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">If the configuration test succeeds, reload Nginx:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo systemctl reload nginx<\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Step 5: Test the Laravel Routes<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Test the application routes from a browser or using <code>curl<\/code>:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">curl -I https:\/\/example.com\/login<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For an API endpoint:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">curl -I https:\/\/example.com\/api\/users<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The requests should now be handled by Laravel instead of returning an Nginx 404 response.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Troubleshooting<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">If the Laravel route still returns 404, check the following.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Check Laravel Routes<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">List the registered routes:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">php artisan route:list<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Confirm that the requested route exists.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Clear Laravel Caches<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">If the route was recently added or modified, clear the application caches:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">php artisan optimize:clear<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Check Nginx Error Logs<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Review the Nginx error log:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">sudo tail -f \/var\/log\/nginx\/error.log<\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Check Laravel Logs<\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Laravel application errors can usually be found under:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">storage\/logs\/<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Make sure the web server has the appropriate permissions to access the Laravel application files and write to required directories.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A Laravel route returning 404 on Nginx is commonly caused by an incorrect Nginx routing configuration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The key configuration is:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">location \/ {\n    try_files $uri $uri\/ \/index.php?$query_string;\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For modern Laravel deployments, also ensure that the Nginx document root points to the application&#8217;s <code>public<\/code> directory.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">After updating the configuration, validate it with <code>nginx -t<\/code>, reload Nginx, and verify the Laravel routes using <code>php artisan route:list<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently Asked Questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. Why does Laravel return 404 for routes on Nginx?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Nginx may not be forwarding requests for non-existent files to Laravel&#8217;s <code>index.php<\/code>. The <code>try_files<\/code> directive is required to route these requests through Laravel&#8217;s front controller.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Why does the Laravel homepage work but <code>\/login<\/code> return 404?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">The homepage may correspond to an actual file or be handled differently, while <code>\/login<\/code> requires Laravel&#8217;s routing system. If Nginx does not forward the request to <code>index.php<\/code>, it can return a 404 before Laravel receives the request.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related Article <\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/pheonixsolutions.com\/blog\/how-to-install-and-configure-php-with-nginx-on-centos7\/\">How to install and configure PHP with Nginx on centos7<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Laravel applications may return a 404 Not Found error when accessing application routes or API endpoints through Nginx, even [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[1022],"tags":[],"class_list":["post-1684","post","type-post","status-publish","format-standard","hentry","category-web-architecture","psol-cat-web-architecture"],"jetpack_publicize_connections":[],"jetpack_shortlink":"https:\/\/wp.me\/phn2x7-ra","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1684","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/comments?post=1684"}],"version-history":[{"count":1,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1684\/revisions"}],"predecessor-version":[{"id":11102,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/posts\/1684\/revisions\/11102"}],"wp:attachment":[{"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/media?parent=1684"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/categories?post=1684"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/pheonixsolutions.com\/blog\/wp-json\/wp\/v2\/tags?post=1684"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}