Files
danyang-match/infrastructure/nginx.conf
T

124 lines
4.8 KiB
Nginx Configuration File
Raw Normal View History

2026-09-09 22:05:00 +08:00
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for" '
'request_id=$upstream_http_x_request_id';
access_log /var/log/nginx/access.log main;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
# Gzip
gzip on;
gzip_vary on;
gzip_min_length 1024;
gzip_types text/plain text/css text/xml text/javascript application/javascript application/xml+rss application/json;
# Rate limiting zone
limit_req_zone $binary_remote_addr zone=api:10m rate=30r/s;
limit_req_zone $binary_remote_addr zone=auth:10m rate=5r/s;
limit_req_zone $binary_remote_addr zone=upload:10m rate=10r/s;
# Docker Compose 内置 DNS resolver —— 让 proxy_pass 中的主机名延迟解析(请求时才查),
# 避免 nginx 启动时 backend/admin-web 容器的 DNS 还没注册好就 [emerg] 退出。
resolver 127.0.0.11 valid=30s ipv6=off;
2026-09-09 22:05:00 +08:00
server {
listen 80;
server_name localhost;
# upstream 变量(必须在 server 块内,不能在 http 顶层)
# 使用固定容器名,避免后端因跨 compose 网络重连后丢失 service alias。
set $backend_upstream "http://danyang-match-backend:8080";
set $admin_upstream "http://admin-web:80";
2026-10-19 20:36:22 +08:00
set $h5_upstream "http://h5-web:80";
2026-09-09 22:05:00 +08:00
# Health check for nginx itself
location /nginx-health {
return 200 'ok';
add_header Content-Type text/plain;
}
# API proxy
location /api/ {
limit_req zone=api burst=50 nodelay;
proxy_pass $backend_upstream;
2026-09-09 22:05:00 +08:00
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_connect_timeout 30s;
proxy_send_timeout 60s;
proxy_read_timeout 60s;
add_header Cache-Control "no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0";
add_header Pragma "no-cache";
2026-09-09 22:05:00 +08:00
}
# 用户间即时聊天已下线,避免网关继续暴露 WebSocket 入口。
2026-09-09 22:05:00 +08:00
location /ws/ {
default_type application/json;
return 410 '{"success":false,"code":"CHAT_DISABLED","message":"平台未提供用户之间的即时聊天功能","data":null}';
2026-09-09 22:05:00 +08:00
}
# Auth endpoints - stricter rate limit
location /api/v1/auth/ {
limit_req zone=auth burst=10 nodelay;
proxy_pass $backend_upstream;
2026-09-09 22:05:00 +08:00
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;
}
# Admin web —— 管理端产物挂在 /admin/ 子路径下(构建 base=/admin/,
# 见 admin-web/vite.config.ts)。与生产 nginx 保持同一形态:**不剥离**前缀地
# 转发给 admin-web 容器,由容器内的 nginx(admin-web/nginx-admin.conf)
# 统一处理 /admin/ 前缀。三处入口(生产 / 本地整套栈 / staging 直连)因此一致。
location = /admin {
return 301 /admin/;
}
location /admin/ {
proxy_pass $admin_upstream;
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;
2026-09-09 22:05:00 +08:00
}
2026-10-19 20:36:22 +08:00
# H5 演示端 —— 小程序用户端 + 红娘端的网页版,产物挂在 /h5/ 子路径下
# (构建 base=/h5/,见 h5-demo/vite.config.ts)。与 /admin/ 同构:**不剥离**前缀
# 转发给 h5-demo 容器,由容器内 nginx(h5-demo/nginx-h5.conf)自行处理 /h5 前缀。
location = /h5 {
return 301 /h5/;
}
location /h5/ {
proxy_pass $h5_upstream;
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;
}
# 其余未匹配路径(不含 /admin、/api、/ws)统一跳转到管理端入口。
# 根路径没有内容,显式跳转以避免"打根路径拿到一个空白/错页"的假成功。
location / {
return 301 /admin/;
}
2026-09-09 22:05:00 +08:00
}
}