How can I avoid users from downloading a big file?

I am serving a static folder with NGINX.

Inside that folder I have one particular file that is 60GB, but it is only meant to be read in small chunks, using an HTTP request like the following:

enter image description here

So if a user tries to download the entire file, NGINX should refuse it.

There are someways I see that this could be done, for eg.:

  1. Check if the HTTP request header Range exists and the byte range is less than X.

  2. Check if the requested response payload is less than, for eg., 100MB.

But I don't know how any of these can be implemented within NGINX.


Solution 1:

I think, that You can configure nginx to check specific location (60gb file location) for specific (Range) header existence. Then You could block requests that don't contain Range header.

You could check these links:

https://stackoverflow.com/questions/18970620/nginx-reject-request-if-header-is-not-present-or-wrong

https://stackoverflow.com/questions/35342049/nginx-reject-request-if-header-not-present/35366796

Edit

It seems a bit problematic coding complex conditions in Nginx configuration. Maybe You should consider serving large file using some server-side scripting, ie. PHP. There are plenty of examples of streaming scripts.

https://gist.github.com/ranacseruet/9826293

After configuring Nginx to using such a script, You need to add logic to script (Range conditions) and also prevent direct access to large file.

Solution 2:

What you're looking for is covered in the Slice module here - https://www.nginx.com/resources/wiki/modules/slice/