📢 Webサイト閉鎖と移転のお知らせ
このWebサイトは2026年9月に閉鎖いたします。
新しい記事は移転先で追加しております。(旧サイトでは記事を追加しておりません)

725行目: 725行目:
Basic認証のパスワードファイルを作成する。<br>
Basic認証のパスワードファイルを作成する。<br>
<br>
<br>
<code>htpasswd</code>コマンドでBasic認証を設定する。<br>
<code>htpasswd</code>コマンドを実行して、Basic認証を設定する。<br>
  htpasswd -c /<任意のパスワードファイルのディレクトリ>/.htpasswd <Basic認証する任意のユーザ名>
以下の例では、.htpasswdファイルとしているが、ファイル名は任意の名前でよい。<br>
  htpasswd -c /<任意のパスワードファイルのディレクトリ>/<Basic認証ファイル名> <Basic認証時のユーザ名>
   
   
  例. htpasswd -c /etc/nginx/conf.d/.htpasswd sample_user
  例. htpasswd -c /etc/nginx/conf.d/.htpasswd sample_user
739行目: 740行目:
     auth_basic "<認証名>";
     auth_basic "<認証名>";
               # 例. "Basic Authentication";
               # 例. "Basic Authentication";
     auth_basic_user_file <.htpasswdファイルの絶対パス>;
     auth_basic_user_file <Basic認証ファイルの絶対パス>;
                         # 例. auth_basic_user_file /etc/nginx/conf.d/.htpasswd;
                         # 例. auth_basic_user_file /etc/nginx/conf.d/.htpasswd;
  }
  }
747行目: 748行目:
     auth_basic "<認証名>";
     auth_basic "<認証名>";
               # 例. "Basic Authentication";
               # 例. "Basic Authentication";
     auth_basic_user_file <.htpasswdファイルの絶対パス>;
     auth_basic_user_file <Basic認証ファイルの絶対パス>;
                         # 例. auth_basic_user_file /etc/nginx/conf.d/.htpasswd;
                        # 例. /etc/nginx/conf.d/.htpasswd;
}
  </syntaxhighlight>
<br>
設定を反映する。<br>
sudo systemctl restart nginx
sudo systemctl reload  nginx
<br><br>
 
== Digest認証 ==
Digest認証のパスワードファイルを作成する。<br>
<br>
<code>htdigest</code>コマンドを実行して、Digest認証を設定する。<br>
以下の例では、.htdigestファイルとしているが、ファイル名は任意の名前でよい。<br>
htdigest -c /<任意のDigest認証ファイルのディレクトリ>/<Digest認証ファイル名> "<realm名>" <Digest認証時のユーザ名>
例. htdigest -c /etc/nginx/conf.d/.htdigest "test" sample_user
<br>
Digest認証を有効にするため、nginx.confファイル等に以下に示す設定を追加する。<br>
<syntaxhighlight lang="nginx">
# トップページ(http://www.example.com/)にDigest認証を掛ける場合、/のlocationディレクティブに設定を追加する
location / {
    root  <ドキュメントルートのディレクトリ>;
    index  index.html index.htm;
    auth_digest "<realm名>";
              # 例. "test";
    auth_digest_user_file <Digest認証ファイルの絶対パス>;
                         # 例. /etc/nginx/conf.d/.htdigest;
}
# トップページ以外(http://www.example.com/hoge/)にDigest認証を掛ける場合、/hoge/のlocationディレクティブを作成および設定を追加する
location /hoge/ {
    auth_digest "<realm名>";
                # 例. "test";
    auth_digest_user_file <Digest認証ファイルの絶対パス>;
                          # 例. /etc/nginx/conf.d/.htdigest;
  }
  }
   </syntaxhighlight>
   </syntaxhighlight>