Nginx – How to disable Nginx logging for localhost or certain ip

loggingnginx

I've got several sections in my Nginx config, fcgi for php, upstream for certain URLs etc and I want the nginx.log file to no log anything for any query coming from localhost as internal processes just flood it and then get processed by cron scripts.

Any ideas?

I've tried this is it ignores it.

location / {
   rewrites......
   if ($host = 127.0.0.1){
     access_log off;
   }
}

Best Answer

Your config is correct, but NginX tends to be a little... crazy? with Ifs. Take a look on it's If is Evil wiki page.

Aside from that, instead of trying to disable the log on that condition, try to redirect it to another log, like /var/log/nginx/access_localhost.log and see if it helps (link it to /dev/null if it works, or just truncate each day).

Also remember that access_log diretives (and other directives in NginX) will not be inherited. This is a quote from the NginX mail list (linked from NginX's wiki) about the subject:

There is no "global" logs in nginx, only "local" ones, either explicitly defined or inhereted from previous level.

The access_log directive, as all array-type directives in nginx config, will ignore inherited values if defined at certain level. I.e. in configuration

http { access_log log1;

server {
    server_name  server1;
}

server {
    server_name  server2;
    access_log   log2;
    ...
} }

only server1 will have logging set to log1, while server2 will have logging set to log2. If you want server2 to write log into both log1 and log2 you should say this explicitly, i.e.

server {
    server_name  server2;
    access_log   log1;
    access_log   log2;
    ...
}