Nginx define logs in nginx.conf vs. server blocks

September 7, 2014 2.2k views

If I enable access and error logs in /etc/nginx/nginx.conf:

access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;

Should I also be keeping logs in server blocks like:

server {
    listen 80;
    server_name www.somesite.org;
    root /var/www/somesite/;
    access_log /var/log/nginx/somesite.org.access.log;
    error_log /var/log/nginx/somesite.org.error.log;

Or this is redundant assuming that everything that's in "somesite" specific logs is already captured by master logs defined in nginx.conf?

I do have several virtual hosts though and I am wondering if above is true, would it be a better practice to disable logging in nginx.conf and log in individual server blocks so that I can keep domain name specific logs nice and clean?

2 Answers

If you have several virtual hosts, it's generally good practice to separate the log files by defining site specific logs in your server block. Though it's really just a matter of preference and readability. So it's up to you.

  • @asb if accesslog is defined in every virtualhost do you think that it could be a problem disabling the main accesslog in /etc/nginx/nginx.conf ?

    this can bring to problems? for example losing logs records.

    • @foolab Generally, leaving the defaults in /etc/nginx/nginx.conf is preferable. Setting the log location at the server block level will override them for everything that terminates there. (Or as the docs put it "Requests are logged in the context of a location where processing ends.")So if all your server blocks have logs locations set, nothing would normally end up in the default log files. So anything that did would be something out of the ordinary that you'd want to know.

      So really there is no reason not to, and if you don't you may potentially loose some information.

      • Ohhh maybe now I can see what's happening!

        I now notice that all the logs in my default access.log are 301.
        The first server block in my configs don't have logs configured. I thought that were double records instead were all 301 redirects.

        I have this kind of configuration:

        server {
            server_name domain.com www.domain.com;
            listen XXX.XXX.XXX.XXX;
            rewrite ^ https://$server_name$request_uri? permanent;
        server {
            server_name domain.com www.domain.com;
            listen XXX.XXX.XXX.XXX:443 ssl;
            #ssl_certificate /home/domain/ssl.cert;
            ssl_certificate /home/domain/chained.cert;
            ssl_certificate_key /home/domain/ssl.key;
            ssl_protocols TLSv1.1 TLSv1.2;
            root /home/domain/public_html;
            index index.html index.htm index.php;
            access_log /var/log/nginx/domain.com_access_log;
            error_log /var/log/nginx/domain.com_error_log;

        can be that the problem?

        edited by asb
Have another answer? Share your knowledge.