LoginSignup
11
10

More than 5 years have passed since last update.

Nginx で Basic認証

Last updated at Posted at 2014-07-23

参考:http://bekkou68.hatenablog.com/entry/2013/10/05/232451

任意の場所へ
cd /etc/nginx/conf.d/


sudo htpasswd -c .htpasswd username

ドットファイルにする方がセキュリティが良い気がする。

パスワードの設定を求められるので、パスワードを2回入力する(この入力したパスワードがログイン時に実際に使用するもので、ファイルに出力されるのはcrypt関数で暗号化されているので簡単に復号化はできないらしい。パスワード忘れてしまった場合はユーザー追加しか対応がないのかな。

ユーザーを追加したい場合、


sudo htpasswd .htpasswd addusername

パスワード生成コマンドlinux

mkpasswd
pwgen
http://d.hatena.ne.jp/tkrd/20121111/1352645711

nginx設定例:


server {
  listen 80;
  server_name ;
  location / {
    # Passwd auth
    auth_basic            "Restricted";
    auth_basic_user_file  /etc/nginx/conf.d/.htpasswd;
    root ;
  }
  location /nginx_status {
    stub_status on;    # activate stub_status module
    access_log off;
    allow 127.0.0.1;   # restrict access to local only
    deny all;
  }
}

設定反映


nginx -t
sudo service nginx reload
11
10
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
10