How to disable Nginx logging?
I have the following in config file
server {
listen 80;
server_name _;
access_log /var/log/nginx/access.log main;
...
server {
listen 80;
server_name example.com
access_log off;
error_log off;
But it is still keep logging for example.com
virtual host. What am I doing wrong?
Solution 1:
You are missing ;
after server_name directive. access_log
and off
are being treated as additional server_names.
Solution 2:
Of course you can completely disable logging. Just point the logfiles to /dev/null and be done. ;)
access_log /dev/null;
error_log /dev/null;
Solution 3:
you can disable access_log
by using
access_log off;
but if you want to disable error_log, just don't use error_log option in your conf.
Solution 4:
OP's problem was a syntax error, which is explained in this answer.
As of January 2020, with nginx 1.14.0+
the general problem of disabling access and error logging in nginx is solved as follows:
Access logs
To disable logs on some configuration level, assuming that it has been explicitly enabled on a higher one or are enabled by default (as they are), use this directive*:
access_log off;
(Answers saying that you should redirect this kind of logs to /dev/null
are outdated.)
Note that the access_log
directive may be set multiple times on a single level but the above entry disables all access log logging on the current level.
Source: nginx docs for access_log
Error logs
Disabling error logs is more tricky as there is NO explicit option to turn it off on a lower configuration level, if it has been enabled on a higher one or it is enabled by default (as it is)!
So in this case you should use what this answer proposed:
error_log /dev/null;
Source: nginx docs for error_log
Caveats
Imagine that you have a single http
with access log enabled on this level, and inside multiple server
entries and one of these server
s is an entry point to all others. Let's say you have one server
with SSL configured that listens for connections from outside with HTTPS and it redirects them to other server
s which are various API gateways.
Then it's not enough to disable access logs in some of the API gateway server
s (or their location
s) as the request will be still logged in the access log of the external HTTPS server
.
Bad example:
http {
access_log /var/log/nginx/access.log main;
server {
listen 80;
location /app1 {
proxy_pass http://localhost:81;
}
(...)
}
server {
listen 81;
access_log off; # <----- this WON'T effectively work - requests will be logged in the access log by the server above
(...)
}
(...)
}
You would have to disable access logs on the whole paths of given requests to make it work in this case. But of course a better solution would be to consider simplifying your config as you may run into many more surprises even after you solve the problem with access logs...
Simpler config example:
http {
access_log /var/log/nginx/access.log main;
server {
listen 80;
location /app1 {
access_log off; # <----- this WILL work
proxy_pass http://app1server;
}
(...)
}
}