Help us understand the problem. What is going on with this article?

nginxでベーシック認証

More than 1 year has passed since last update.

タイトル通りです。

普段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
Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away