nginxで特定IP以外からのアクセスにメンテ表示する方法メモ
1)メンテ用confファイルを作成
仮に /etc/nginx/function/mainte.conf とします。
if ($uri = '/mainte.html') { set $is_allow_ip 1;} ←mainte.htmlへのリクエストは許可しないとループしちゃう if ($remote_addr = '127.0.0.1') { set $is_allow_ip 1;} ←許可IPを書いていく if ($remote_addr = 'xxx.xxx.xxx.xxx') { set $is_allow_ip 1;} if ($remote_addr = 'xxx.xxx.xxx.xxx') { set $is_allow_ip 1;} if ($is_allow_ip != '1') { rewrite ^(.*)$ http://hogefuga/mainte.html? redirect; }
※ELB配下などの場合は
if ($http_x_forwarded_for = 'xxx.xxx.xxx.xxx') { set $is_allow_ip 1;}
で。
2)nginxのconfファイルから上記mainte.confをincludeする
server { …… include /etc/nginx/function/mainte.conf; …… }
メンテ解除は上記をコメントするか、またはincludeしているファイルの中身を消すなどで。