Files
KennethCheng 7d5ab0a2ec fix(deploy): API_PORT 端口模板化,web upstream 不再写死 8080
NAS 部署时 POST /api/vehicles 返 502 Bad Gateway,根因是 compose 改
SERVER_ADDR: ":8081" 但 fuellog-web/nginx/web.conf 里 upstream 还写死
server api:8080 → nginx upstream 连不上,web 容器把请求反代失败,返 502。

修法:把端口做成 build arg + sed 替换:
- fuellog-web/Dockerfile 加 `ARG API_PORT=8080` + `sed` 在 build 时把
  web.conf 里的 __API_PORT__ 占位替换为真实端口
- deploy/docker-compose.yml:
  * api.SERVER_ADDR 改成 `:${API_PORT:-8080}`(默认仍是 8080)
  * api.expose / healthcheck 端口都改用 ${API_PORT:-8080}
  * web build args 传 API_PORT,容器内 web 镜像就有了对的 upstream
  * 新增 WEB_PORT 控制宿主机 → web 容器 :80 映射
- README 新增"改端口"小节,说明 API_PORT / WEB_PORT 用法;
  新增"排错:502 Bad Gateway"小节,把这次踩的坑(改 SERVER_ADDR 忘
  改 web upstream、忘重 build)写明,下次再撞就有现成排查路径

期间的小坑(都修了):
- web.conf 占位用 __API_PORT__(双下划线)而不是 ${API_PORT},避免
  nginx 把它当成变量语法尝试解析
- docker-compose 用 ${API_PORT:-8080} 而不是裸 ${API_PORT},这样
  用户不传环境变量也能跑(默认 8080)
- README 里 web/api 表格里"golang:1.22-alpine"也顺手改成 1.27-alpine
  跟实际 Dockerfile 一致(上次 f81047f 提过但 README 漏了)

未做(留待后续):
- 端口改成可改 web upstream 后,理论上仍然可能出现 build-time / run-time
  不一致(build args vs 环境变量);更稳的方案是给 web 加个 entrypoint 脚本,
  启动时再 sed 一次 env 变量到 nginx conf —— 留到真有"频繁换端口"的
  场景再做
- `.env.example` 没改,只加了 README 文档化的 .env 用法;compose 默认值
  已经是 8080,本地 dev 不需要 .env
2026-09-13 19:39:19 +08:00

57 lines
1.7 KiB
Plaintext

# FuelLog 前端容器用的 nginx 配置
# - 提供 /usr/share/nginx/html 静态资源
# - SPA fallback:所有非 /api 路径都回 /index.html(让 vue-router 接管)
# - /api/* 反代到后端容器 api:8080
upstream fuellog_api {
server api:__API_PORT__;
keepalive 32;
}
server {
listen 80 default_server;
server_name _;
# gzip 静态资源
gzip on;
gzip_types text/plain text/css application/javascript application/json image/svg+xml application/xml;
gzip_min_length 1024;
# 静态资源 + SPA fallback
root /usr/share/nginx/html;
index index.html;
# 显式 favicon / manifest / icon
location = /favicon.svg { access_log off; expires 30d; }
location = /manifest.webmanifest { access_log off; expires 1d; }
location ~* ^/pwa-.*\.png$ { access_log off; expires 30d; }
# 反代 /api 到后端
location /api/ {
proxy_pass http://fuellog_api;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_set_header Connection ""; # keep-alive
proxy_read_timeout 30s;
proxy_send_timeout 30s;
}
# /api/health 这种后端直出,允许直接走
location = /health { proxy_pass http://fuellog_api/health; }
# Hashed 静态资源(js/css/woff2)长缓存
location ~* \.(?:js|css|woff2|svg|png|jpg|jpeg|gif|ico)$ {
expires 30d;
add_header Cache-Control "public, immutable";
try_files $uri =404;
}
# SPA fallback
location / {
try_files $uri $uri/ /index.html;
}
}