server { listen 80; server_name localhost; charset utf-8; # 访问日志 (生产环境可关闭 access_log 减少 IO) access_log /var/log/nginx/access.log; error_log /var/log/nginx/error.log warn; # 站点根目录 root /usr/share/nginx/html; index index.html; # ============================================================ # SPA 路由处理:所有非静态资源请求返回 index.html # 因为前端使用 hash router,这里主要是兜底 # ============================================================ location / { try_files $uri $uri/ /index.html; } # ============================================================ # 静态资源缓存策略 # /assets/ 下的文件经过 Vite 处理带有 content hash,可长期缓存 # ============================================================ location /assets/ { expires 1y; add_header Cache-Control "public, immutable"; access_log off; } # ============================================================ # index.html 禁止缓存(确保前端更新后立即生效) # ============================================================ location = /index.html { add_header Cache-Control "no-cache, no-store, must-revalidate"; add_header Pragma "no-cache"; add_header Expires 0; } # ============================================================ # Gzip 压缩 # ============================================================ gzip on; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_min_length 1024; gzip_types text/plain text/css text/xml text/javascript application/javascript application/json application/xml image/svg+xml; # 隐藏 Nginx 版本号 server_tokens off; }