taiyoh's memorandum

@ttaiyoh が、技術ネタで気づいたことを書き溜めておきます。

nginxでステータスコード404,500,503時に特定の静的なファイルを表示する

 というメモ。

    server {
        listen       80;
        server_name  example.com;

        root /path/to/docroot;
        access_log  logs/example.access.log  main;

        location ~ ^/(js/|img/|css/|swf/) {
            index  index.html index.htm;
        }

        error_page 404  /404.html;
        error_page 500  /500.html;
        error_page 503  /503.html;

        location ~ /(404|503|500).html {
        }

        location / {
            proxy_intercept_errors on;
            proxy_set_header       Host $host;
            proxy_pass   http://127.0.0.1:5000;
        }
    }

 ミソは"proxy_intercept_errors"で、アプリケーションのレスポンスから特定のエラーコードを検知した時、システムではなくnginx側で所定の表示内容を出力するというフラグを立てる。