LoginSignup
13
13

More than 5 years have passed since last update.

Nginx で Digest認証が出来なかったのでソースからコンパイルしてみたぜ

Last updated at Posted at 2015-05-05

Digest認証ができないの?

ちょっとBasic認証だとアレなので、Digest認証を使いたいなと思ったら標準でサポートしていないのね。と、2012年位の話題から現在に至るまで対応してないなんて。そんなわけで現状(2015/5)でも、samizdatco氏の作ったnginx-http-auth-digestというサードパーティモジュールを利用するらしい。

HttpAuthDigestModule (Nginx Wiki)
Nginx Digest Authentication module (GitHub)

Basic認証だとアレなんだよ

Basic認証だとアレっていうのは、リクエストヘッダの認証部分が暗号化されていないという話です。Chrome の Developer Tools でリクエストヘッダ部分をみてみると下記部分があって、これをでコードかけると。。。あらw

Authorization:Basic ZG9nOmRvZ2RvZw==

# echo "ZG9nOmRvZ2RvZw==" | base64 -d
dog:dogdog

みれちゃうのよね。なのでDigest認証にすべきと。
しかし認証って多用な暗号化が使えるってしらんかった。

CRYPT , MD5 : Nginx 初版 から 対応
apr1 , PLAIN , SSHA : Nginx 1.0.3 から 対応
SHA : Nginx 1.3.13 から 対応

Nginx でBasic認証(ユーザ名、パスワードを求める )するには

Digest認証ソースをダウンロード、コンパイル

なんだかこの http-auth-digest-module は、bugがあるらしくsamizdatco氏マージしてないんだかなんだかなので(なんだかx2回目w)パッチも同時にあてちゃいます。

[FreeBSD][nginx] http-auth-digestモジュールパッチ

前回の[Nginx で WebDAV 環境構築、PROPFIND 405出で使えなかったのでソースからコンパイルしてみた ]コンパイル環境を元にします。

$ cd nginx-1.9.0
$ git clone https://github.com/samizdatco/nginx-http-auth-digest.git
$ cd nginx-http-auth-digest
$ git clone https://gist.github.com/frah/3921741 
$ patch -u < 3921741/patch-ngx_http_auth_digest_module.diff 

そして

$ ./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-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_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' --add-module=./nginx-dav-ext-module --add-module=./nginx-http-auth-digest 
$ make
$ sudo make install

Digest認証を設定

Apache に付属している htdigest コマンドを使用するのだけど、yum install httpd-tools とすればコマンドだけが入るので先にインスコしておく。
ユーザ:hage, パスワード:hagehage って感じでDigest認証ファイルを作ります。
あ、あとレアム?は、"digest realm-name" にとりあえずしときます。

$ cd /etc/nginx
$ htdigest -c .htdigest 'digest realm-name' hage

よし。できた。あと Nginx の定義は、いかのようにします。

location / {
  # digest認証
  auth_digest "digest realm-name";
  auth_digest_user_file /etc/nginx/.htdigest;

Nginx を再起動してアクセスしてみる。

ついでにBasic認証の設定方法

こっちも Apache に付属している htpasswd コマンドを使用するので、yum install httpd-tools を先にインスコしておく。
ユーザ:hage, パスワード:hagehage って感じでBasic認証ファイルを作ります。

$ cd /etc/nginx
$ sudo htpasswd -c .htpasswd hage

よし。できた。あと Nginx の定義は、いかのようにします。

location / {
  # basic認証
  auth_basic "Aurh Basic";
  auth_basic_user_file /etc/nginx/.htpasswd;
13
13
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
13
13