From af4f967ec9ea9938ae4a0f4ab5b098679f6892fd Mon Sep 17 00:00:00 2001 From: hhs <386998068@qq.com> Date: Mon, 18 May 2026 21:32:25 +0800 Subject: [PATCH] vault backup: 2026-05-18 21:32:25 --- hhs/DEV/跨域问题/跨域问题.md | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/hhs/DEV/跨域问题/跨域问题.md b/hhs/DEV/跨域问题/跨域问题.md index 7ec7909..a646363 100644 --- a/hhs/DEV/跨域问题/跨域问题.md +++ b/hhs/DEV/跨域问题/跨域问题.md @@ -169,9 +169,8 @@ server { try_files $uri $uri/ /index.html; } - # --- API 反代 + CORS --- - location /api/ { - # 处理预检请求 + # --- Location 1: OPTIONS 预检请求(直接返回,不通后端)--- + location ~ ^/api/.*$ { if ($request_method = 'OPTIONS') { add_header 'Access-Control-Allow-Origin' '$http_origin'; add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS'; @@ -179,10 +178,12 @@ server { add_header 'Access-Control-Max-Age' 1728000; add_header 'Content-Type' 'text/plain; charset=utf-8'; add_header 'Content-Length' 0; - return 204; # 直接结束预检,不走后端 + return 204; } + } - # 真实请求,注入响应头 + # --- Location 2: 真实请求(反向代理 + CORS 头)--- + location /api/ { add_header 'Access-Control-Allow-Origin' '$http_origin' always; add_header 'Access-Control-Allow-Credentials' 'true' always; add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';