user nginx; worker_processes auto; error_log /var/log/nginx/error.log warn; pid /var/run/nginx.pid; events { worker_connections 1024; use epoll; multi_accept on; } 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"'; access_log /var/log/nginx/access.log main; # 基本设置 sendfile on; tcp_nopush on; tcp_nodelay on; keepalive_timeout 65; types_hash_max_size 2048; server_tokens off; # 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; # 文件上传限制 client_max_body_size 64G; client_body_timeout 60s; client_header_timeout 60s; # 缓存设置 open_file_cache max=10000 inactive=5m; open_file_cache_valid 2m; open_file_cache_min_uses 1; open_file_cache_errors on; # 上游服务器 upstream chuan_backend { server chuan:8080; keepalive 32; } # HTTP服务器(重定向到HTTPS) server { listen 80; server_name _; return 301 https://$server_name$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_session_timeout 1d; ssl_session_cache shared:SSL:50m; ssl_session_tickets off; # 现代SSL配置 ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384; ssl_prefer_server_ciphers off; # 安全头 add_header Strict-Transport-Security "max-age=63072000" always; add_header X-Frame-Options DENY; add_header X-Content-Type-Options nosniff; add_header X-XSS-Protection "1; mode=block"; # 静态文件缓存 location ~* \.(css|js|jpg|jpeg|png|gif|ico|svg|woff|woff2|ttf|eot)$ { expires 1y; add_header Cache-Control "public, immutable"; add_header Vary Accept-Encoding; access_log off; } # 上传文件服务 location /uploads/ { alias /var/www/uploads/; expires 24h; add_header Cache-Control "public"; # 安全设置 add_header X-Content-Type-Options nosniff; add_header Content-Security-Policy "default-src 'none'; style-src 'unsafe-inline';"; # 限制访问 valid_referers none blocked server_names; if ($invalid_referer) { return 403; } } # WebSocket代理 location /ws/ { proxy_pass http://chuan_backend; 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; proxy_read_timeout 86400; } # API代理 location /api/ { proxy_pass http://chuan_backend; 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; } # 主应用代理 location / { proxy_pass http://chuan_backend; 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_buffering on; proxy_buffer_size 4k; proxy_buffers 8 4k; # 超时设置 proxy_connect_timeout 30s; proxy_send_timeout 30s; proxy_read_timeout 30s; } # 健康检查 location /health { access_log off; return 200 "healthy\n"; add_header Content-Type text/plain; } # 错误页面 error_page 404 /404.html; error_page 500 502 503 504 /50x.html; location = /404.html { root /usr/share/nginx/html; } location = /50x.html { root /usr/share/nginx/html; } } }