3
6

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.

Nginxで静的ページにPostすると405で取得できない場合の対処

Last updated at Posted at 2018-10-12

課題(やりたいこと)

Nginxでメンテナンスモードで503を返すようにしたかった。Getリクエストであれば、503を返すことができるが、静的ページにPostリクエストすると405になってしまった(´; ω ;`)。これはNginxの仕様らしい...

実施方法

recursive_error_pages on;

  location @maintenance {
  if (-f $request_filename) {
     break;
  }
    error_page 405 = /maintenance.html;
    rewrite ^(.*)$ /maintenance.html break;
  }
  • 検証で設定したconfファイルの全体
server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;
    server_name localhost;
    error_page 503 @maintenance;

  recursive_error_pages on;
  if (-e /var/www/html/maintenance.html) {
        set $maintenance true;
  }
  if ($maintenance = true) {
        return 503;
  }

    location / {
        try_files $uri $uri/ =404;
    }

  location @maintenance {
  if (-f $request_filename) {
     break;
  }
    error_page 405 = /maintenance.html;
    rewrite ^(.*)$ /maintenance.html break;
  }
}

実行結果 ( 対策前 )

  • $ curl -X POST localhost -d id=hoge
<html>
<head><title>405 Not Allowed</title></head>
<body bgcolor="white">
<center><h1>405 Not Allowed</h1></center>
<hr><center>nginx/1.14.0</center>
</body>
</html>

実行結果 ( 対策後 )

  • $ curl -X POST localhost -d id=hoge
<html>
<p> This is Maintenance Page </p>
</html>

関連

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?