Nginx 对于没有 '/public/' 前缀的 Laravel Filament 静态文件返回 404

问题描述 投票:0回答:1

我已经按照 Laravel 文档的建议设置了一个 Laravel Filament 项目并配置了 Nginx。但是,我遇到了一个问题,即 Nginx 对静态文件响应 404 错误,除非我在 URL 中包含“/public/”前缀。

enter image description here 这是我的 Nginx 配置:

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 文件

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:

当我尝试访问 http://localhost:8060/css/filament/forms/forms.css?v=3.2.71.0 等静态文件时,Nginx 返回 404 错误。但是,当我添加“/public/”前缀(如 http://localhost:8060/public/css/filament/forms/forms.css?v=3.2.71.0)时,文件加载成功。

我已确保文件路径正确并且权限设置正确。我的 Nginx 配置中是否缺少某些内容导致了此问题?

任何帮助或见解将不胜感激!谢谢!

php laravel nginx
1个回答
0
投票

事实证明问题出在 docker compose 文件中的卷。

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
© www.soinside.com 2019 - 2024. All rights reserved.