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

I have the following code that is working on Nginx to keep the AWS ELB healthcheck happy.

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

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

I know the 'IF' is best avoided with Nginx and I wanted to ask if someone would know how to recode this without the 'if'?

thankyou


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