Global NGINX server / location directive to redirect file downloads

I have a PHP script that keeps track of file downloads. This works pretty well for a single site, but I want to keep track of downloads across 20 NGINX websites.

My theory is that if I add a server block with a location directive in the main /etc/nginx/nginx.conf file, before the sites-enabled directive, this might work.

But the code below doesn’t seem to get executed:

cat /etc/nginx/nginx.conf

##
# Virtual Host Configs
##

server {

location /download/ {
   rewrite /download/(.*).(rar|zip|pdf)$ /home/_downloads/download.php?file=$1.$2;
   }
}

include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
server_names_hash_bucket_size 128;

I’m also not sure about the second part, if I can just specifiy an absolute path to the file system or if I’m supposed to specify a URL. At this point I would prefer an absolute path if possible.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.