13

I'm running nginx behind haproxy (running on the same server). I've configured haproxy to use a simple html file on nginx to verify the service is up, since I don't have/want a valid "/" URL on this host. Nginx doesn't support the OPTIONS request type (as far as I know) which is the default that haproxy uses, so I've changed it to a GET.

Since I have access logs turned on in nginx, I'm getting all these uptime poll requests in my access log. Is there a way that I can configure nginx to ignore certain requests, and skip logging them?

Here's the haproxy backend:

backend static_http
        option  httpchk GET /test.html
        option  redispatch
        balance roundrobin
        #fullconn 1000
        server  w1_static www1:81 check port 81 inter 2000

And Here's what I see in the nginx logs:

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 "-" "-"
127.0.0.1 - - [24/Jul/2009:19:28:26 +0000] "GET /test.html HTTP/1.0" 200 12 "-" "-"
127.0.0.1 - - [24/Jul/2009:19:28:28 +0000] "GET /test.html HTTP/1.0" 200 12 "-" "-"
127.0.0.1 - - [24/Jul/2009:19:28:30 +0000] "GET /test.html HTTP/1.0" 200 12 "-" "-"

3 Answers 3

16

You can now do this

http://nginx.org/en/docs/http/ngx_http_log_module.html

The if parameter (1.7.0) enables conditional logging. A request will not be logged if the condition evaluates to “0” or an empty string

map $request_uri $loggable {
  / 0;
  /healthcheck.html 0;
  default 1;
}

server {
...
  access_log /var/log/nginx/access.log combined if=$loggable;

}
1
  • Worked like a charm, though for some reason I had to use ~ regex matching in map, otherwise it wasn't matching my URI. Commented Jul 9, 2020 at 6:18
10

Well, you could try having a specific location directive for this.

Something like

location /test.html {
  access_log off;
}

should work (untested)...

0

There's no built-in filtering that will do what you want.

There are a few tricks you might try though. These will all result in the data you're not interested in being logged to a different file, which you can nuke externally (remember to HUP nginx afterwards) as often as you feel is appropriate.

  • if haproxy is the only thing that requests /test.html, you can have a different access log per location block (unfortunately not for an if block inside a location block).

  • if other nodes request /test.html but all requests from 127.0.0.1 are haproxy, you could create a map based on $remote_addr that translates 127.0.0.1 to access_log.junk and everything else to access_log. Use the map variable in the name of the access log statement

Failing that, you've got the source, so hack what you need in.

You must log in to answer this question.

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