LoginSignup
5

More than 5 years have passed since last update.

nginxでメンテナンスページを表示させる。改良版

Posted at

はじめに

以前書いた記事で、「nginxでメンテナンスページを表示させる。」というのがあるのですが、もっと良いやり方があったので改めて書きます。

設定

とりあえず設定内容です。今回もmaintenanceというファイルをincludeしている前提で進めます。

maintenance
error_page 500 @maintenance;

# Set Env
set $maintenance "on";

# Allow Paths
if ($uri ~ "^/exclude/") {
  set $maintenance "off";
}

# Allow IPs
if ($remote_addr ~ "xxx.xxx.xxx.xxx") {
  set $maintenance "off";
}
if ($http_x_forwarded_for ~ "xxx.xxx.xxx.xxx") {
  set $maintenance "off";
}

# maintenance
if ($maintenance = "on") {
  return 500;
}
location @maintenance {
  rewrite ^(.*)$ /maintenance.html break;
}
nginx.conf(追記)
# include /etc/nginx/conf.d/maintenance;
rewrite ^/maintenance.html(.*)$ https://$host/ permanent

説明

まず初めの1行目のerror_page 500 @maintenance;というところでリターンコード500の場合@maintenanceというところへリライトするように設定しています。
このリライト先というのが一番下のlocationブロックです。
次にset $maintenance "on";で変数$maintenanceonという値をセットしておきます。
以下の部分ではif文を利用し、Allow Pathsの部分では特定パスへのアクセスに対して$maintenanceoffに、Allow IPsの部分では特定IPからのアクセスに対し$maintenanceoffを設定し直すように記載されています。
そして最後のところで$maintenanceonならリターンコードとして500を返すようにしています。
そうすることでメンテナンスを有効にしてもメンテナンスページへリダイレクトされるのではなくメンテナンスページのリライトで済みます。解除すると先ほどアクセスしていたパスが表示されます。

また、nginx.confの方ではinclude /etc/nginx/conf.d/maintenance;をコメントアウト(#)付きで記載しておき、#の付け外しで制御するようにしています。
また、その下のrewriteルールでmaintenance.htmlへのアクセスは無条件にサイトのトップへリダイレクトさせることで何か間違ってメンテナンスページへ直接アクセスしてしまった相手に対しての対処もしています。

おわりに

リダイレクトとリライトをうまく駆使することがポイントだなと感じました。

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5