【问题标题】:How to avoid basic authentication for AWS ELB health-check with nginx configuration如何使用 nginx 配置避免 AWS ELB 健康检查的基本身份验证
【发布时间】:2018-03-12 00:49:50
【问题描述】:

我在尝试为 ELB 运行状况检查实施基本身份验证时遇到问题。 我已经搜索了很多以找出 nginx 文件配置以避免如下所示的 401 错误,ELB 由于基本身份验证而返回

unhealthy in target-group hogehoge due to (reason Health checks failed with these codes: [401])

我试图修改 nginx.conf 以避免它,但它不起作用。 下面的代码给了我[emerg] "server" directive is not allowed here 错误。

http {
    server {
        location / {
            if (!$http_x_forwarded_for) {
                auth_basic           'Please enter ID and password';
                auth_basic_user_file /usr/src/redmine/.htpasswd;
            }
        }
    }
}

如何避免 ELB 健康检查由于基本身份验证导致的 401 错误?

感谢您的帮助。

【问题讨论】:

  • 您必须在自定义配置中添加它才能启用基本身份验证?您是如何首先启用身份验证的?

标签: amazon-web-services nginx passenger amazon-elb


【解决方案1】:

最简单的方法是为 ELB 创建一个位置,例如:

location /elb-status {
  access_log off;
  return 200;
}

您只需将Ping Path 更改为/elb-status

如果您想在测试时在浏览器上看到某些内容,您可能需要更改 content-type,因为默认为 application/octet-stream,并且浏览器会提供保存文件,所以这样的事情应该可以工作:

location /elb-status {
   access_log off;
   return 200 'your text goes here';
   add_header Content-Type text/plain;
}

如果您想检查用户代理,可以使用以下方法:

set $block 1;

# Allow all the ELB health check agents.
if ($http_user_agent ~* '^ELB-HealthChecker\/.*$') {
    set $block 0;
}
if (!$http_x_forwarded_for) {
    set $block 1
}

if ($block = 1) {
    auth_basic           'Please enter ID and password';
    auth_basic_user_file /usr/src/redmine/.htpasswd;
}

【讨论】:

  • 因为我还想启用/禁用基于 IP 地址的基本身份验证,所以我使用了您的第二个选项,并且它有效。非常感谢!
  • 小心ifs 在 nginx 中是邪恶的(谷歌你会看到),你应该使用 map 指令来设置 $block
  • 尽管如@CyrilDuchon-Doris 提到的那样,if 是邪恶的(因为它们是 Nginx 声明式风格中的命令式构造),但在服务器块中使用它们是相当安全的......参见 [Nginx on ifs] (@ 987654321@) 尤其是在他们说的地方:>“该怎么办?如果它适合您的需要,请使用 try_files。在其他情况下使用“return ...”或“rewrite ... last”。在某些情况下,它也是可以将 ifs 移动到服务器级别(这是安全的,因为其中只允许使用其他重写模块指令)。”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-30
  • 1970-01-01
  • 2020-12-18
  • 2019-10-25
  • 1970-01-01
  • 2018-10-29
  • 2020-05-29
相关资源
最近更新 更多