


先日、CentOS 8のNginxでBasic認証を設定してみましたが、今回はIPアドレスによるアクセス制限です。
Module ngx_http_access_module
https://nginx.org/en/docs/http/ngx_http_access_module.html
Combining Basic Authentication with Access Restriction by IP Address
https://docs.nginx.com/nginx/admin-guide/security-controls/configuring-http-basic-authentication/#configuring-nginx-and-nginx-plus-for-http-basic-authentication
折角なので上記ドキュメントのようにBasic認証との組み合わせを確認してみました。
環境
・CentOS Linux release 8.2.2004 (Core)
・Kernel 4.18.0-193.6.3.el8_2.x86_64
・nginx version: nginx/1.18.0(Nginx repository)
- IPアドレス制限
- reload
- 確認
- satisfy directive
前回、Basic認証を設定した領域に192.168.100.0/24からのアクセスは許可、それ以外はすべて拒否の設定を追加します。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } location /Restricted { root /usr/share/nginx; index index.html index.htm; auth_basic "Restricted Area"; auth_basic_user_file /usr/share/nginx/.htpasswd; allow 192.168.100.0/24; deny all; } (snip) |
1 2 3 4 |
[root@centos8 ~]# nginx -t nginx: the configuration file /etc/nginx/nginx.conf syntax is ok nginx: configuration file /etc/nginx/nginx.conf test is successful [root@centos8 ~]# systemctl reload nginx |
http://IP_Address//Restricted/ にアクセスするとログイン画面が表示されずに403 Forbiddenになりました。ちなみにクライアント側は192.168.1.0/24です。
satisfy any; を追加するとBasic認証あるいはクライアントIPアドレスが192.168.100.0/24のどちらかを満たせばアクセスできます。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
[root@centos8 ~]# cat /etc/nginx/conf.d/default.conf server { listen 80; server_name localhost; #charset koi8-r; #access_log /var/log/nginx/host.access.log main; location / { root /usr/share/nginx/html; index index.html index.htm; } location /Restricted { root /usr/share/nginx; index index.html index.htm; satisfy any; auth_basic "Restricted Area"; auth_basic_user_file /usr/share/nginx/.htpasswd; allow 192.168.100.0/24; deny all; } (snip) |
satisfy directiveは省略した場合はallなので「Basic認証とクライアントIPアドレスの両方を満たす」必要があります。