From 6bf98b5d7cba25e1ca2b29bc65c69d4bed1855af Mon Sep 17 00:00:00 2001 From: Alan Date: Wed, 10 Apr 2024 12:10:45 +0300 Subject: [PATCH] fix cors --- docker/nginx/nginx.conf | 32 ++++++++++++++++++++++---------- 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/docker/nginx/nginx.conf b/docker/nginx/nginx.conf index c460156..579e126 100644 --- a/docker/nginx/nginx.conf +++ b/docker/nginx/nginx.conf @@ -6,23 +6,25 @@ server { listen 80; + location ~* \.(eot|ttf|woff|woff2)$ { + add_header Access-Control-Allow-Origin *; + } location ~ /api/ { - proxy_pass http://docker_fastapi; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_set_header X-Forwarded-Host $server_name; - add_header 'Access-Control-Allow-Origin' '*'; - add_header 'Access-Control-Allow-Methods' 'GET, POST, HEAD, OPTIONS'; - if ($request_method = 'OPTIONS') { - add_header 'Access-Control-Allow-Credentials' 'true'; - add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type'; - add_header 'Access-Control-Max-Age' 57542400; - add_header 'Content-Type' 'text/plain charset=UTF-8'; - add_header 'Content-Length' 0; - return 204; + if ($http_referer ~* ^(http?\:\/\/)(.*?)\/(.*)$) { + set $ref $1$2; + } + add_header 'Access-Control-Allow-Origin' $ref always; + add_header 'Access-Control-Allow-Credentials' 'true' always; + add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always; + add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,signature,timestamp' always; + add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always; + } @@ -30,6 +32,16 @@ server { root /usr/share/nginx/html; index index.html index.htm; try_files $uri $uri/ /index.html; + if ($http_referer ~* ^(http?\:\/\/)(.*?)\/(.*)$) { + set $ref $1$2; + } + add_header 'Access-Control-Allow-Origin' $ref always; + add_header 'Access-Control-Allow-Credentials' 'true' always; + add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS, PUT, DELETE' always; + add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range,signature,timestamp' always; + add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always; + + } error_page 500 502 503 504 /50x.html;