0

I found excellent rewrite rules and a PHP script that works with Apache to count the number of downloads for certain file types. I need this converted to be NGINX friendly. The Apache rewrite rules are:

RewriteEngine on    
RewriteRule ^(.*).(rar|zip|pdf)$ http://xy.com/downloads/download.php?file=$1.$2 [R,L]    

The download script is here.

I'm trying to do the same with NGINX:

When any pdf file is downloaded, the script must intercept the download so that I can keep track of it. I want to keep track of two things, the amount of times it's been downloaded, and the IP address of the download.

The closest I got to was this:

location / {
   rewrite ^(.*).(rar|zip|pdf)$ /track-downloads.php?file=$1 break;
}

But the rule doesn't work, it doesn't get called. Also note sure about the location tag.

I found some other tips on rewrite rule conversion on NGINX's blog, here, but those conversions are the rather simplistic mainstream ones.

I've googled this to death. The problem with googling phrases like "execute custom php script when downloading PDF file nginx" is that so many hits are returned with regards to PHP not working properly and wanting to download instead of serve files.

Any help or direction would be greatly appreciated. Just as a side note, but two NGINX environments are Laravel Valet and the Virtualmin control panel.

  • What do you mean by _"it doesn't get called"_? You get the content of `/track-downloads.php` or a _"File not found"_ error in **Nginx** error log? Can you add at least the names of your other `location` blocks? – Piotr P. Karwasz Apr 05 '20 at 16:29
  • hows about `location ~* { rewrite ^/(.*).(rar|zip|pdf)$ http://xy.com/downloads/download.php?file=$1.$2 redirect; }` – djdomi Apr 05 '20 at 19:43

0 Answers0