用于AWS Amazon ELB健康检查的Nginx解决方案-不带IF返回200


22

我有以下正在Nginx上运行的代码,以使AWS ELB运行状况检查保持满意。

map $http_user_agent $ignore {
  default 0;
  "ELB-HealthChecker/1.0" 1;
}

server {
  location / {
    if ($ignore) {
      access_log off;
      return 200;
    }
  }
}

我知道Nginx最好避免使用“ IF”,我想问问是否有人会知道如何在没有“ if”的情况下重新编码?

谢谢

Answers:


62

不要使事情过于复杂。只需将您的ELB健康检查指向一个专门针对他们的特殊URL。

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

谢谢您的回复...您能解释一下吗...目前在ELB健康检查中,我将其指向/index.html。您的意思是将运行状况检查指向“ / elb-status”并添加上面的服务器块吗?是吗 / elb-status网址需要存在吗?再次感谢
Adam

当我将/ elb-status放入ELB并在上面添加了服务器块时,它工作得非常好-非常感谢!!!非常感激
亚当(Adam)

很高兴我能帮上忙!
ceejayoz

1
嗯,我很想"/usr/share/nginx/html/elb-status" failed (2: No such file or directory)……为什么会这样呢?
2014年

1
整洁的解决方案。😙–
phegde

27

只是为了改善上述答案,这是正确的。以下作品很棒:

location /elb-status {
    access_log off;
    return 200 'A-OK!';
    # because default content-type is application/octet-stream,
    # browser will offer to "save the file"...
    # the next line allows you to see it in the browser so you can test 
    add_header Content-Type text/plain;
}

5

更新:如果需要用户代理验证,

set $block 1;

# Allow only the *.example.com hosts. 
if ($host ~* '^[a-z0-9]*\.example\.com$') {
   set $block 0;
}

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

if ($block = 1) { # block invalid requests
  return 444;
}

# Health check url
location /health {
  return 200 'OK';
  add_header Content-Type text/plain;
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.