3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ELB + EC2 + nginxでメンテナンスページを出す

Posted at

nginxの設定で詰まったのでメモ。
特定IPのみサイトアクセスを許可し、それ以外のIPには許可しない設定をここでは行う。

ELBを利用していない場合

remote_addrで接続元IPが取得できるので、それで判定する。
書き方例は以下

  ### Start - Show the maintenance page.
   error_page 503 @maintenance;
   location @maintenance {rewrite ^(.*)$ /maintenance.html break;}
   set $flg "true";
   if ($remote_addr = "127.0.0.1" ) {set $flg "false";}
   if ($remote_addr ~ ^192\.168\. ) {set $flg "false";}
   if ($remote_addr = "111.111.111.111"   ) {set $flg "false";}
   if ($request_uri ~ .*\.(jpg|txt|JPG|gif|GIF|png|PNG|swf|SWF|css|CSS|js|JS|inc|INC|ico|ICO)){set $flg "false";}
   if ($flg = "true") {return 503;}
  ###  End  - Show the maintenance page.

ELBを利用している場合

ELBを利用している場合は、
remote_addrはELBのIPが返ってくる。
http_x_forwarded_forが実際の接続元IPとなることに注意する。
書き方例は以下

  ### Start - Show the maintenance page.
   error_page 503 @maintenance;
   location @maintenance {rewrite ^(.*)$ /maintenance.html break;}
   set $flg "true";
   if ($remote_addr = "127.0.0.1" ) {set $flg "false";}
   if ($remote_addr ~ ^192\.168\. ) {set $flg "false";}
   if ($http_x_forwarded_for = "111.111.111.111"   ) {set $flg "false";}
   if ($request_uri ~ .*\.(jpg|txt|JPG|gif|GIF|png|PNG|swf|SWF|css|CSS|js|JS|inc|INC|ico|ICO)){set $flg "false";}
   if ($flg = "true") {return 503;}
  ###  End  - Show the maintenance page.
3
1
0

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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?