Server/Nginx

nginx CORS 처리

web data 2025. 2. 26. 19:33

 

nginx 에 특정 도메인에 대해 CORS 를 오픈해줄때 (mydomain.com)

        .
        .
        location ^~ /emoticons {

            # CORS 헤더 추가
            add_header 'Access-Control-Allow-Origin' 'https://mydomain.com' always;
            add_header 'Access-Control-Allow-Methods' 'GET' always;
            add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type' always;

            # OPTIONS 요청에 대한 처리
            if ($request_method = 'OPTIONS') {
                add_header 'Access-Control-Allow-Origin' 'https://mydomain.com';
                add_header 'Access-Control-Allow-Methods' 'GET';
                add_header 'Access-Control-Allow-Headers' 'Authorization,Content-Type';
                add_header 'Content-Length' '0';
                add_header 'Content-Type' 'text/plain charset=UTF-8';
                add_header 'Access-Control-Max-Age' 1728000;
                return 204;
            }
        }
        .
        .

 

이후 nginx 설정 테스트 후 재시작

nginx -t
service nginx restart

 


끝.