如何在 Nginx 中保存某个 URL 的下载次数?

如何在 Nginx 中保存某个 URL 的下载次数?

更准确地说,我是否可以配置 Nginx,以便每当请求特定 URL 时将日志条目输出到单独的日志文件?稍后,该日志中的行数可以为我提供下载计数。

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;
    }

}

缓存通用配置文件

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";
}

答案1

您能发布完整的配置文件吗?

另一种方法是使用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/cink.applegrew.com/download-renderer-min.log main;
        }
  1. 删除点前的所有反斜杠。
  2. 删除$末尾的 或使用~代替=$在 RegEx 中表示行尾,因此必须使用前缀~

答案2

这可能适用于你的网站的 nginx vhost

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

其中 /path/to/file.ext 相对于你的 webroot

答案3

这应该有效

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

相关内容