実行環境は
CentOS7 64bit
htpasswdのインストール
$ sudo yum -y install httpd-tools
Basic認証用の認証情報の設定
/etc/nginx/.htpasswdにBasic認証用ファイルを作成する
$ cd /etc/nginx
$ sudo htpasswd -cm .htpasswd ${USER}
New password: ${PASSWORD}
Re-type new password: ${PASSWORD}
Adding password for user ${USER}
nginxでBasic認証を有効化
$ sudo vi /etc/nginx/conf.d/default.conf
Basic認証を適用したいlocationにauth_basicとauth_basic_user_fileを追加
location /とし、Basic認証を使用するディレクトリを指定auth_basicパスワード入力ダイアログに表示するメッセージを記述auth_basic_user_fileには上で作成したBasic認証用ファイルを指定
location / {
root /var/www/html;
index index.html index.htm;
auth_basic "input password";
auth_basic_user_file /etc/nginx/.htpasswd;
}
※locationを/以外にする場合でもrootとindexは記述する必要がある