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; 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"; # 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; 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"; } # 用户间即时聊天已下线,避免网关继续暴露 WebSocket 入口。 location /ws/ { default_type application/json; return 410 '{"success":false,"code":"CHAT_DISABLED","message":"平台未提供用户之间的即时聊天功能","data":null}'; } # Auth endpoints - stricter rate limit location /api/v1/auth/ { limit_req zone=auth burst=10 nodelay; proxy_pass $backend_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 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; } # 其余未匹配路径(不含 /admin、/api、/ws)统一跳转到管理端入口。 # 根路径没有内容,显式跳转以避免"打根路径拿到一个空白/错页"的假成功。 location / { return 301 /admin/; } } }