nginx.conf の return ステートメントが、レンダリングされる実際のファイルではなく、プレーンテキスト ファイルの場所をブラウザーに返すのはなぜでしょうか?

nginx.conf の return ステートメントが、レンダリングされる実際のファイルではなく、プレーンテキスト ファイルの場所をブラウザーに返すのはなぜでしょうか?

環境:Node.js の

Nginx でエラー処理を実験していますが、ファイルを返すことができません。

下記の簡略化された例では、ngxinx.confhttprequest_methodでない場合GET、サーバーは 405.html エラー ページを返すようにしています。HEADPOST

期待される出力:405.html がブラウザに送信されます。

実際の出力:このプレーンテキストはブラウザに送信されます。 http://www.example.com/html/405.html

注: これを Postman でテストしているので、さまざまな HTTP メソッドをサーバーに送信できる追加の拡張機能を Chrome にインストールする必要はありません。

私の設定の関連部分:

server {

    include conf.d/listen-80;

    server_name example.com www.example.com;

    if ($request_method !~ ^(GET|HEAD|POST)$) {
        return 405 $scheme://www.example.com/html/405.html;
    }

    return 301 https://www.example.com$request_uri;

}

server {

    include conf.d/listen-443;

    server_name example.com;

    include conf.d/letsencrypt;

    if ($request_method !~ ^(GET|HEAD|POST)$) {
        return 405 $scheme://www.example.com/html/405.html;
    }

    return 301 https://www.example.com$request_uri;

}

server {

    include conf.d/listen-443;

    server_name www.example.com;

    include conf.d/letsencrypt;

    if ($request_method !~ ^(GET|HEAD|POST)$) {
        return 405 $scheme://www.example.com/html/405.html;
    }

    root /srv/example/views/public;

    location ~* \.(htm|html)$ {
        include conf.d/content-security-policy-html-rendered;
        include conf.d/cache-control-30-days;
        include conf.d/security-headers-html-rendered;
    }
}

答え1

後の URL は、return nnnリダイレクト コード (301、302、303、307、および 308 ステータス コード) のターゲット URL としてのみ扱われます。ドキュメントでは、コードが他のものである場合に何が行われるかは明確に説明されていません。

あなたのケースでエラー ページを取得するには、以下を使用します。

if ($request_method !~ ^(GET|HEAD|POST)$) {
    return 405;
}

error_page 405 /html/405.html;

/html/405.htmlこれにより、ステータス コード 405 が返されたときに送信するように nginx に指示します。

関連情報