Nginxでサブネットマスク範囲指定のIP制限をする

nginxでアクセス制限をするには、

            location ~* /api/ {
                allow 210.123.145.155;
 
                deny all;
            }

こんなふうに書く。

広告

でも、AWSでInternal ELBからのアクセスを許可する設定にしたいなあと思った。

IPがElasticに一定しないので、ホスト名で
allow .ap-northeast-1.compute.internal
こんなふうに書いてみたけど、これはだめだった。nginxのアクセス制御では、ドメイン名は使えないのだった。

hosts.allowみたいにこうやって書いてみるとどうだろう。
allow 172.31.

[dacelo@space nginx]$ sudo nginx -t                                                                                                                 
nginx: [emerg] invalid parameter "172.31." in /etc/nginx/conf.d/www_kids_http.conf:38
nginx: configuration file /etc/nginx/nginx.conf test failed

これはだめか。

じゃあサブネットマスクでこれはどうかな。
172.31.30.91/16

[dacelo@space nginx]$ sudo vi /etc/nginx/conf.d/www_kids_http.conf
[dacelo@space nginx]$ sudo nginx -t                                                                                                                 
nginx: [warn] low address bits of 172.31.30.91/16 are meaningless in /etc/nginx/conf.d/http.conf:39
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Syntax的にはOKなんだけど、
「low address bits of 172.31.30.91/16 are meaningless」
意味ねーぞ、という警告を受けてしまった。

やっぱこうか。
172.31.0.0/16

[dacelo@space nginx]$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

OKでした。

結論。

            location ~* /api/ {
                allow 210.123.145.155;
                allow 172.31.0.0/16;
 
                deny all;
            }

About: dacelo


Leave a Reply

Your email address will not be published. Required fields are marked *