Nginx detecting number of clients `curl`ing/`wget`ing or otherwise currently downloading content?

tsujp asked:

I plan to host some bootstrap files that need to be updated every 24 hours, maybe more often. I plan to serve the files using nginx, how can I ensure that I only replace files when 0 people are currently downloading them or otherwise detect how many people are currently downloading the content for my own metrics such that I could have an old/ and current/ bootstrap I can swap between when replacement is required.

My answer:

Use a canonical URL from which you provide a 302 redirect to the URL of the specific version of the file you want to serve at any time. For example:

server {

    location = /download/file.img {
        return 302 /download/file-v1.3.18.img;

Provide a link to /download/file.img to your clients. Whenever you want to change the version, update the nginx configuration and reload nginx.

It doesn’t matter if simultaneous downloads exist at the time you make a change. They will continue to completion and new clients will get the new file.

Your log files will show the specific version any client downloaded, and you can use log analysis tools on them.

View the full question and any other answers on Server Fault.

Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.