允许任何IP进入private_url,但仅允许白名单IP进入nginx中的public_url

时间:2019-10-12 10:00:10

标签: nginx whitelist

我在nginx后面的WebSphere上有一个Java Spring应用程序。 我有
my_website.com/private_url
my_website.com/public_url
当前,两个地址都可以通过任何IP访问。告诉Nginx只接受白名单子网列表中对my_website.com/private_url的请求的正确方法是什么?

1 个答案:

答案 0 :(得分:1)

要拒绝访问除特定目录的某些地址以外的其他所有人或请求,请添加此位置阻止

location ^~ /private_url {
  allow x.x.x.x/32;
  allow x.y.x.x/16; 
  deny all;
}

从上到下依次检查规则,直到找到第一个匹配项为止。

您应该在nginx.conf中添加它,但是您不想在每次添加新IP时都编辑此文件。因此,应将所有ip地址写入nginx主目录的whitelist.conf中,并将此文件包含在location块中。

whitelist.conf

allow x.x.x.x/32;
allow x.y.x.x/16;

nginx.conf

location ^~ /private_url {
  include whitelist.conf; 
  deny all;
}