LoginSignup
1
3

More than 5 years have passed since last update.

nginxでベーシック認証

Last updated at Posted at 2017-03-27

タイトル通りです。

普段Apacheはあまり使わないのですが、BASIC認証(でいいや)をかける必要が出てきたのでついでに書いておきます。
※ 本記事は私が過去にブログに書いた記事からの転載です

環境

  • Ubuntu 14.04
  • nginxがインストール済み

手順

1. htpasswdコマンドを使えるようにする

Debian系(Ubuntuとか)

sudo apt-get install apache2-utils

RHEL系(CentOSとか)

sudo yum install htpasswd

2. .htpasswdファイルを作成

.htpasswdファイルを作成します。

内容はユーザー名:パスワードのペアになりますが、パスワードは暗号化できます。(認証が暗号化されるわけではない)

今回はSHA-1で暗号化します。

htpasswd -nbs ユーザー名 パスワード > .htpasswd

3. nginx.confを書き換え

こんな感じ

location / {
    root   /usr/share/nginx/html;
    index  index.html index.htm index.php;

    auth_basic  "basic auth";  # 認証の時に表示されるメッセージ
    auth_basic_user_file  ".htpasswd" # 先ほど作成した.htpasswdファイルのパス
}

4. nginxを再起動

sudo service nginx restart
1
3
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
1
3