22

I'm configuring a failover server tasked to accept any incoming request, and reply with blank 200 response. The idea is to minimize the reply time and to ensure we dont send any 40x or 50x.

I tried using return 200; for the desired locations within Nginx, but my monitoring systems (Pingdom) didn't like the response, and consider the server not responding.

Is there a better way to do this, of course with minimal overhead on the server?

2 Answers 2

27

HTTP status code 204 No Content is meant to say "I've completed the request, but there is no body to return":

10.2.5 204 No Content

The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation. The response MAY include new or updated metainformation in the form of entity-headers, which if present SHOULD be associated with the requested variant.

If the client is a user agent, it SHOULD NOT change its document view from that which caused the request to be sent. This response is primarily intended to allow input for actions to take place without causing a change to the user agent's active document view, although any new or updated metainformation SHOULD be applied to the document currently in the user agent's active view.

The 204 response MUST NOT include a message-body, and thus is always terminated by the first empty line after the header fields.

2
  • Hello Alex Thanks for the note. Just wondering if 204 response code is recognized by all major browsers these days. i.e. if my server start sending 204 around, is there any probability that some visitors (across browsers, mobiles, terminals) get an error? Oct 11, 2011 at 14:07
  • 1
    @SparshGupta I'm not an expert in browser compatibility, sorry. I've made a quick check in a couple of browsers (FF 7 and Safari under Mac OSX) -- they do exactly nothing. For example, if you have a page already open, then you put an URL returning 204 into the address bar and hit Enter, both browsers still show that previous page. Oct 11, 2011 at 16:35
35

You can have nginx return back an empty HTTP 200 by a config block like:

location = /health {
  return 200;
  #access_log off;
}

You can uncomment the access_log line if you dont want all those health checks to be logged.

2
  • 6
    It should be noted that the '=' sign after location is important. Without it, that file must exist, otherwise nginx will not return 200. With it, you can just make up any location you want and not worry about files and nginx will happily return 200. May 29, 2015 at 23:36
  • How to combine that with basic auth, such that authenticated request return 200, and unauthenticated 401?
    – Kiril
    Aug 17, 2016 at 14:40

You must log in to answer this question.

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