NGINX 摘要,limit_except GET,但允许本地主机

NGINX Digest, limit_except GET, but allow localhost

我正在尝试在 nginx 中配置 Digest Auth,为此我正在使用非官方模块,NGINX Digest module,并且在大多数情况下我可以让它正常工作,我可以锁定端点,除非它是 GET,这是我的位置配置。

location /config {
    proxy_pass http://internal_config_service/config;
    
    limit_except GET {
        auth_digest "peek a boo";
    }
}

但是,我有一个场景,我允许 localhost 不受挑战,但我并没有真正找到一个很好的方法来做到这一点。

我探索过的东西,我尝试过的东西 allow 127.0.0.1; 我什至研究过尝试用 if 做一些事情并检查 $host 是本地的,而不是添加摘要指令,但我认为这是不可能的,因为我的理解是配置是非常静态的。

我能想到的一个解决方案可能可行,但需要大量工作,并且会给新手带来额外的困惑,基本上是创建 2 个服务器,一个只能由 localhost 访问,并允许 localhost 通过没有挑战,不能从外部访问。然后是可公开访问并被摘要锁定的第二台服务器。

我希望有更好的解决方案,但我仍在学习 NGINX 的复杂性,但对更好的解决方案并不乐观。

您可以使用 satisfy 指令: http://nginx.org/en/docs/http/ngx_http_core_module.html#satisfy

问题:我不知道 auth_digest(非官方模块)是否会成为 NGINX 请求处理中 Auth-Face 的一部分。但是,如果是这种情况,您还可以使用 auth_request。但是试一试:

...
location /authreq {

  satisfy any;
  allow 127.0.0.1;
  deny all;
  auth_digest "something";
  # If auth_digest is not working try
  auth_request /_authdigest;

}

location = /_authdigest {
  internal;
  auth_digest "something";
}

更新您关于 allow 127.0.0.1; deny all

的问题

这将不会阻止所有其他客户端/流量。它告诉 NGINX 结合 satisfy any 如果 IP 不是 127.0.0.1 任何其他身份验证功能(auth_basic、auth_jwt、auth_request)必须成功让请求通过。在我的演示中:如果我不向 localhost 发送请求,我将不得不通过 auth_request 位置。如果 auth_request 类似于 200 它满足我的配置并且我被允许连接到上游代理。

我已经构建了一个小的 njs 脚本,为用户禁用 auth_digest 并针对受摘要身份验证保护的后端验证代理请求。但这不是你需要的,不是吗?

如果你想拆分配置,一个用于本地主机,另一个用于 public ip,你的服务器配置可能如下所示:

server {
  listen 127.0.0.1:80;

  ## do localhost configuration here
}

server {
   listen 80;
 ## apply configuration for the IP of nic eth0 (for example) here.
}