nginx의 여러 위치에 CORS 헤더를 추가하는 방법은 무엇입니까?

nginx의 여러 위치에 CORS 헤더를 추가하는 방법은 무엇입니까?

다음에 제공된 코드를 사용하여 nginx 서버에 기본 CORS 지원을 추가할 수 있었습니다.활성화-cors.org. 그러나 이 솔루션은 모든 위치 블록에 해당 코드를 복사하여 붙여넣는 것을 의미하며 다음과 같은 여러 위치가 있습니다.

location /game1 {
    alias   /development/games/game1/output;
    index  index.html;
}

location /game2 {
    alias   /development/games/game2/output;
    index  index.html;
}

add_header블록 외부를 포함하는 규칙을 만드는 방법이 있나요 location?

명확하게 편집: 나는 다음을 시도했습니다

server {
    listen       80;
    server_name  localhost;

    charset UTF-8;

    #access_log  logs/host.access.log  main;

    if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        ...
    }
    location /game1 {
        alias /development/games/game1/output;
    }
    ...
}

하지만 작동하지 않습니다.

2015/10/14 19:00:01 [emerg] 3464#7384: "add_header" directive is not allowed
here in C:\development\servers\nginx-1.7.9/conf/nginx.conf:43

답변1

add_header물론, 블록 아래에 추가할 수 있지만 server그렇게 되면언제나당신이 원하는 것이 아닐 수도 있습니다.

그렇지 않으면 원하는 지시어로 파일을 만든 다음 CORS 헤더를 추가하려는 include각 위치에서 파일을 만들 수 있습니다.location

관련 정보