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 反代(聊天 ws/chat、扫码登录 ws/qr-status → Granian + Channels) location /ws/ { proxy_pass http://backend: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 反代 → Granian(ADRF 异步视图) # 路由分散在 /api /user /bug /article /chat /learn /message /tool /history /media 等前缀下 location ~ ^/(api|user|bug|article|chat|learn|message|tool|history|media|search|app|logs|s|air-quality|weather|currency|shorturl)/ { proxy_pass http://backend: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; } # Django admin / swagger / redoc / i18n / 测试页 location /admin/ { proxy_pass http://backend:8000; proxy_set_header Host $host; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; client_max_body_size 50M; } location /swagger { proxy_pass http://backend:8000; proxy_set_header Host $host; } location /swagger.json { proxy_pass http://backend:8000; proxy_set_header Host $host; } location /redoc/ { proxy_pass http://backend:8000; proxy_set_header Host $host; } location /i18n/ { proxy_pass http://backend:8000; proxy_set_header Host $host; } location /test/ { proxy_pass http://backend:8000; proxy_set_header Host $host; } # 带哈希指纹的构建产物:永久强缓存(immutable) location ~* \assets/.*\.(js|css|png|jpg|jpeg|gif|webp|avif|ico|svg|woff|woff2|ttf|eot)$ { expires 1y; add_header Cache-Control "public, immutable"; access_log off; } # 其他静态资源(public/ 目录:图片/字体等无指纹文件):适中缓存 location ~* \.(png|jpg|jpeg|gif|webp|ico|svg|woff|woff2|ttf|eot)$ { expires 7d; add_header Cache-Control "public"; access_log off; } # index.html 不缓存,保证发版后立即生效 location = /index.html { add_header Cache-Control "no-cache, no-store, must-revalidate"; } # SPA 路由回退(放在最后,避免拦截上面的 API 请求) location / { try_files $uri $uri/ /index.html; } }