場所によって異なるヘッダーを提供したいのですが、一致するファイルが見つからない場合は、常に特定のファイルを提供するようにフォールバックします。
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...last
Nginxは新しい場所を探すリクエストを処理します。最後にのみ、location
応答ヘッダーを設定できます。
location
レスポンスヘッダーを設定する同じブロック内でリクエスト全体を処理することも、rewrite...break
もっと簡単に言えば使用してtry_files
次のように:
try_files /index.html =404;
この/index.html
用語はない最後のパラメータ。これが最後のパラメータだった場合、Nginx はリクエストを処理するための新しい場所を検索します。
あるいは、すべてのadd_header
ステートメントをまとめて、を使用してmap
特定の値を設定します。この答え。