server { listen 80; server_name localhost; root /usr/share/nginx/html; index index.html; # 开启gzip压缩 gzip on; gzip_vary on; gzip_min_length 1024; gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml+rss application/javascript application/json; # WebSocket 反代(聊天等实时功能) location /ws/ { proxy_pass http://127.0.0.1:8000; 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 3600s; proxy_send_timeout 3600s; } # API 反代(路由分散在 /api /user /bug /article /chat /learn /message /tool /history /media 等前缀下) location ~ ^/(api|user|bug|article|chat|learn|message|tool|history|media)/ { proxy_pass http://127.0.0.1:8000; proxy_http_version 1.1; 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_set_header Content-Type $http_content_type; proxy_read_timeout 60s; proxy_send_timeout 60s; client_max_body_size 50M; } # 静态资源缓存(图片/字体/JS/CSS) location ~* \.(js|css|png|jpg|jpeg|gif|ico|svg|woff|woff2|ttf|eot)$ { expires 1y; add_header Cache-Control "public, immutable"; } # SPA 路由回退(放在最后,避免拦截上面的 API 请求) location / { try_files $uri $uri/ /index.html; } }