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

Timbertlt's avatar

Laravel 6.0 render nginx 404 error

Upgraded my app from 5.8 to 6.0.4. The nginx config is the same:

location / {
        ........
        try_files $uri $uri/ /index.php$is_args$args;
}

location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
        ..........
        fastcgi_pass unix:/run/php/php7.3-fpm.sock;
}

wep.php config:

.....
some app routes
.....
Route::get('/{page}',[
    'uses'  => 'PageController@getPage',
    'as'    => 'page'
]);

Route::fallback('PageController@fallback');

On laravel 5.8 if the route does not exist, I get 404 error rendered by laravel.

On laravel 6.0 I get nginx 404 error.

Is it the problem with laravel routing or do I need to change something in nginx config?

0 likes
3 replies
bobbybouwmann's avatar

This should still work the same... Are you sure you didn't change anything in your nginx config?

Timbertlt's avatar

Nothing changed. I've just copied old config and changed path to public, server name, and php7.1 to php7.3.

In error log I see something like this:

2019/09/27 14:38:04 [error] 2442#2442: *258 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: 192.168.56.1, server: safaribelleza3, request: "GET /dfdfdfdffd/dfdff HTTP/1.1", upstream: "fastcgi://unix:/run/php/php7.3-fpm.sock:", host: "safaribelleza3"

I tried to make the location @laravel and send requests to this location:

    location / {
        server_tokens off;
        client_max_body_size 10m;
        client_body_buffer_size 128k;
        #try_files $uri $uri/ /index.php$is_args$args;
        try_files $uri $uri/ @laravel;
    }

    location @laravel {
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 128k;
        fastcgi_read_timeout 300;
        fastcgi_pass unix:/run/php/php7.3-fpm.sock;
        fastcgi_index index.php;
        include /etc/nginx/fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root/index.php;
    }

With this config works everything except home page, and 404 error is rendered by laravel.

Timbertlt's avatar

I've created nginx config, which works in my case:

server {
    listen 80;
    server_name example.com;

    error_page 404 = /index.php;

    error_log /path_to_log/log.error.log;
    charset utf8;
    index index.php;
    root /path_web_root/example.com/public/;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    location ~* \.(jpg|jpeg|gif|css|png|js|ico|html|woff2)$ {
        access_log off;
        expires max;
        log_not_found off;
    }

    # removes trailing slashes
    if (!-d $request_filename)
    {
        rewrite ^/(.+)/$ / permanent;
    }

    if (!-e $request_filename)
    {
        rewrite ^/(.*)$ /index.php?/ last;
        break;
    }

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

    location ~* \.php$ {
        fastcgi_buffer_size 128k;
        fastcgi_buffers 4 128k;
        fastcgi_read_timeout 300;
        try_files $uri = 404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.3-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }

    location ~ /.well-known {
        allow all;
    }

    location ~ /\.ht {
        deny all;
    }
}

I'm not an nginx guru, so if you have any suggestions, please let me know.

Please or to participate in this conversation.