Search code examples
phplaravelnginx

Nginx returning 404 for Laravel Filament static files without '/public/' prefix


I've set up a Laravel Filament project and configured Nginx following the recommendations from the Laravel documentation. However, I'm encountering an issue where Nginx responds with a 404 error for static files unless I include the '/public/' prefix in the URL.

enter image description here Here's my Nginx configuration:

server {
    listen 80;
    listen [::]:80;
    server_name localhost;
    root /var/www/html/public;

    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-Content-Type-Options "nosniff";

    index index.php;

    charset utf-8;

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

    location = /livewire/livewire.js {
        expires off;
        try_files $uri $uri/ /index.php?$query_string;
    }

    error_page 404 /index.php;

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

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

    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
}

docker-compose file

services:
    app:
        container_name: ec-app
        restart: unless-stopped
        build:
            context: .
            dockerfile: Dockerfile
        volumes:
            - .:/var/www/html
        working_dir: /var/www/html
        depends_on:
            - db
        networks:
            - laravel
            - nginxnet
            - dbnet
    web:
        container_name: ec-web
        image: nginx:latest
        restart: unless-stopped
        ports:
            - '8060:80'
        volumes:
            - ./nginx.conf:/etc/nginx/conf.d/default.conf
            - .:/var/www/html/public
        depends_on:
            - app
        networks:
            - nginxnet
            - laravel
    db:
        container_name: ec-db
        image: mariadb:latest
        restart: unless-stopped
        ports:
            - '3307:3306'
        environment:
            MARIADB_DATABASE: laravel
            MARIADB_ALLOW_EMPTY_ROOT_PASSWORD: yes
        networks:
            - dbnet
            - laravel
        volumes:
            - db_data:/var/lib/mysql
networks:
    laravel:
    nginxnet:
    dbnet:

volumes:
    db_data:

When I try to access a static file like http://localhost:8060/css/filament/forms/forms.css?v=3.2.71.0, Nginx returns a 404 error. However, when I add the '/public/' prefix like http://localhost:8060/public/css/filament/forms/forms.css?v=3.2.71.0, the file loads successfully.

I've ensured that the file paths are correct and the permissions are set properly. Is there something missing in my Nginx configuration that's causing this issue?

Any help or insights would be greatly appreciated! Thank you!


Solution

  • It turns out the problem was the volume in the docker compose file.

    web:
        container_name: ec-web
        image: nginx:latest
        restart: unless-stopped
        ports:
            - '8060:80'
        volumes:
            - ./nginx.conf:/etc/nginx/conf.d/default.conf
            - .:/var/www/html
        depends_on:
            - app
        networks:
            - nginxnet
            - laravel