我在尝试实现ELB HealthCheck的基本身份验证方面遇到了麻烦。我已经搜索了很多以找出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 HealthCheck避免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;
}
为什么不将401状态代码用作您的成功健康检查??
这意味着您的服务正在要求基本授权...换句话说,服务可用。
elb允许您指定HTTPS状态代码期望的。"高级健康检查设置"