# nginx-cache.conf —— 源站 Nginx 配置样例(缓存 + 压缩 + 404) # 用法:合并进 server { } 块(或 include),nginx -t 通过后 reload。 # 说明: # - 静态资源命中/location ^~ 规则,浏览器缓存 1 年 + immutable; # 站内已有 ?v= 版本串,发布新版改版本号即可,无需清缓存。 # - HTML 走默认,每次校验。 # - 压缩:Cloudflare 已在边缘做 Brotli,源站开 gzip 即可(出口流量同样省)。 # - 404:返回品牌化 404.html(与本包 02-SEO基建/404.html 配套)。 server { listen 80; server_name henhaoxue.com; # ---------- 压缩 ---------- gzip on; gzip_comp_level 5; gzip_min_length 1024; gzip_proxied any; gzip_vary on; gzip_types text/html text/css text/plain application/javascript application/x-javascript application/json image/svg+xml font/woff2; # ---------- 静态资源:1 年长缓存 ---------- location ~* \.(?:css|js|mjs|webp|png|jpe?g|gif|svg|ico|woff2?|ttf|otf)$ { expires 1y; add_header Cache-Control "public, max-age=31536000, immutable"; access_log off; try_files $uri =404; } # ---------- 内容 JSON:1 天 ---------- location ~* ^/learning/content/.+\.json$ { expires 1d; add_header Cache-Control "public, max-age=86400"; try_files $uri =404; } # ---------- 动态/HTML:每次校验 ---------- location / { add_header Cache-Control "public, max-age=0, must-revalidate"; # 反代到应用服务时: # proxy_pass http://127.0.0.1:8080; # proxy_set_header Host $host; # proxy_set_header X-Real-IP $remote_addr; } # ---------- 404 ---------- error_page 404 /404.html; location = /404.html { internal; # 只作错误页,不直接访问 add_header Cache-Control "no-store"; } # ---------- 安全头(可选) ---------- add_header X-Content-Type-Options "nosniff" always; add_header Referrer-Policy "strict-origin-when-cross-origin" always; }