CORS

Примеры настройки CORS у Nginx ( Пример не истина просто для понимания)

http {
    map $http_origin $allow_origin {
        ~^https://(.*\.)?test1.ru(:\d+)?$ $http_origin;
        ~^https?://(.*\.)?test3.com(:\d+)?$ $http_origin;
        default "";
    }
    server {
        location /dynamicads/feeds {
                if ($request_method = 'OPTIONS') {
                        add_header 'Access-Control-Allow-Origin' $allow_origin always;
                        add_header 'Access-Control-Allow-Methods' 'GET, OPTIONS' always;
                        add_header 'Access-Control-Allow-Headers' 'authorization,access-control-allow-origin,access-control-allow-headers,Content-Type' always;
                        add_header 'Access-Control-Max-Age' 1000;
                        add_header 'Content-Type' 'text/plain; charset=utf-8';
                        add_header 'Content-Length' 0;
                        add_header 'Access-Control-Allow-Credentials' 'true';
                        return 204;
                }
                if ($request_method = 'GET') {
                        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' 'access-control-allow-headers,Origin,DNT,Authorization,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;
                }
                if ($request_method = 'POST') {
                        add_header 'Access-Control-Allow-Origin' '*' always;
                        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS' always;
                        add_header 'Access-Control-Allow-Headers' 'access-control-allow-headers,Origin,DNT,Authorization,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;
                }
                proxy_set_header Accept-Encoding "";
                proxy_pass https://127.0.0.1:8000/check;
        }
    }
}

Проверка CORS c помощью curl 

curl -I --request GET 'https://example.com' --header 'Origin: https://sd.test.com'
HTTP/2 200
server: nginx/1.18.0
date: Fri, 08 Jul 2022 11:45:33 GMT
content-type: text/xml
content-length: 26299
accept-ranges: bytes
etag: "df5a9f6ce6ac17508538e4ab1680eb32"
last-modified: Fri, 08 Jul 2022 10:25:27 GMT
x-amz-request-id: 46748052351fced1
x-amz-version-id: null
access-control-allow-origin: https://sd.test.com
access-control-allow-methods: GET, POST, OPTIONS
access-control-allow-headers: access-control-allow-headers,Origin,DNT,Authorization,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range
access-control-expose-headers: Content-Length,Content-Range

Добавить комментарий 0