서로 다른 경로에 대해 서로 다른 헤더를 구성하지만 항상 동일한 파일을 제공하게 됩니다.

서로 다른 경로에 대해 서로 다른 헤더를 구성하지만 항상 동일한 파일을 제공하게 됩니다.

다른 위치에 대해 다른 헤더를 제공하고 싶지만 항상 특정 파일을 제공하도록 대체하고 싶습니다(일치하는 파일을 찾을 수 없는 경우).

또한 모든 요청에 ​​추가해야 하는 전역 헤더도 있습니다. 단, 해당 레벨에 이미 추가된 경우 추가하지 않기를 바랍니다 location.

나는 .template.conf지금까지 이것을 가지고 있습니다 :

server {   
   listen   ${PORT};
   # Don't include any local server address info in redirects
   absolute_redirect off;

   index    index.html;

   root ${HTML_SRC};

   include  ${INCLUDE_DIR}/*.conf;
    
   # Serve any matching requested file (CSS, etc.) fallbacking to /index.html
   location / {
      # First try a matching existing file, then just show index.html
      try_files     $uri $uri/ /index.html;
   }
}

include(in ) 은 INCLUDE_DIR/headers.conf다음과 같이 정의됩니다.

# Global headers
add_header  X-FRAME-OPTIONS "DENY"  always;
add_header  Cache-Control   "no-store, must-revalidate" always;
add_header  Report-To   "<global-config>" always;
add_header  Content-Security-Policy "<global-config>" always;

# Location specific paths

# Specific known existing file in /$HTML_SRC/.well-known
location /.well-known/apple-app-site-association$ {
    default_type    application/json;
    # Redirect everything to fallback / location to serve the file
    rewrite .* / last;
}

# Assets
location ~* \.(jpg|jpeg|gif|png|svg|ttf)$ {
    # Redeclare global headers (not inherited)
    add_header  X-FRAME-OPTIONS "DENY"  always;
    add_header  Report-To   "<global-config>" always;
    add_header  Content-Security-Policy "<global-config>" always;
    # Cache assets for a bit longer
    add_header  Cache-Control   "max-age=31557600";
    # Redirect everything to fallback / location to serve any files file
    rewrite .* / last;
}

location = /login {
    # Redeclare global headers (not inherited)
    add_header  X-FRAME-OPTIONS "DENY"  always;
    add_header  Cache-Control   "no-store, must-revalidate" always;
    # Path specific CSP headers
    add_header  Report-To   "<specific-config>" always;
    add_header  Content-Security-Policy "<specific-config>" always;
    # Redirect everything to /
    rewrite .* / last;
}

# All locations with this prefix have these headers
location /some-other-path {
    # Redeclare global headers (not inherited)
    add_header  X-FRAME-OPTIONS "DENY"  always;
    add_header  Cache-Control   "no-store, must-revalidate" always;
    # Path specific CSP headers
    add_header  Report-To   "<other-specific-config>" always;
    add_header  Content-Security-Policy "<other-specific-config>" always;
    # Redirect everything to /
    rewrite .* / last;
}

location나는 모든 add_header최상위 수준(이 경우 server템플릿에서) add_header을 의미한다는 것을 알고 있습니다 .상속되지 않습니다.

위의 문제는 제대로 작동하지 않는다는 것입니다! 특정 location헤더가 항상 사용되는 것은 아닙니다. 그게 문제인 것 같지만 (템플릿 하나와 일치) rewrite시도했지만 성공하지 못했습니다. 나는 또한 행운 없이 블록 try_files에 추가 헤더를 제외하고는 아무것도 갖지 않으려고 노력했습니다 . location나도 내가 하고 있는 일이 최선의 방법은 아니라고 생각하는데...

추가된 헤더를 단순화했으며 CSP 헤더가 포함되어 있습니다. 예를 들어 다른 전역 헤더와 기타 위치별 헤더가 있습니다. 다른 헤더가 필요한 다른 경로도 있지만 위에 있는 내용은 해당 경로에 적용할 수 있는 일반적인 솔루션으로 해결할 수 있다고 생각합니다.

답변1

rewrite...lastNginx가새로운 위치를 검색하다요청을 처리합니다. 최종 location응답 헤더만 설정됩니다.

location응답 헤더를 설정하는 동일한 블록 내에서 전체 요청을 처리할 수 있습니다 rewrite...break.사용하여try_files다음과 같이:

try_files /index.html =404;

/index.html용어는 다음과 같습니다.~ 아니다마지막 매개변수. 마지막 매개변수인 경우 Nginx는 요청을 처리할 새 위치를 검색하게 됩니다.


또는 모든 add_header문을 함께 유지하고 a를 사용하여 map특정 값을 설정하세요. 보다이 답변.

관련 정보