server { listen 3000; # 设置根目录和默认文件(全局设置,无需在location中重复) root /usr/share/nginx/html; index index.html index.htm; # 设置最大请求体大小 client_max_body_size 100M; # 设置正确的 MIME 类型(server级别) types { text/html html htm; text/css css; application/javascript js; application/json json; text/xml xml; image/svg+xml svg; image/x-icon ico; image/png png; image/jpeg jpg jpeg; video/mp4 mp4; video/webm webm; video/ogg ogg; } # gzip压缩配置 gzip on; gzip_static on; # 优先使用预压缩文件(.gz) gzip_vary on; gzip_types text/plain text/css application/json application/javascript text/xml application/xml image/svg+xml; gzip_min_length 1024; gzip_comp_level 6; gzip_buffers 16 8k; # 处理视频文件 - 优先级最高,避免被其他规则拦截 location ~* \.(mp4|webm|ogg|avi|mov)$ { # 启用范围请求支持 add_header Accept-Ranges bytes; # 视频文件缓存策略 - 30天 add_header Cache-Control "public, max-age=2592000, immutable"; } # 处理其他静态资源(JS、CSS、图片等)长缓存策略 - 1年 location ~* \.(js|css|json|xml|svg|png|jpg|jpeg|ico|ttf|woff|woff2)$ { add_header Cache-Control "public, max-age=31536000, immutable"; } # 请自行添加代理配置 # 主应用路由 - 放在最后,作为默认fallback location / { # SPA应用的智能回退:原始文件 -> gzip文件 -> 目录 -> index.html try_files $uri $uri.gz $uri/ /index.html; # 页面文件不缓存,确保版本更新检测正常工作 add_header Cache-Control "no-cache, no-store, must-revalidate"; add_header Pragma "no-cache"; add_header Expires "0"; } # 全局错误页面配置 error_page 404 /index.html; error_page 500 502 503 504 /index.html; }