nginx: 同じ configuration level で auth_basic off は指定できない

nginx の ngx_http_auth_basic_module を使って BASIC 認証をかけることができる。

ドキュメントを読むと明示的に `auth_basic off` を指定することで BASIC 認証を切ることができそうだったので試したけど、うまくいかないケースがある。

結論からいうと `auth_basic off` は上位のコンテキストから継承した設定を打ち消すためのもので、同じコンテキストで指定された `auth_basic` は打ち消せない。

実際のコード例:

# app.nginx.conf
server {
    server_name app.example.com;

    include "/path/to/config/auth.nginx.conf"; # 同じコンテキストで auth_basic が指定されている
    auth_basic off; # 祖先コンテキストではなく同じコンテキストなのでダメ
}

auth.nginx.conf:

auth_basic "Authentication Required";
auth_basic_user_file "/etc/nginx/conf/auth.htpasswd";

実際に config test を行うとエラーになる:

nginx: [emerg] "auth_basic" directive duplicate in app.nginx.conf:5
nginx: configuration file /etc/nginx/nginx.conf test failed

これはドキュメント中の以下の文章で示されている:

The special value off allows cancelling the effect of the auth_basic directive inherited from the previous configuration level.

http://nginx.org/en/docs/http/ngx_http_auth_basic_module.html#auth_basic