Nginx 在 docker-compose 中阻止静态文件

Nginx 在 docker-compose 中阻止静态文件

我在内部端口 8732 上运行 docker-compose,Nginx 作为代理服务器工作,它监听来自 80/443 的请求并将它们转发到内部 8732。问题是静态文件(.css、.js、.png)没有提供服务并出现错误 404,而主文件成功获得状态 200。这是我的 docker-compose 文件:

version: '3'

services:
  vaultwarden:
    image: vaultwarden/server:latest
    container_name: vaultwarden
    restart: always
    volumes:
      - ./vw-data:/data
    ports:
      - 8732:80
    environment:
      WEBSOCKET_ENABLED: "true"  # Enable WebSocket notifications.

这是 Nginx 配置文件:

# HTTPS configuration
server {

        # SSL configuration
        listen [::]:443 ssl default_server ipv6only=on;
        listen 443 ssl default_server;

        ssl_certificate /etc/letsencrypt/live/bw.example.com/fullchain.pem;
        ssl_certificate_key /etc/letsencrypt/live/bw.example.com/privkey.pem;
        ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

        include /etc/letsencrypt/options-ssl-nginx.conf;
        # include snippets/snakeoil.conf;

        server_name bw.example.com;
        root /opt/vault;

        # index index.html index.htm index.nginx-debian.html;

        location / {
                # First attempt to serve request as file, then
                # as directory, then fall back to displaying a 404.
                proxy_pass http://localhost:8732;
                try_files $uri $uri/ =404;      
        }

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

}

#HTTP config
server {
    if ($host = bw.example.com) {
        return 301 https://$host$request_uri;
    }

        listen 80 default_server;
        listen [::]:80 default_server;

        server_name bw.example.com;
    return 404;

}

答案1

我已经通过注释/删除 nginx 配置文件 try_files 中的行解决了这个问题

#try_files $uri $uri/ =404;      

try_files按指定顺序检查文件是否存在,并使用第一个找到的文件进行请求处理;处理在当前上下文中执行。

相关内容