Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.


error_log  /home/webadmin/;

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 {

        access_log /var/log/ main;
        error_log  /var/log/;

        root /var/www/;

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

        include 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";
share|improve this question
up vote 1 down vote accepted

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


location = /js/cink\.renderer\.min\.js$ {
            access_log /var/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.
share|improve this answer
Yes this is possible, but I would prefer keeping individual logs. This is just a personal preference. – AppleGrew Sep 20 '11 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 '11 at 8:25
Yes the logs files are created but remain blank. – AppleGrew Sep 20 '11 at 8:33
How we can help without config file? – quanta Sep 20 '11 at 8:39
Provided now. :-) – AppleGrew Sep 20 '11 at 8:40

This should work

location ~* /js/cink.renderer.min.js$ {
        root /var/www/;
        access_log /path/to/separate/log/file;
share|improve this answer

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

share|improve this answer
It doesn't seem to work. I have url of the form - 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 '11 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) – anthonysomerset Sep 20 '11 at 12:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.