Skip to content

[BUG] Docker 单镜像部署显示 nginx 欢迎页,无法访问 BiliNote 页面 #405

@fivedang

Description

@fivedang

环境

  • 镜像: ghcr.io/jefferyhcool/bilinote:latest
  • 运行命令: docker run -d -p 80:80 ghcr.io/jefferyhcool/bilinote:latest
  • docker ps 显示容器正常运行,端口映射 0.0.0.0:80->80/tcp

现象

访问 http://localhost 显示 nginx 默认欢迎页("Welcome to nginx!"),无法进入 BiliNote 界面。

根因分析

经过排查,发现问题出在 Dockerfile.complete 构建的镜像中存在两个 nginx 配置缺陷:

1. 默认 nginx site 劫持了 80 端口

/etc/nginx/sites-enabled/default 中定义了:

server {
    listen 80 default_server;
    listen [::]:80 default_server;
    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;
    server_name _;
    location / {
        try_files $uri $uri/ =404;
    }
}

这个 default_server 优先级高于 /etc/nginx/conf.d/default.conf 中 BiliNote 的自定义配置,导致所有请求都走到了 /var/www/html(nginx 欢迎页),而不是 /usr/share/nginx/html(BiliNote 前端文件)。

2. 自定义 nginx 配置错误地将前端请求代理到不存在的 8080 端口

/etc/nginx/conf.d/default.conf/ 路由配置为:

location / {
    proxy_pass http://127.0.0.1:8080;
}

但容器中没有任何服务在 8080 端口监听。容器内只有:

  • nginx (80)
  • Python backend (8483)

Supervisor 配置也只管理 nginxbackend 两个进程,不包含前端服务。

根据 Dockerfile.complete 的构建逻辑,前端是通过 pnpm run build 编译后复制到 /usr/share/nginx/html/ 的静态文件。构建脚本中执行的 sed 替换:

proxy_pass http://frontend:80 → http://127.0.0.1:8080

这原本是为 docker-compose 多容器部署准备的配置,在单镜像部署中应该改为直接服务静态文件,而不是代理到不存在的 8080 端口。

临时修复方法(当前运行容器)

# 1. 删除默认 nginx site
docker exec -u root bilinote rm /etc/nginx/sites-enabled/default

# 2. 修改 nginx 配置,将 / 路由改为直接服务静态文件
docker exec -u root bilinote sh -c "cat > /etc/nginx/conf.d/default.conf << 'NGINX_CONF'
server {
  listen 80;
  client_max_body_size 10G;

  gzip on;
  gzip_vary on;
  gzip_min_length 1024;
  gzip_proxied any;
  gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript image/svg+xml;

  location / {
    root /usr/share/nginx/html;
    index index.html;
    try_files \$uri \$uri/ /index.html;
  }

  location /api/ {
    proxy_pass http://127.0.0.1:8483;
    proxy_set_header Host \$host;
    proxy_set_header X-Real-IP \$remote_addr;
  }

  location /static/ {
    proxy_pass http://127.0.0.1:8483/static/;
    proxy_set_header Host \$host;
    proxy_set_header X-Real-IP \$remote_addr;
    expires 7d;
    add_header Cache-Control \"public, immutable\";
  }
}
NGINX_CONF"

# 3. 重载 nginx
docker exec -u root bilinote nginx -s reload

建议的修复方向

Dockerfile.complete 中:

  1. 删除默认 nginx site: rm /etc/nginx/sites-enabled/default
  2. 将 nginx 配置模板中 / 路由从 proxy_pass http://127.0.0.1:8080 改为直接服务 /usr/share/nginx/html/ 下的静态文件,并使用 try_files 支持 SPA 路由

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions