# Nginx配置文件 - 生产环境 # 用于Vue3前端静态文件服务和API代理 # HTTP服务器 - 重定向到HTTPS server { listen 80; server_name anxin.com www.anxin.com; # 强制重定向到HTTPS return 301 https://$server_name$request_uri; } # HTTPS服务器 - 主要配置 server { listen 443 ssl http2; server_name anxin.com www.anxin.com; # SSL配置 ssl_certificate /etc/nginx/ssl/anxin.com.crt; ssl_certificate_key /etc/nginx/ssl/anxin.com.key; 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; # HSTS配置 add_header Strict-Transport-Security "max-age=63072000" always; # 生产环境日志配置 access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log error; # 安全头配置 add_header X-Frame-Options DENY; add_header X-Content-Type-Options nosniff; add_header X-XSS-Protection "1; mode=block"; add_header Referrer-Policy "strict-origin-when-cross-origin"; add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self' data:; connect-src 'self' https://api.anxin.com;"; # Gzip压缩配置 gzip on; gzip_vary on; gzip_min_length 1024; 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/atom+xml image/svg+xml; # 静态文件服务 location / { root /usr/share/nginx/html; index index.html index.htm; try_files $uri $uri/ /index.html; # 生产环境缓存配置 location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { expires 1y; add_header Cache-Control "public, immutable"; access_log off; } location ~* \.(html)$ { expires 1h; add_header Cache-Control "public"; } } # API代理到后端服务 location /prod-api/ { proxy_pass http://anxin-backend:8080/; 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 10s; proxy_send_timeout 10s; proxy_read_timeout 10s; proxy_buffering on; proxy_buffer_size 4k; proxy_buffers 8 4k; # 限制请求大小 client_max_body_size 5m; # 代理缓存配置 proxy_cache_bypass $http_upgrade; proxy_set_header Connection 'upgrade'; proxy_set_header Upgrade $http_upgrade; } # 健康检查端点 location /health { access_log off; return 200 "healthy\n"; add_header Content-Type text/plain; } # 拒绝访问敏感文件 location ~ /\. { deny all; access_log off; log_not_found off; } location ~ ~$ { deny all; access_log off; log_not_found off; } }