nginxでproxyしてるときにカスタムのエラーページを表示する

結論としては、proxy_intercept_errors onを使う。

nginxで、error_pageディレクティブを指定するとカスタムのエラーページを表示できる。
でも、下記のような書き方だと、proxy先の返すhttp statusまで見てエラーページが表示されたりまではしない。

server {
  listen 80;
  server_name example.com;

  access_log /var/log/nginx/access.log ltsv;
  error_log /var/log/nginx/error.log;

  root /var/www/html/htdocs;

  location / {
    if (!-f $request_filename) {
      proxy_pass http://backends;
    }
  }

  error_page 404 /error/404.html;
  error_page 400 401 403 500 501 502 503 /error/503.html;

proxy_intercept_errorsを設定すれば、proxy先のstatusも見て、カスタムのエラーページを表示することができた。

server {
  listen 80;
  server_name example.com;

  access_log /var/log/nginx/access.log ltsv;
  error_log /var/log/nginx/error.log;

  root /var/www/html/htdocs;

  location / {
    if (!-f $request_filename) {
      proxy_intercept_errors on;
      proxy_pass http://backends;
    }
  }

  error_page 404 /error/404.html;
  error_page 400 401 403 500 501 502 503 /error/503.html;