LoginSignup
1
1

More than 1 year has passed since last update.

EC2 x nginx ベーシック認証の掛け方

Last updated at Posted at 2022-06-26

EC2 x nginxのBasic認証の掛け方

apacheとnginxで掛け方が異なるので注意。

今回はnginxの方法(nginxの今回の方法では.htaccessの作成は不要

参考記事

.htpasswdの作成と**nginx.confに.htpasswdを使用したBasic認証の記述を追加する

参考記事

.htpasswdの作成

EC2にssh接続して以下のディレクトリへ移動

$ cd /usr/share/nginx/html/

さらに以下で.htpasswd作成

sudo touch .htpasswd

sudo vi .htpasswd を使用し、上述のサイトで作った.htpasswdに記入する1行を貼り付け

nginx.confの編集

nginx.conf に basic認証 の設定を追記

sudo vim /etc/nginx/nginx.conf

        server {
            listen 80;
            server_name localhost;
    
            #charset koi8-r;
    
            #access_log /var/log/nginx/host.access.log main;
    
           <--以下を追記-->
            auth_basic "basic authentication";
            auth_basic_user_file "/usr/share/nginx/html/.htpasswd";
           <--追記ここまで-->
    
            location / {
                root /usr/share/nginx/html;
                index index.html index.htm;
            }
  • auth_basic ディレクティブは、basic認証時のメッセージダイアログに表示
  • auth_basic_user_file ディレクティブは、先ほど作ったパスワードファイルの場所を指定
1
1
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
1