加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 运营中心 > Nginx > 正文

覆盖单个位置块的nginx拒绝规则

发布时间:2020-12-13 21:27:58 所属栏目:Nginx 来源:网络整理
导读:我有这样的nginx设置,其中服务器应该主要是私有的(只有某个IP地址可以使用服务器),除了一个应该公开的位置块: server { listen 443 ssl default; # Allow access only from certain IP addresses allow 12.34.56.78/32; allow 10.0.2.2/32; deny all; # Pro

我有这样的nginx设置,其中服务器应该主要是私有的(只有某个IP地址可以使用服务器),除了一个应该公开的位置块:

server {
  listen  443 ssl default;

  # Allow access only from certain IP addresses
  allow   12.34.56.78/32;
  allow   10.0.2.2/32;
  deny    all;

  # Proxy dynamic requests to the app
  location / {
    proxy_pass  http://127.0.0.1:8000;
  }
  # Serve static assets from disk
  location = /favicon.ico {
    alias  /var/www/example.com/htdocs/static/images/favicon.png;
  }
  location /static {
    alias  /var/www/example.com/htdocs/static;
  }
  ...

  # Allow public access to this endpoint
  location = /public/endpoint {
    proxy_pass  http://127.0.0.1:9000;

    # Allow *all* IPs here,so that they don't hit the server "deny" rule
    # [except this doesn't seem to work...]
    allow 0.0.0.0/0;
  }
}

但是,在最后添加允许公共位置块中的规则不起作用 – 来自上面列表中的IP的请求被拒绝.

将拒绝所有规则从服务器块移动到每个非公共位置块中也没有预期的效果.

有没有办法实现所需的行为,而不必将整套“允许,允许,拒绝”规则复制到每个非公共位置块?

最佳答案
你应该只使用allow all

location = /public/endpoint {
    proxy_pass  http://127.0.0.1:9000;

    # Allow *all* IPs here,so that they don't hit the server "deny" rule
    allow all;
}

此外,如果您使用不同类型的限制,您可能需要添加满足任何;它的工作原理.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读