代码之家  ›  专栏  ›  技术社区  ›  Samurai_TT

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

  •  7
  • Samurai_TT  · 技术社区  · 6 年前

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

    下面的代码告诉我 [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错误?

    谢谢你的帮助。

    1 回复  |  直到 6 年前
        1
  •  13
  •   nbari    6 年前

    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;
    }