Files
OnlineJudge/deploy/nginx/locations.conf
yuetsh 8d6f4616b9 perf(nginx): 修复 JS 未压缩,静态资源加不可变缓存
nginx 1.21.1 起 mime.types 把 .js 标成 text/javascript,而 gzip_types
只写了 application/javascript,导致前端 JS 实际一直在裸传。补全类型列表,
并加上 application/json 让 API 响应也走压缩。

gzip_comp_level 此前用的是默认值 1,改为 6,实测 js+css 总量 3.85M → 3.44M。

/assets/ 下的产物文件名带内容 hash,加 immutable 永久缓存,省掉每次进页面
对 32 个 chunk 的条件请求;index.html 反过来设 no-cache,避免发版后刷不到新版。

location 里出现 add_header 会中断 http 级 add_header 的继承,
两个 block 都重新声明了原有的三个安全头。

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-05 09:11:48 -06:00

60 lines
1.8 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
location /public {
root /data;
}
# WebSocket 支持
location /ws/ {
proxy_pass http://backend;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP __IP_HEADER__;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# WebSocket 超时设置
proxy_connect_timeout 7d;
proxy_send_timeout 7d;
proxy_read_timeout 7d;
}
location /api {
include api_proxy.conf;
}
# location /admin {
# root /app/dist/admin;
# try_files $uri $uri/ /index.html =404;
# }
location /.well-known {
alias /data/ssl/.well-known;
}
# 注意location 里一旦出现 add_headerhttp 级的 add_header 就不再继承,
# 所以下面两个 block 都要把 nginx.conf 里的安全头重新写一遍。
# 构建产物文件名带内容 hash内容一变文件名就变可以永久缓存。
# 命中后浏览器直接读磁盘,不再发条件请求。
location /assets/ {
root /app/dist;
expires 1y;
add_header Cache-Control "public, immutable";
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Frame-Options SAMEORIGIN always;
add_header X-Content-Type-Options nosniff always;
access_log off;
}
location / {
root /app/dist;
try_files $uri $uri/ /index.html =404;
# index.html 引用着带 hash 的文件名,必须每次回源校验,
# 否则发新版后学生刷不到。no-cache 是"缓存但每次校验",命中走 304。
add_header Cache-Control "no-cache";
add_header X-XSS-Protection "1; mode=block" always;
add_header X-Frame-Options SAMEORIGIN always;
add_header X-Content-Type-Options nosniff always;
}