1

More precisely can I configure Nginx to output the log entry to a separate log file whenever a particular url is requested? Later the number of lines in that log can give me the download count.

nginx.conf

error_log  /home/webadmin/applegrew.com/error-n.log;

events {
    worker_connections  1000;
}


http {
    include       mime.types;
    index         index.html index.htm index.php index.shtml;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';


    sendfile        on;
    keepalive_timeout 5; 

    gzip  on;

    server {
        server_name cink.applegrew.com;

        access_log /var/log/cink.applegrew.com/access.log main;
        error_log  /var/log/cink.applegrew.com/error.log;

        root /var/www/cink.applegrew.com/html;

        location = /js/cink\.compiler\.min\.js$ {
            access_log /var/log/cink.applegrew.com/download-compiler-min.log main;
        }
        location = /js/cink\.renderer\.min\.js$ {
            access_log /var/log/cink.applegrew.com/download-renderer-min.log main;
        }
        location = /js/cink\.combined\.min\.js$ {
            access_log /var/log/cink.applegrew.com/download-combined-min.log main;
        }
        location /js/src/ { #This works
            access_log /var/log/cink.applegrew.com/download-src.log main;
        }

        include cacheCommon.conf;
    }

}

cacheCommon.conf

location ~* \.(?:ico|css|js|gif|jpe?g|png|txt|xml)$ {
    # Some basic cache-control for static files to be sent to the browser
    expires max;
    add_header Pragma public;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
}

3 Answers 3

1

Could you please post the full configuration file?

Another way is use grep -c:

grep -c "/js/cink.renderer.min.js" /path/to/the/main/access_log

UPDATE

location = /js/cink\.renderer\.min\.js$ {
            access_log /var/log/cink.applegrew.com/download-renderer-min.log main;
        }
  1. Remove all the backslashs before the dot.
  2. Either remove the $ at the end or use ~ instead of =. $ means the end of line in RegEx, so you must use the ~ prefix.
8
  • Yes this is possible, but I would prefer keeping individual logs. This is just a personal preference.
    – AppleGrew
    Sep 20, 2011 at 7:54
  • Please show us the entire of configuration file. Do you mean the log file is blank when you said "doesn't work"?
    – quanta
    Sep 20, 2011 at 8:25
  • Yes the logs files are created but remain blank.
    – AppleGrew
    Sep 20, 2011 at 8:33
  • How we can help without config file?
    – quanta
    Sep 20, 2011 at 8:39
  • Provided now. :-)
    – AppleGrew
    Sep 20, 2011 at 8:40
2

this probably would work in your nginx vhost for the site

location ~* /path/to/file.ext$ {
    access_log /var/log/some/seperate.log;
}

where the /path/to/file.ext is relative to your webroot

2
  • It doesn't seem to work. I have url of the form - http://cink.applegrew.com/js/cink.renderer.min.js. For this I tired with /js/cink.renderer.min.js. I also tried escaping the .. It works if I give /js/, but I want to log one specific file.
    – AppleGrew
    Sep 20, 2011 at 7:21
  • see Iain's answer, the location tag needs tweaking, i've corrected mine on that basis - the root tag isnt strictly required and make sure this appears above any other rules (eg above where you include cacheCommon.conf) Sep 20, 2011 at 12:45
1

This should work

location ~* /js/cink.renderer.min.js$ {
        root /var/www/cink.applegrew.com/html;
        access_log /path/to/separate/log/file;
    }

You must log in to answer this question.

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