nginxでBasic認証をかけたのでメモです。

nginxの設定

nginxの設定は単純で、以下の2行追加すればOKでした。

    auth_basic  "QS Beta TEST";
    auth_basic_user_file "/home/httpd/.htpasswd";

なので、通して書くと、

server{
  listen      80;
  server_name xxx.abc.com;
  location / {
    auth_basic  "QS Beta TEST";
    auth_basic_user_file "/home/httpd/.htpasswd";
    proxy_pass http://localhost:4009;
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header Host $http_host;
    allow all;
  }
}

みたいな感じになります。

.htpasswdの作成

で、次に、.htpasswdを作ります。 ここではまったのが、MD5が標準では使えないということでした。 つまり、8文字位以上のパスワードは使えません。 今回はさくっとかけたいだけなので、

$ htpasswd -nb user名 7文字以下のパスワード > /home/httpd/.htpasswd

として、作成しました。

リロード

最後に、反映しましょう。

$ /etc/rc.d/init.d/nginx configtest
$ /etc/rc.d/init.d/nginx reload

と、リロードしておきます。以上です。