NginxにDigest(ダイジェスト)認証を組み込む~ソースのコンパイルが必要です~
システムに欠かせないミドルウェアの世界

中山テック 代表の中山です。

さて、NginxのDigest認証を実現させたくて色々ページを見ていたのですがソース⇒makeで作成する必要があるそうで・・・

面倒ですが、これぐらいしないと実現できないので早速実践してみました!

こちらのサイトを参考にしました







  • VMware × CentOS関連索引

※CentOS7上でのインストールになります

まずは必要なパッケージをDL&インストール。

yum -y install gcc pcre pcre-devel make perl httpd-tools

各種ファイルダウンロード

・Nginx本体

・ダイジェスト認証

・圧縮・伸張ライブラリ⇒コンパイル自にも使用

・OpenSSL

をダウンロードします。

wget "http://nginx.org/download/nginx-1.20.0.tar.gz" -O /tmp/nginx-1.20.0.tar.gz
wget "https://github.com/atomx/nginx-http-auth-digest/archive/master.zip" -O /tmp/nginx-http-auth-digest.zip
wget "http://zlib.net/zlib-1.3.1.tar.gz" -O /tmp/zlib-1.3.1.tar.gz
wget "https://www.openssl.org/source/old/1.1.1/openssl-1.1.1d.tar.gz" -O /tmp/openssl-1.1.1d.tar.gz

続いて解凍。

tar zxvf /tmp/nginx-1.20.0.tar.gz -C /usr/local/src/
unzip /tmp/nginx-http-auth-digest.zip -d /usr/local/src/
tar zxvf /tmp/zlib-1.3.1.tar.gz -C /usr/local/src/
tar zxvf /tmp/openssl-1.1.1d.tar.gz -C /usr/local/src/

解凍したファイル(ディレクトリ)をroot権限に修正します。

chown -R root:root 対象のディレクトリ名

そしてコンパイルします。

cd /usr/local/src/nginx-1.20.0/

./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-openssl=/usr/local/src/openssl-1.1.1d --with-zlib=/usr/local/src/zlib-1.3.1 --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-http_v2_module --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' --add-module=../nginx-http-auth-digest-master/

make && install

エラーがなければ完了です!

  • システム開発関連記事

ユーザ、キャッシュディレクトリ作成

Nginx実行ユーザ・グループを作成します。

あくまでも実行ユーザであり、ログインできないように最後のコマンドを入れます。

groupadd nginx
useradd -g nginx nginx
usermod -s /bin/false nginx

mkdir -p /var/cache/nginx/client_temp

起動スクリプト、ログローテート作成

起動スクリプトを作成します。

cat <<EOF > /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=forking
PIDFile=/run/nginx.pid

ExecStartPre=/usr/bin/rm -f /run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t
ExecStart=/usr/sbin/nginx
ExecReload=/bin/kill -s HUP $MAINPID
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=mixed
PrivateTmp=true

[Install]
WantedBy=multi-user.target
EOF

ログローテートを設定します。

cat <<EOF > /etc/logrotate.d/nginx
/var/log/nginx/*log {
    create 0664 nginx root
    daily
    rotate 10
    missingok
    notifempty
    compress
    sharedscripts
    postrotate
        /bin/kill -USR1 `cat /run/nginx.pid 2>/dev/null` 2>/dev/null || true
    endscript
}
EOF

Firewall(HTTP通信設定)

ブラウザからのアクセスをしたいのでhttpを追加します。

firewall-cmd --add-service=http --permanent --zone=public
firewall-cmd --reload

パスワードファイル作成

ここ大事です。

・Realm(認証先)

・ユーザ

・パスワード

が設定されます。今回はアクセス⇒ユーザ名・パスワードを入力するパターンなのでRealmは何でもいいです。

※本来はPOST通信でHTTPヘッダ(Authorization)に設定されてきて、合わせた値にする使い方になるかと

⇒わからない場合はtcpdump等で確認するのが良いでしょう

今回はID:user、パスワード:testで設定します。

htdigest -c /etc/nginx/.htdigest_test "test" user

Adding password for user in realm test.
New password:  ←パスワード
Re-type new password:  ←確認パスワード

confファイル修正

location内に作成したパスワードファイル、Realm名を定義します。

vi /etc/nginx/nginx.conf

~中略~
        location / {
            root   html;
            index  index.html index.htm;
            auth_digest "test"; #Realm名
            auth_digest_user_file /etc/nginx/.htdigest_test; #パスワードファイル格納場所
        }

ここまで出来たらNginxを起動します。

systemctl enable nginx
systemctl start nginx

画面

「http://IPアドレス」と入力すると認証ダイアログが出てユーザ名・パスワードが求められます。

※セキュリティ強化にもなっております

先ほど設定したユーザ名・パスワードを入力します。

バッチリでございます!

普段はこのような使い方をせず、POSTヘッダにDigest認証情報を設定して通信します。

是非色々と試してみてください!



まとめ

いかがでしたでしょうか。

手順はほぼこちらのサイトと同じになりますのでブログにして良いかどうかとは思いましたが・・・

ひとまずHTTP通信でセキュア(HTTPS)出ない場合は是非構築すべき手法です。

現在はNginxを使用しているシステムも多いので、皆様のご参考になれば幸いです。

設定値はこちらから


  • VMware × CentOS関連索引

※中山テックに掲載しているゲーム画像の著作権、商標権、その他知的財産権は、当該コンテンツの提供元に帰属します


おすすめの記事