在 nginx 中允许任何 IP private_url 但只允许列入白名单的 IP public_url

Allow any IPs to private_url but only allow whitelisted IPs to public_url in nginx

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

拒绝除某些地址之外的所有人访问特定目录或请求添加此位置块

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

按照从上到下的顺序检查规则,直到找到第一个匹配项。

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

whitelist.conf

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

nginx.conf

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