異なるパスに異なるヘッダーを設定するが、常に同じファイルを提供することになる

異なるパスに異なるヘッダーを設定するが、常に同じファイルを提供することになる

場所によって異なるヘッダーを提供したいのですが、一致するファイルが見つからない場合は、常に特定のファイルを提供するようにフォールバックします。

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( )は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ステートメントをまとめて、を使用してmap特定の値を設定します。この答え

関連情報