nex_animation/frontend/nginx.conf

40 lines
2.7 KiB
Nginx Configuration File
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

server {
listen 80; server_name _; root /usr/share/nginx/html; index index.html;
location /api/ { proxy_pass http://api:8000/api/; proxy_set_header Host $host; }
location /media/ { proxy_pass http://api:8000/media/; proxy_set_header Host $host; }
location /docs { proxy_pass http://api:8000/docs; }
location /openapi.json { proxy_pass http://api:8000/openapi.json; }
# ---- 交付面(与后端 backend/app/main.py 的 SPA 托管同一套口径,两边不要各自发明)----
# 首屏闭包 1 469 kB 里 1 144 kB 是一个带哈希的 JS chunk:不压就是按 raw 发,
# 已经量好的 gzip 450 kB 白量了(`node scripts/ui/chunkinfo.mjs`)。
# 只压文本,mp4 / 已经压过的图片交给 `gzip_types` 挡在外面。
gzip on; gzip_vary on; gzip_min_length 1024; gzip_comp_level 6;
gzip_types text/plain text/css text/csv application/javascript application/json
application/xml image/svg+xml;
# 反代这一路的分工,三组合实测在 `reports/ui_evidence/nex-p30/nginx-ab.log`:
# · 后端 `app.main` 自己会先按 media type 压 `/api`,nginx 见到已带 `Content-Encoding`
# 的响应直接透传,不叠第二层,上游那条 `Content-Length` 也原样保住(C 组合);
# · 后端没压时这里兜底压上:A 组合(上游=阶段 29 的 main.py)`/api/projects`
# 341 909 → 53 333 B 就是 nginx 自己压的。
# 权威的那张文本族名单在后端 `app.main.gzip_eligible`,这里这份是兜底,
# 两边都只认 media type、不认路径前缀。
# `gzip_proxied` 管的是**带 `Via` 请求头的请求**(nginx 站在另一层代理/CDN 后面时),
# 跟「响应是不是来自上游」无关:默认 `off` 下直连浏览器照样压 —— A(无本行)与 B(有本行)
# 六个样本逐字段相同。开它是为了挂 CDN 时这套压缩不整块失效,
# 别把它当成「`/api` 有压缩」的原因,那个原因在上一段。
gzip_proxied any;
# Vite 的产物名带内容哈希 ⇒ 缓存一年;index.html 反过来必须每次回源校验,
# 否则发版后老标签页拿着旧壳去引已经不存在的哈希文件,用户看到白屏。
location /assets/ {
# 只留 add_header 这一条:再写 `expires 1y;` 会额外发一条 Cache-Control,
# 语法上合法(RFC 9110 允许逗号合并),但两个来源迟早会各改一半。
add_header Cache-Control "public, max-age=31536000, immutable" always;
try_files $uri =404;
}
location = /index.html { add_header Cache-Control "no-cache" always; }
# history 回落发出去的也是壳,别让它被缓存住。
location / { add_header Cache-Control "no-cache" always; try_files $uri $uri/ /index.html; }
}