How to Return HTTP 200 Without Serving a File in Nginx Configuration


3 views

When implementing health checks or request logging endpoints, there are cases where you need Nginx to return a 200 OK status without serving any actual file content. While Apache's Redirect 200 directive handles this elegantly, Nginx requires a different approach.

The most straightforward method uses an empty return directive within a location block:

location /health-check {
    access_log /var/log/nginx/health.log;
    return 200;
}

For more complex scenarios, consider these variations:

# Return 200 with custom headers
location /ping {
    add_header Content-Type text/plain;
    add_header X-Custom-Header "Pong";
    return 200 "";
}

# Return 200 for POST requests
location /log-request {
    if ($request_method = POST) {
        return 200;
    }
    return 405;
}

This method has minimal overhead because:

  • No filesystem I/O occurs
  • Nginx handles the response at the configuration level
  • Works efficiently even under high load

This technique proves valuable for:

# Load balancer health checks
location /lb-health {
    return 200;
}

# API endpoint monitoring
location /api/status {
    return 200 '{"status":"ok"}';
}

# Maintenance mode responses
location /maintenance-check {
    return 200 "System operational\n";
}

Verify your configuration with:

curl -I http://yourserver.com/health-check

Expected output should show HTTP/1.1 200 OK without any body content.


Sometimes, you need Nginx to respond with a simple HTTP 200 status code without serving any actual file content. This is useful for health checks, logging, or API endpoints where the response body isn't necessary.

Unlike Apache's Redirect 200 directive, Nginx requires a different approach. Here are two clean solutions:

# Solution 1: Using return directive
location /healthcheck {
    return 200;
    # Optional: add custom headers
    add_header Content-Type text/plain;
}
# Solution 2: Using empty response
location /ping {
    access_log off;
    return 200 "";
}

For more complex scenarios, you might want to:

location /custom-endpoint {
    # Return 200 with custom headers
    add_header X-Custom-Header "Value";
    add_header Content-Length 0;
    return 200;
}

When implementing this:

  • Disable access logging if not needed
  • Consider connection keep-alive settings
  • Monitor the performance impact if used extensively

Practical applications include:

# Load balancer health check
location /lb-health {
    return 200;
    access_log off;
    error_log off;
}
# Maintenance mode endpoint
location /maintenance-check {
    return 503; # Or 200 depending on your needs
}