SmallStyle


2012-03-05

_ nginx でメンテナンス画面を表示する方法

アプリケーションサーバーをメンテナンスするために,フロントの nginx でメンテナンス中の 503 画面を出すために,以下のような設定を定義してみた.(設定はそれっぽく抜粋しただけ.)

server {
  listen 80;
  server_name example.com;
  
  root /var/www/example.com/public/;
  index index.html;
  
  error_page 503 @maintenance;
  location @maintenance {
    rewrite ^(.*)$ /503.html break;
  }
  
  location / {
    # For maintenance mode
    if ($request_addr != "127.0.0.1") {
      return 503;
    }
    
    if (!-f $request_filename) {
      proxy_pass http://app;
      break;
    }
  }
}

よくあるケースなのでググればいくらでもサンプルは出てくるのですが,実際にやってみたところ,確かに GET でのリクエストでは 503 を返してくれるのですが, POST とかでリクエストをすると 405 が返されてしまう.これは nginx が static なファイルに対しての POST リクエストを許していないからこうなるみたいなんだけど,それはそれで困る.

これに対しての解決策はServing Static Content Via POST From Nginx - In Valid Logicにあるような方法をとればよさそう.

    # For maintenance mode
    error_page 405 =503 @maintenance;
    if ($request_addr != "127.0.0.1") {
      return 503;
    }

といった感じで,メンテナンス時の 405 エラーも 503 扱いにすることによって,GET 時と同じレスポンスを返すことができた.


about me

いろいろと興味を持ったことを書いてます.ちょっとしたことは hb(@smallstyle) on Twitter で書いてます.

Archive

2003|01|02|03|04|05|06|07|08|09|10|11|12|
2004|01|02|03|04|05|06|07|08|09|10|11|12|
2005|01|02|03|04|05|06|07|08|09|10|11|12|
2006|01|02|03|04|05|06|07|08|09|10|11|12|
2007|01|02|03|04|05|06|07|08|09|10|11|12|
2008|01|02|03|04|05|06|07|08|09|10|12|
2009|01|02|03|04|05|06|07|08|09|10|11|12|
2010|01|02|03|04|05|06|07|08|09|10|11|12|
2011|01|02|03|04|05|06|07|08|09|10|11|12|
2012|01|02|03|04|05|06|07|08|09|10|11|12|
2013|01|02|03|04|05|06|07|08|09|10|11|12|
2014|01|02|03|04|05|06|07|08|09|10|11|12|
2015|01|02|03|04|05|06|07|08|09|10|11|12|
2016|01|02|03|04|05|06|07|08|09|12|