28

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

3 Answers 3

79

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;
  }
}
11
  • thankyou for you reply... can you explain a touch more... currently in the ELB health check I'm pointing it at /index.html. Do you mean point the health checks at say '/elb-status' and the add the above server block? is that it? does the /elb-status url need to exist? thx again
    – Adam
    Jun 25, 2013 at 0:52
  • worked perfectly when I put /elb-status in the ELB and added the server block above - thankyou so much!!! greatly apprecated
    – Adam
    Jun 25, 2013 at 0:58
  • 1
    Hmm, I'm gettting "/usr/share/nginx/html/elb-status" failed (2: No such file or directory)... any idea why this could be? Jan 24, 2014 at 17:47
  • 1
    Neat solution. 😙
    – pTK
    Sep 20, 2017 at 8:00
  • 1
    @SandeepBalagopal See that access_log off line?
    – ceejayoz
    May 4, 2020 at 16:19
33

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

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .