TL;DR
前回はこちら。
参考書はこちら。
今回はIP制限やBasic認証といったアクセス制御についてです。
環境
設定ファイル中で変数を扱う
$testが変数名、"hogehoge"が値です。
set $test "hogehoge";
これによってログファイル名に変数を使うことが可能。
set $test "hogehoge";
access_log /var/log/nginx/$test.access.log;
IPによる制限
location / {
# ブラックリスト方式
# deny 172.20.0.1;
# allow all;
# ホワイトリスト方式
# allow 192.168.1.0/24;
# deny all;
root /framework/www/html;
index index.html;
}
Basic認証による制限
認証用のユーザーを作成するために、あらかじめhttpd-toolsをインストールしておく必要がある。
yum -y install httpd-tools
インストール後、認証用ユーザーとパスワードを追加。
htpasswd -c /etc/nginx/htpasswd <username>
追加できたら、設定ファイルにてBasic認証の設定を追加。
location / {
# Basic認証
auth_basic "enter paswd";
auth_basic_user_file /etc/nginx/htpasswd;
root /framework/www/html;
index index.html;
}
IP制限とBasic認証を組み合わせて使う
satisfy any;を記述すると、いずれか一つの認証条件を満たす場合にアクセスが許可される。
以下の設定例の場合、192.168.1.0~192.168.1.255以外はBasic認証が必要となる。
location / {
satisfy any;
allow 192.168.1.0/24;
deny all;
auth_basic "enter paswd";
auth_basic_user_file /etc/nginx/htpasswd;
root /framework/www/html;
index index.html;
}