When running Nginx behind HAProxy with active health checks, you'll often see your access logs flooded with repetitive requests like this:
127.0.0.1 - - [24/Jul/2009:19:28:22 +0000] "GET /test.html HTTP/1.0" 200 12 "-" "-"
127.0.0.1 - - [24/Jul/2009:19:28:24 +0000] "GET /test.html HTTP/1.0" 200 12 "-" "-"
These are legitimate health check requests from HAProxy, but they can make your logs noisy and harder to analyze real traffic patterns.
The most elegant solution is to use Nginx's conditional logging with the map
directive. This approach gives you granular control over what gets logged without affecting request processing.
http {
map $request_uri $loggable {
/test.html 0;
default 1;
}
server {
access_log /var/log/nginx/access.log combined if=$loggable;
location / {
# Your regular configuration
}
}
}
Another approach is to create a specific location block for your health check endpoint and disable logging there:
server {
access_log /var/log/nginx/access.log combined;
location = /test.html {
access_log off;
root /path/to/your/healthcheck/files;
}
}
While we're focusing on Nginx, it's worth noting you can adjust HAProxy's check interval to reduce log volume:
backend static_http
option httpchk GET /test.html
server w1_static www1:81 check port 81 inter 5000 rise 2 fall 3
This changes the check interval from 2 seconds to 5 seconds and adjusts failure detection thresholds.
For more complex scenarios, you might want to exclude requests based on multiple criteria (IP + URI):
map $remote_addr:$request_uri $skip_log {
"127.0.0.1:/test.html" 0;
default 1;
}
server {
access_log /var/log/nginx/access.log combined if=$skip_log;
}
Conditional logging does add minimal overhead, but in most cases it's negligible compared to:
- Disk I/O savings from writing fewer log entries
- Reduced log rotation frequency
- Easier log analysis with less noise
For high-traffic servers, test with and without conditional logging to measure any performance difference.
When running Nginx behind HAProxy (especially on the same server), health check requests can flood your access logs. In this configuration, HAProxy's regular HTTP health checks create unnecessary log entries like:
127.0.0.1 - - [24/Jul/2009:19:28:22 +0000] "GET /test.html HTTP/1.0" 200 12 "-" "-"
The solution lies in Nginx's conditional logging capability. We can modify the access_log directive to exclude specific requests based on:
- Request URI
- Remote IP address
- User agent
- HTTP method
Here's how to configure your Nginx server block:
server {
listen 80;
server_name example.com;
# Define a variable to check health requests
set $skip_log 0;
# Match HAProxy health checks
if ($request_uri = /test.html) {
set $skip_log 1;
}
# Alternative: Match by both URI and User-Agent
if ($request_uri = /test.html) {
set $skip_log "${skip_log}1";
}
if ($http_user_agent ~* "^HAProxy Health Check") {
set $skip_log "${skip_log}1";
}
# Apply conditional logging
access_log /var/log/nginx/access.log combined if=$skip_log;
# Your regular configuration continues...
location / {
# Your normal configuration
}
}
For more complex filtering, use the map directive in your http block:
http {
map $request_uri $loggable {
/test.html 0;
default 1;
}
server {
access_log /var/log/nginx/access.log combined if=$loggable;
# Rest of your configuration
}
}
While these solutions work well, consider:
- Conditional logging adds minimal overhead
- Map directives are evaluated at request time
- For high-traffic servers, test impact first
After implementing, verify with:
tail -f /var/log/nginx/access.log | grep -v test.html
And confirm HAProxy still receives proper health check responses with:
curl -I http://localhost/test.html