From fb3f1b5d34b247bf406e1955b74097743f1deccd Mon Sep 17 00:00:00 2001 From: knst-kotov Date: Sat, 16 Apr 2022 12:56:00 +0300 Subject: [PATCH] fix CORS config --- nginx.conf.sigil | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/nginx.conf.sigil b/nginx.conf.sigil index fe82c74e..b199feaa 100644 --- a/nginx.conf.sigil +++ b/nginx.conf.sigil @@ -4,6 +4,11 @@ {{ $listen_port := index $port_map_list 1 }} {{ $upstream_port := index $port_map_list 2 }} +map $http_origin $allow_origin { + ~^https?://(.*\.)?localhost:3000|new.discours.io|discours.io()(:\d+)?$ $http_origin; + default ""; +} + {{ if eq $scheme "http" }} server { listen [{{ $.NGINX_BIND_ADDRESS_IP6 }}]:{{ $listen_port }}; @@ -109,12 +114,13 @@ server { {{ if $.PROXY_X_FORWARDED_SSL }}proxy_set_header X-Forwarded-Ssl {{ $.PROXY_X_FORWARDED_SSL }};{{ end }} if ($request_method = 'OPTIONS') { - add_header 'Access-Control-Allow-Origin' '*'; + add_header 'Access-Control-Allow-Origin' '$allow_origin' always; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS'; # # Custom headers and headers various browsers *should* be OK with but aren't # add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range'; + add_header 'Access-Control-Allow-Credentials' 'true'; # # Tell client that this pre-flight info is valid for 20 days # @@ -125,17 +131,19 @@ server { } if ($request_method = 'POST') { - add_header 'Access-Control-Allow-Origin' '*' always; + add_header 'Access-Control-Allow-Origin' '$allow_origin' always; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always; add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always; add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always; + add_header 'Access-Control-Allow-Credentials' 'true' always; } if ($request_method = 'GET') { - add_header 'Access-Control-Allow-Origin' '*' always; + add_header 'Access-Control-Allow-Origin' '$allow_origin' always; add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always; add_header 'Access-Control-Allow-Headers' 'DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range' always; add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range' always; + add_header 'Access-Control-Allow-Credentials' 'true' always; } }