NginxでBasic認証を使う
スポンサーリンク
実行環境は
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
は記述する必要がある