4747 # HTTP → HTTPS 重定向
4848 server {
4949 listen 80 ;
50+ # 公网 IP 保留在 server_name:用于 IP 直连访问/域名验证场景(如 CDN 归属验证、
51+ # 证书签发前的连通性测试)命中本 server 块而非默认块;删除会导致此类请求落入
52+ # 错误 server 块,故保留不删
5053 server_name entropydecrease.com www.entropydecrease.com 121.40.24.242 ;
5154
5255 # 域名归属验证与 ACME http-01 挑战必须在 HTTP 下直接可达:
@@ -102,12 +105,10 @@ http {
102105 ssl_session_cache shared:SSL:10m ;
103106 ssl_session_timeout 10m ;
104107
105- # 安全头
106- add_header X-Frame-Options "SAMEORIGIN" always;
107- add_header X-Content-Type-Options "nosniff" always;
108- add_header X-XSS-Protection "1; mode=block" always;
109- add_header Referrer-Policy "strict-origin-when-cross-origin" always;
110- add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
108+ # 安全头(统一片段,含 HSTS/X-Frame-Options/nosniff/Referrer-Policy/CSP)
109+ # 注意:下方任何自带 add_header 的 location 都不会继承此处,
110+ # 必须各自 include 该片段(见 conf.d/security_headers.conf 头部说明)
111+ include conf.d/security_headers.conf;
111112
112113 # AI 网关健康检查(转发到 ai-gateway,不可用时降级为 Nginx 自检)
113114 location /health {
@@ -131,7 +132,10 @@ http {
131132 # 健康检查降级 fallback(ai-gateway 不可达时由 Nginx 直接响应)
132133 # 命名 location 不继承 server 级 add_header,且不经过 FastAPI CORSMiddleware,必须自带 CORS 头
133134 location @health_fallback {
134- add_header Access-Control-Allow-Origin "*" always;
135+ # 命名 location 内出现 add_header 即不再继承 server 级安全头,需显式 include
136+ include conf.d/security_headers.conf;
137+ # CORS 收敛为官网主域(与 CORS_ORIGINS 一致),不再允许任意来源
138+ add_header Access-Control-Allow-Origin "https://entropydecrease.com" always;
135139 add_header Access-Control-Allow-Methods "GET, POST, OPTIONS" always;
136140 add_header Access-Control-Allow-Headers "Authorization, Content-Type" always;
137141 return 200 '{"status":"degraded","service":"ai-gateway","version":"unknown","error":"gateway unreachable"}' ;
@@ -152,6 +156,8 @@ http {
152156 proxy_read_timeout 300s ;
153157 # SSE 必需配置:禁用缓存
154158 proxy_cache off;
159+ # 本 location 自带 add_header,不再继承 server 级安全头,需显式 include
160+ include conf.d/security_headers.conf;
155161 add_header X-Accel-Buffering no;
156162 }
157163
@@ -255,10 +261,13 @@ http {
255261 limit_conn dl_total 64 ;
256262
257263 # 更新元数据(latest.yml / latest.json)禁止缓存,
258- # 否则客户端会长期读到旧版本信息;允许 www 子域跨域读取
264+ # 否则客户端会长期读到旧版本信息;仅允许官网域跨域读取
259265 location ~ * \.(yml|json)$ {
266+ # 本 location 自带 add_header,不再继承 server 级安全头,需显式 include
267+ include conf.d/security_headers.conf;
260268 add_header Cache-Control "no-cache" always;
261- add_header Access-Control-Allow-Origin "*" always;
269+ # CORS 收敛为官网主域(www 已 301 至主域,无需额外放行)
270+ add_header Access-Control-Allow-Origin "https://entropydecrease.com" always;
262271 }
263272 }
264273
@@ -274,15 +283,19 @@ http {
274283 location ^~ /pwa/assets/ {
275284 root /usr/share/nginx;
276285 expires 365d ;
286+ # 本 location 自带 add_header,不再继承 server 级安全头,需显式 include
287+ include conf.d/security_headers.conf;
277288 add_header Cache-Control "public, immutable" ;
278289 access_log off;
279290 }
280291 location = /pwa/manifest.webmanifest {
281292 root /usr/share/nginx;
293+ include conf.d/security_headers.conf;
282294 add_header Cache-Control "no-cache" always;
283295 }
284296 location = /pwa/sw.js {
285297 root /usr/share/nginx;
298+ include conf.d/security_headers.conf;
286299 add_header Cache-Control "no-cache" always;
287300 }
288301
@@ -301,6 +314,8 @@ http {
301314 location /_next/static/ {
302315 root /usr/share/nginx/html;
303316 expires 365d ;
317+ # 本 location 自带 add_header,不再继承 server 级安全头,需显式 include
318+ include conf.d/security_headers.conf;
304319 add_header Cache-Control "public, immutable" ;
305320 access_log off;
306321 }
@@ -309,6 +324,8 @@ http {
309324 location ~ * \.(png|jpg|jpeg|gif|ico|svg|webp|woff|woff2|ttf)$ {
310325 root /usr/share/nginx/html;
311326 expires 30d ;
327+ # 本 location 自带 add_header,不再继承 server 级安全头,需显式 include
328+ include conf.d/security_headers.conf;
312329 add_header Cache-Control "public" ;
313330 access_log off;
314331 }
0 commit comments