Nginx Solution for AWS Amazon ELB Health Checks - return 200 without IF

Solution 1:

Don't overcomplicate things. Just point your ELB health checks at a special URL just for them.

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

Solution 2:

Just to improve on the above answer, which is correct. The following works great:

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

Solution 3:

Update: If the user agent validation is necessary,

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