たまにnginxでbasic認証かけてくれと言われ毎回忘れて調べなおす・・・というのが、そこそこあるのでメモ。
basic認証用のユーザ、パスワード対応ファイルを作成する。構文は以下。
$ sudo htpasswd -c [ファイル保存先] [basic認証で使うユーザ名]
実例は以下のような感じ。
# sudo htpasswd -c /etc/nginx/.htpasswd www-data
New password: [パスワードを入力]
Re-type new password: [パスワードを入力]
Adding password for user www-data
そしてnginxの設定ファイルを更新する・・前にバックアップを取る。バックアップ大事!
# cd /etc/nginx/conf.d
# cp www.ukkari-san.net.conf www.ukkari-san.net.conf.old
設定ファイルを更新する。追加するのはauth_basic*がついた部分。以下の例ではサイト全体にかけているが特定のディレクトリ配下なども可能。
# vi www.ukkari-san.net.conf
location / {
auth_basic "Message here";
auth_basic_user_file /etc/nginx/.htpasswd;
...
設定チェックをして大丈夫そうならリロード。
# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
# service nginx reload
これで設定したユーザとパスワードでbasic認証がかかるようになりましたとさ。めでたしめでたし。
コメント