ken-korn
ken-korn

Reputation: 105

Laravel, Docker and Nginx: 404 for all files in /public folder

I created a new project in Laravel 7 by using docker-compose 3.3 and Nginx 1.17.

The enpoints created on Laravel works well, the problem comes when I try to access to whatever static asset in the '/public' folder. I tried with .css and .js files and also with the robots.txt but Laravel returns a 404 not found error.

This is my nginx.conf (taken from https://laravel.com/docs/7.x/deployment#nginx)

events {
}

http {
    server {
        server {
        listen 80;
        server_name localhost;
        root /var/www/app/public;

        index index.html index.htm index.php;

        charset utf-8;

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

        error_page 404 /index.php;

        location ~ \.php$ {
            fastcgi_pass app:9000;
            fastcgi_index index.php;
            fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
            include fastcgi_params;
        }

        location ~ /\.(?!well-known).* {
            deny all;
        }
    }
}

I tried different fixes found on forums and on Stackoverflow, like recompiling files, clear the cache... But nothing works.

Can you spot the problem?

Upvotes: 3

Views: 3553

Answers (1)

oreopot
oreopot

Reputation: 3450

can you try the following config:

server {
    listen 80;
    index index.php index.html;
    server_name localhost;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /var/www/html/public;

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

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

Upvotes: 1

Related Questions