52

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?

3
  • What do you intend "main" to do here? Dec 14, 2015 at 20:40
  • I've flagged this question for removal, because the most upvoted answer has to do with your syntax-error. It has nothing to do with turning off error logging. It has no searchable error messages, and I'm not sure what you're asking or why the provided answers don't solve your problem. Dec 14, 2015 at 20:42
  • 1
    I have added an answer for general problems with turning off logging in nginx, @Evan. I think that with it this question is helpful and should not be deleted. Jan 8, 2020 at 17:28

5 Answers 5

42

You are missing ; after server_name directive. access_log and off are being treated as additional server_names.

4
  • 13
    In addition, error_log off; doesn't turn off error logging. It just logs errors to a file named 'off'. There's no way to completely disable error logging, the closest you can get is error_log /dev/null crit; which is almost the same thing, since no error log will appear.
    – kolbyjack
    Oct 5, 2011 at 12:16
  • 9
    Actually, doing access_log off; causes nginx to write the log into file called off. So this is not right answer.
    – user965363
    Oct 17, 2011 at 8:40
  • 5
    Actually, this is right answer. "Using "off" as the only parameter clears all access_log directives for the current level": wiki.nginx.org/HttpLogModule#access_log. It is not true for error_log (as added by @kolbyjack), but I believe question was about access, not error log. It is possible that some very old versions of nginx don't support this.
    – rvs
    Oct 17, 2011 at 16:29
  • Is there an advantage to using off instead of /dev/null? If off causes some versions of Nginx write to a file named off, then /dev/null seems like a more universal solution.
    – Joe Mornin
    Jan 22, 2016 at 23:53
32

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;
4
  • 4
    I use this one too; /dev/null is a real destination; while "off" is a file in some directory
    – edoceo
    Dec 13, 2015 at 19:30
  • 8
    This is not the official recommend way to do it. Using 'off' is the correct way. Jul 30, 2017 at 21:31
  • I made it /var/null and now it start saving logs in file named "null" in var directory. Should it be "/dev/null" only?
    – Gaurav
    Oct 17, 2017 at 12:48
  • yes, /dev/null is a special Unix file (aka null device). It's a unique type of file that discards all data written to it but reports that the write operation succeeded.
    – kekko12
    Nov 30, 2023 at 15:53
25

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.

8
  • 2
    How is this adding to the earlier answers?
    – Dave M
    Dec 13, 2015 at 17:42
  • 1
    Cuz, previous answer is unuseful! Dont use that
    – MIT
    Apr 4, 2017 at 15:51
  • 2
    If the config doesn't contain an error_log option nginx defaults to "error_log logs/error.log error": nginx.org/en/docs/ngx_core_module.html#error_log Mar 2, 2018 at 7:41
  • 1
    @DanielKhan no! read it, nginx.org/en/docs/http/ngx_http_log_module.html
    – MIT
    Dec 20, 2019 at 9:54
  • 1
    @DanielKhan, here what it says; ``` access_log path [format [buffer=size] [gzip[=level]] [flush=time] [if=condition]]; access_log off; ``` If you want to turn it off, you can just set it off. Yeah, it might be. They have a lot access_log fixes due to their change logs.
    – MIT
    Dec 24, 2019 at 13:57
15

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 servers 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 servers which are various API gateways.

Then it's not enough to disable access logs in some of the API gateway servers (or their locations) 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;
    }
    (...)
  }

}


2
  • What about error_log off;? Feb 26, 2022 at 20:02
  • @MelroyvandenBerg: unfortunately nginx is not consistent here and while off IS a special value for access_log directive, it IS NOT for error_log. Feb 26, 2022 at 21:09
0

It should be pretty simple, you could comment the log lines out

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;

and if you do get a crit errors you should do it like this : ´error_log /dev/null crit;´ so it will be :

server {
    listen       80;
    server_name  _;
    access_log  /dev/null crit;
  ...

server {
    listen       80;
    server_name  example.com
    access_log  /dev/null crit;
    error_log /dev/null crit;
3
  • 1
    Don't use 'off'. As mentioned below, this will create a log file called 'off' in nginx's home dir.
    – DanielKhan
    May 11, 2019 at 7:49
  • No, it’s not like that anymore with the current versions of nginx, @DanielKhan. Jan 8, 2020 at 17:25
  • Commenting out access_log in server level won’t work as it is enabled by default, @Daniel. Jan 8, 2020 at 17:26

You must log in to answer this question.

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