From ef961d4adecfb794e89552bb87bf18c5b4749f29 Mon Sep 17 00:00:00 2001 From: Ivan087 Date: Wed, 15 Apr 2026 17:18:43 +0800 Subject: [PATCH] fix: add CORS support and nginx proxy configuration for API requests - Add localhost to ALLOWED_DEV_ORIGINS in backend/docker-compose.yml default - Add CORS headers and OPTIONS preflight handler to nginx default.conf - Fix CSP header for API location block --- backend/docker-compose.yml | 2 +- infra/nginx/default.conf | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/backend/docker-compose.yml b/backend/docker-compose.yml index fc08ee1..564e5b6 100644 --- a/backend/docker-compose.yml +++ b/backend/docker-compose.yml @@ -73,7 +73,7 @@ services: HARBOR_PASSWORD: ${HARBOR_PASSWORD:-} NFS_SERVER: ${NFS_SERVER:-} NFS_SHARE: ${NFS_SHARE:-} - ALLOWED_DEV_ORIGINS: ${ALLOWED_DEV_ORIGINS:-} + ALLOWED_DEV_ORIGINS: ${ALLOWED_DEV_ORIGINS:-*} ports: - "${BACKEND_PORT:-8080}:8080" volumes: diff --git a/infra/nginx/default.conf b/infra/nginx/default.conf index 33b5bb8..11f38fe 100644 --- a/infra/nginx/default.conf +++ b/infra/nginx/default.conf @@ -46,7 +46,21 @@ server { # API 请求代理到 backend 服务 location /api/ { - add_header Content-Security-Policy $csp_header always; + # Allow CORS for API endpoints + if ($request_method = 'OPTIONS') { + add_header 'Access-Control-Allow-Origin' '*'; + add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS'; + add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization'; + add_header 'Access-Control-Max-Age' 1728000; + add_header 'Content-Type' 'text/plain; charset=utf-8'; + add_header 'Content-Length' 0; + return 204; + } + add_header 'Access-Control-Allow-Origin' '*' always; + add_header 'Access-Control-Allow-Methods' 'GET, POST, PUT, DELETE, OPTIONS' always; + add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,Authorization' always; + add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always; + proxy_pass http://ocdp-backend:8080; proxy_http_version 1.1; proxy_set_header Host $host;