Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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";
}
share|improve this question

3 Answers 3

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

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.
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 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 - 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 '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

This should work

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

Your Answer

 
discard

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.