126 lines
4.4 KiB
Nginx Configuration File
126 lines
4.4 KiB
Nginx Configuration File
# =============================================================================
|
||
# PERLER LAB — Nginx 前端服务配置
|
||
# 用途:SPA 路由回退 + 静态资源缓存 + 安全头 + Gzip + API 反向代理
|
||
# 部署方式:docker-compose 中作为 frontend 服务,/api 反向代理到 server 服务
|
||
# =============================================================================
|
||
#
|
||
# 兼容性说明:
|
||
# - 当前 perler-lab 为 Vite + React 单页应用(vite build → dist/)
|
||
# - 若未来迁移到 Next.js,需改为 next build + node server 模式,此配置转为
|
||
# 上层 Nginx 反代(参考 DEPLOY.md 的域名 HTTPS 方案)
|
||
#
|
||
# =============================================================================
|
||
|
||
upstream backend {
|
||
# docker-compose 中 backend 服务名为 server,端口 8788
|
||
server server:8788;
|
||
}
|
||
|
||
upstream captcha-platform {
|
||
# tianai-captcha 自托管平台(prod1)。
|
||
# 值由环境变量 CAPTCHA_UPSTREAM 注入(Dockerfile 给默认值,生产 deploy-prod.yml
|
||
# 覆盖为 WireGuard IP 10.9.0.2:18200——prod2 公网到 prod1:18200 被安全组拦截)。
|
||
# 本文件作为 /etc/nginx/templates/default.conf.template 挂载,由 nginx 镜像入口的
|
||
# envsubst 渲染;envsubst 只替换「已定义的环境变量」,$host/$remote_addr 等
|
||
# nginx 内置变量不受影响。
|
||
server ${CAPTCHA_UPSTREAM};
|
||
}
|
||
|
||
server {
|
||
listen 80 default_server;
|
||
server_name _;
|
||
|
||
# ── 静态文件根目录(Vite 构建产物 dist/ 合并至此,index.html 在根目录) ──
|
||
root /usr/share/nginx/html;
|
||
index index.html;
|
||
|
||
# ── Gzip 压缩 ──
|
||
gzip on;
|
||
gzip_vary on;
|
||
gzip_proxied any;
|
||
gzip_comp_level 6;
|
||
gzip_min_length 1024;
|
||
gzip_types
|
||
text/plain
|
||
text/css
|
||
text/xml
|
||
text/javascript
|
||
application/json
|
||
application/javascript
|
||
application/xml
|
||
application/xml+rss
|
||
application/octet-stream
|
||
image/svg+xml
|
||
image/x-icon;
|
||
|
||
# ── 安全头 ──
|
||
add_header X-Content-Type-Options "nosniff" always;
|
||
add_header X-Frame-Options "SAMEORIGIN" always;
|
||
add_header X-XSS-Protection "1; mode=block" always;
|
||
add_header Referrer-Policy "strict-origin-when-cross-origin" always;
|
||
add_header Permissions-Policy "camera=(), microphone=(), geolocation=()" always;
|
||
# 若需 CSP 可在此加,但拼豆工具需要加载用户上传图片,暂不限制过严
|
||
# add_header Content-Security-Policy "default-src 'self'; img-src 'self' data: blob:; ..." always;
|
||
|
||
# ── 静态资源:强缓存(Vite 产物文件名带内容 hash,可 immutable) ──
|
||
location /assets/ {
|
||
expires 30d;
|
||
add_header Cache-Control "public, immutable, max-age=2592000";
|
||
access_log off;
|
||
}
|
||
|
||
# ── Favicon ──
|
||
location = /favicon.svg {
|
||
expires 7d;
|
||
add_header Cache-Control "public, max-age=604800";
|
||
access_log off;
|
||
}
|
||
|
||
# ── API 反向代理 → 后端服务 ──
|
||
location /api/ {
|
||
proxy_pass http://backend;
|
||
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_read_timeout 120s;
|
||
proxy_send_timeout 30s;
|
||
|
||
# 关闭缓冲,便于 SSE / 大响应
|
||
proxy_buffering off;
|
||
}
|
||
|
||
# ── TAC 人机验证:同源反代 → tianai-captcha 平台(/tac/api/xx → /api/xx,generate/verify 用 X-Site-Key 头) ──
|
||
location /tac/api/ {
|
||
proxy_pass http://captcha-platform/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_read_timeout 15s;
|
||
proxy_send_timeout 15s;
|
||
}
|
||
|
||
# ── 健康检查端点 ──
|
||
location = /healthz {
|
||
access_log off;
|
||
return 200 "OK";
|
||
}
|
||
|
||
# ── SPA 路由回退 ──
|
||
location / {
|
||
try_files $uri $uri/ /index.html;
|
||
}
|
||
|
||
# ── 拒绝访问隐藏文件 ──
|
||
location ~ /\.(?!well-known) {
|
||
deny all;
|
||
access_log off;
|
||
log_not_found off;
|
||
}
|
||
} |