# 全局配置 user www-data; worker_processes auto; worker_rlimit_nofile 51200; error_log /var/log/nginx/error.log crit; pid /var/run/nginx.pid; events { use epoll; worker_connections 51200; multi_accept on; } http { include mime.types; default_type application/octet-stream; client_max_body_size 1024m; # 日志格式 log_format main '$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 main; # 性能优化 sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; # Gzip 压缩 gzip on; gzip_disable "msie6"; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_buffers 16 8k; gzip_http_version 1.1; gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript; # 代理缓存配置 proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=nginx_cache:10m max_size=1g inactive=24h use_temp_path=off; # 主服务器配置 server { listen 3000; # 修改为 3000 端口 server_name localhost; # 健康检查 location /health { return 200 "OK"; } # 根路径代理 location / { proxy_pass http://127.0.0.1:3000; # 修改为 3001 端口 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_http_version 1.1; proxy_set_header Upgrade $http_upgrade; proxy_set_header Connection 'upgrade'; # WebSocket 超时设置 proxy_read_timeout 300s; proxy_send_timeout 300s; } # 静态资源缓存 location ~* \.(css|js|jpg|jpeg|png|gif|ico|svg|woff|woff2|ttf|eot)(.*) { proxy_pass http://127.0.0.1:3000; # 修改为 3001 端口 proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; # 启用浏览器缓存 add_header Cache-Control "public, max-age=31536000, immutable"; } # 模型接口缓存 location /api/models { proxy_cache nginx_cache; proxy_cache_key $request_uri; proxy_cache_valid 200 10m; proxy_cache_background_update on; proxy_cache_use_stale updating; proxy_cache_revalidate on; proxy_cache_min_uses 1; proxy_pass http://127.0.0.1:3000; # 修改为 3001 端口 add_header X-Cache-Status $upstream_cache_status; } # 排除版本检查 API 的缓存 location /api/check_version { proxy_pass http://127.0.0.1:3000; # 修改为 3001 端口 proxy_no_cache 1; } # 安全头配置 add_header X-Content-Type-Options "nosniff"; add_header X-Frame-Options "SAMEORIGIN"; add_header X-XSS-Protection "1; mode=block"; add_header Content-Security-Policy "default-src 'self';"; } }