LoginSignup
247
215

More than 5 years have passed since last update.

Nginx で Basic 認証

Last updated at Posted at 2015-12-10

パッケージインストール

htpasswd コマンドを利用するため、下記のパッケージをインストール。

Debian 系

$ sudo apt-get install apache2-utils

Red Hat 系

$ sudo yum install httpd-tools

.htpasswd ファイルの作成

ユーザ名とパスワードを追加。

$ sudo htpasswd -c /etc/nginx/.htpasswd username
New password: password
Re-type new password: password
Adding password for user username

nginxの設定ファイルの追記

.htpasswd ファイルを読み込み、Basic 認証を適用。

$vim /etc/nginx/nginx.conf
…
server {
    listen 80;
    root /usr/share/nginx/html;
    index index.html index.htm;

    location / {
        auth_basic "Restricted";                   # 認証時に表示されるメッセージ
        auth_basic_user_file /etc/nginx/.htpasswd; # .htpasswdファイルのパス
    }
}
…

再起動

sudo service nginx restart
247
215
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
247
215