Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

parth's avatar
Level 3

Getting file not found for all routes except default route on nginx

Hi, I am setting up Laravel 10 app on Ubuntu 22.04. The web server I am using is nginx. The issue I am facing is when I am accessing the default route, i can see the laravel default page, but when I try another route I am getting "File not found" message in the browser. Here is the config from my nginx file:

location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                #       try_files $uri $uri/ =404;
                try_files $uri $uri/ /index.php$is_args$args;
        }

        error_page 404 /index.php;

        # pass PHP scripts to FastCGI server
        #
        location ~ \.php$ {
        #       include snippets/fastcgi-php.conf;
        #
        #       # With php-fpm (or other unix sockets):
                fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        #       # With php-cgi (or other tcp sockets):
        #       fastcgi_pass 127.0.0.1:9000;
                fastcgi_index index.php;
                include fastcgi_params;
                fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
                #fastcgi_split_path_info ^(.+?\.php)(/.+)$;
                #include fastcgi_params;
        }

Need help as i am stuck with this issue.

0 likes
1 reply
LaryAI's avatar
Level 58

It looks like your Nginx configuration is mostly correct, but there might be a few tweaks needed to ensure that it works properly with Laravel. Here's a revised version of your Nginx configuration that should work:

server {
    # ... other server config ...

    root /path/to/your/laravel/public;

    index index.php index.html index.htm;

    server_name yourdomain.com;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    # ... any other location blocks or directives ...
}

Make sure to replace /path/to/your/laravel/public with the actual path to the public directory of your Laravel application and yourdomain.com with your actual domain or IP address.

Here are a few things to check and ensure they are correct:

  1. Root Directive: Ensure that the root directive points to the public directory of your Laravel application.
  2. Server Name: The server_name should match your domain or IP address.
  3. Try Files: The try_files directive should attempt to serve the URI as a file, then as a directory, and finally, pass the request to index.php with the query string.
  4. PHP Location Block: The fastcgi_param SCRIPT_FILENAME should be set to $document_root$fastcgi_script_name to ensure the correct script is executed.
  5. PHP Socket: Ensure that the fastcgi_pass directive points to the correct PHP-FPM socket file. This should match the version of PHP you are using and the path where the socket is located.

After making these changes, you should restart Nginx to apply the new configuration:

sudo systemctl restart nginx

If you continue to experience issues, check the Nginx error logs for more detailed error messages:

sudo tail -f /var/log/nginx/error.log

This should help you troubleshoot any remaining issues with your Nginx configuration.

Please or to participate in this conversation.