4
2

More than 5 years have passed since last update.

nginxでベーシック認証設定

Posted at

最近、現場でnginxを触り始めたので学んだ事を細々とメモします。

パッケージインストール

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

$ sudo yum install httpd-tools

.htpasswdファイルの作成

$ sudo htpasswd -c /etc/nginx/.htpasswd <ユーザ名>
New password:
Re-type new password:
Adding password for user <ユーザ名>

nginx.confに下記を追記

auth_basic "<認証領域名>";
auth_basic_user_file /etc/nginx/.htpasswd;

設定例

/etc/nginx.conf
# ワーカプロセス数
worker_processes 1;
# エラーログ出力先
error_log /var/log/nginx/error.log;
# PIDファイルの配置先
pid /var/run/nginx.pid;
# 1ワーカプロセスが同時オープン可能なファイルディスクリプタ数最大値
worker_rlimit_nofile 1024;

events {
    # 1ワーカプロセスが同時オープン可能なコネクション数最大値
    worker_connections 512;
}
http {
    # アクセスログ出力先
    access_log /var/log/nginx/access.log;
    # nginxに常時接続しているクライアントに対するタイムアウト時間
    keepalive_timeout 10s;
    server {
        listen 80;
        location / {
            # 全てのアドレスを許可
            allow all;
            # Basic認証有効化
            auth_basic "Login Authentication";
            # パスワードファイルの指定
            auth_basic_user_file /etc/nginx/.htpasswd;
            # プロキシ設定
            proxy_pass http://xxx.xxx.xxx.xxx:80;
        }
    }
}

nginxを再起動

$ sudo service nginx restart
4
2
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
4
2