How to Configure Nginx to Return Empty 200 Responses for Failover Servers


1 views

When setting up failover servers, we often need them to accept all requests and respond with empty 200 OK status. While return 200; seems straightforward, monitoring tools like Pingdom sometimes misinterpret these responses as server failures.

The minimal response might be too minimal. Some monitoring systems expect:

  • Proper HTTP headers
  • Content-Length header when applicable
  • Standard response formatting

This solution provides the bare minimum while ensuring compatibility:


location / {
    return 200 "";
    add_header Content-Type text/plain;
    add_header Content-Length 0;
}

For systems requiring a physical response file:


location / {
    empty_gif;
    access_log off;
}

Create an empty file (e.g., /var/www/empty.txt) and reference it:


location / {
    return 200 "";
    root /var/www;
    try_files /empty.txt =200;
}

To minimize overhead:


location / {
    return 200 "";
    access_log off;
    error_log off;
    add_header Cache-Control "no-store";
    expires off;
}

Verify with curl:


curl -I http://your-server/

Should return:


HTTP/1.1 200 OK
Server: nginx
Content-Type: text/plain
Content-Length: 0

If Pingdom still complains:

  1. Ensure connection isn't being reset (check firewall)
  2. Verify TCP handshake completes
  3. Test with different monitoring locations

When setting up a failover server that needs to accept all incoming requests and respond with a blank 200 status, many administrators first try the straightforward approach:

location / {
    return 200;
}

While this technically works, monitoring tools like Pingdom often flag these responses as "no response" because they expect some minimal content or headers. The empty response can trigger false alarms in monitoring systems.

Here's a better approach that satisfies both the need for minimal processing and monitoring system requirements:

location / {
    add_header Content-Type text/plain;
    return 200 'OK';
}

This configuration:

  • Returns a proper 200 status code
  • Includes a minimal response body ('OK')
  • Sets appropriate headers
  • Maintains extremely low overhead

For absolute minimal processing overhead, we can further optimize:

location / {
    add_header Content-Length 0;
    add_header Content-Type text/plain;
    return 204;
}

Note we're using 204 (No Content) here instead of 200. This is semantically more correct for empty responses and may work better with some monitoring systems.

For a comprehensive solution that handles various request methods:

location / {
    if ($request_method = GET) {
        add_header Content-Type text/plain;
        return 200 'OK';
    }
    if ($request_method = POST) {
        add_header Content-Type text/plain;
        return 200 'OK';
    }
    if ($request_method = HEAD) {
        add_header Content-Length 0;
        return 204;
    }
    # Default case
    add_header Content-Type text/plain;
    return 200 'OK';
}

After implementing these changes, verify with curl:

curl -I http://your-server.com/
curl -X POST http://your-server.com/
curl -X HEAD http://your-server.com/

This will help you confirm that all request types are handled appropriately and that monitoring systems will interpret the responses correctly.