# Sanguo VeighNa Nginx 配置 # 用于反向代理、HTTPS 和静态文件服务 user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; use epoll; } 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"'; log_format json '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log json; # 性能优化 sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; client_max_body_size 100M; # Gzip 压缩 gzip on; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_types text/plain text/css text/xml text/javascript application/json application/javascript application/xml+rss application/rss+xml font/truetype font/opentype application/vnd.ms-fontobject image/svg+xml; # 缓存配置 proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=vnpy_cache:10m max_size=1g inactive=60m use_temp_path=off; # ============================================ # HTTP 服务器 - 重定向到 HTTPS # ============================================ server { listen 80; server_name _; # 允许健康检查 location /health { access_log off; return 200 "healthy\n"; add_header Content-Type text/plain; } # 其他请求重定向到 HTTPS location / { return 301 https://$host$request_uri; } } # ============================================ # HTTPS 服务器 # ============================================ server { listen 443 ssl http2; server_name _; # SSL 证书配置 ssl_certificate /etc/nginx/ssl/cert.pem; ssl_certificate_key /etc/nginx/ssl/key.pem; # SSL 安全配置 ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m; # 安全头 add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always; add_header X-Frame-Options "SAMEORIGIN" always; add_header X-Content-Type-Options "nosniff" always; add_header X-XSS-Protection "1; mode=block" always; # ============================================ # API 代理 # ============================================ location /api/ { proxy_pass http://vnpy:8000; 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 60s; proxy_send_timeout 60s; proxy_read_timeout 60s; } # ============================================ # WebSocket 代理 # ============================================ location /ws/ { proxy_pass http://vnpy:8080; proxy_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection "upgrade"; 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; # WebSocket 超时配置 proxy_connect_timeout 7d; proxy_send_timeout 7d; proxy_read_timeout 7d; } # ============================================ # 健康检查 # ============================================ location /health { proxy_pass http://vnpy:8000/health; access_log off; } # ============================================ # 静态文件 # ============================================ location /static/ { alias /app/static/; expires 7d; add_header Cache-Control "public, immutable"; } location /favicon.ico { access_log off; log_not_found off; } # ============================================ # 根路径 - 前端应用 # ============================================ location / { proxy_pass http://vnpy:8000; 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; } # ============================================ # 安全:拒绝访问敏感文件 # ============================================ location ~ /\. { deny all; access_log off; log_not_found off; } location ~ ~\.(git|svn|env)$ { deny all; access_log off; log_not_found off; } } # ============================================ # 流控制(可选) # ============================================ limit_req_zone $binary_remote_addr zone=api_limit:10m rate=10r/s; limit_req_zone $binary_remote_addr zone=ws_limit:10m rate=5r/s; limit_conn_zone $binary_remote_addr zone=conn_limit:10m; # 应用限流 location /api/ { limit_req zone=api_limit burst=20 nodelay; limit_conn conn_limit 10; } location /ws/ { limit_req zone=ws_limit burst=10 nodelay; limit_conn conn_limit 5; } }