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
This commit is contained in:
Ivan087
2026-04-15 17:18:43 +08:00
parent 29d0310f03
commit ef961d4ade
2 changed files with 16 additions and 2 deletions

View File

@ -73,7 +73,7 @@ services:
HARBOR_PASSWORD: ${HARBOR_PASSWORD:-} HARBOR_PASSWORD: ${HARBOR_PASSWORD:-}
NFS_SERVER: ${NFS_SERVER:-} NFS_SERVER: ${NFS_SERVER:-}
NFS_SHARE: ${NFS_SHARE:-} NFS_SHARE: ${NFS_SHARE:-}
ALLOWED_DEV_ORIGINS: ${ALLOWED_DEV_ORIGINS:-} ALLOWED_DEV_ORIGINS: ${ALLOWED_DEV_ORIGINS:-*}
ports: ports:
- "${BACKEND_PORT:-8080}:8080" - "${BACKEND_PORT:-8080}:8080"
volumes: volumes:

View File

@ -46,7 +46,21 @@ server {
# API 请求代理到 backend 服务 # API 请求代理到 backend 服务
location /api/ { 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_pass http://ocdp-backend:8080;
proxy_http_version 1.1; proxy_http_version 1.1;
proxy_set_header Host $host; proxy_set_header Host $host;