我有下面的代码在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'的情况下重新编码,
谢谢
不要过分复杂的事情。 只需将您的ELB健康检查指向特定的URL即可。
server { location /elb-status { access_log off; return 200; } }
只是为了改善上述答案,这是正确的。 以下工作很好:
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; }
更新:如果用户代理validation是必要的,
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; }