如何使用 nginx 配置避免 AWS ELB 健康检查的基本身份验证

How to avoid basic authentication for AWS ELB health-check with nginx configuration

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

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 healthcheck因为basic authentication出现401错误?

感谢您的帮助。

最简单的方法是为 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;
}