How to get multiple urls for Access-Control-Allow-Credentials header in nginx configuration?

I use the following configuration for nginxserver:

server {
    .....
    .....

location / {

    if ($request_method = 'OPTIONS') {
       add_header 'Access-Control-Allow-Origin' 'http://www.domain1.com';      
       add_header 'Access-Control-Allow-Credentials' 'true';
       add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
       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' 1728000;
       add_header 'Content-Type' 'text/plain charset=UTF-8';
       add_header 'Content-Length' 0;
       return 204;
    }
    if ($request_method = 'POST') {
       add_header 'Access-Control-Allow-Origin' 'http://www.domain1.com';
   add_header 'Access-Control-Allow-Credentials' 'true';
       add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
       add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
    }
    if ($request_method = 'GET') {
       add_header 'Access-Control-Allow-Origin' 'http://www.domain1.com';
       add_header 'Access-Control-Allow-Credentials' 'true';
       add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
       add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
    }

}

I want to add more domains to the header

'Access-Control-Allow-Origin' 'http://www.domain2.com';

but when i add the code:

if ($http_origin = "https://www.domain1.com") {
          add_header 'Access-Control-Allow-Origin' $http_origin;
    }
if ($http_origin = "https://www.domain2.com") {
          add_header 'Access-Control-Allow-Origin' $http_origin;
    }

he does not work.

+4
source share

Source: https://habr.com/ru/post/1608801/


All Articles