How to notify how to fully download resources in Nginx

I need to know that resources are completely downloaded from the server. My server is configured with NginX web server, and I want to do something if and only if the resources are completely downloaded by the user.
If you use Nginx to process download files (using XSendfile), you should configure the download in Nginx (in the “Server” block) Add a specific access_log block to the processing block. It will look like this:

location /download_music/ {
internal;
root /usr /share/nginx/MY_SITE/media/music;
access_log /var/log/nginx/download.MY_SITE.log download;
}

The “download” at the end of the access_log line The word is actually a log format, you should add it to the nginx main configuration file (/etc/nginx/nginx.conf) in the “http” block. It may look like this:

< /p>

http {
...

log_format download'{ "remote_addr": "$remote_addr", "time":"$time_local", "request":" $request",'
'"traffic":$body_bytes_sent, "x_forwarded_for":"$http_x_forwarded_for" }';

...
}

You can change this format to what you want (you will use it in the script later). If you monitor this log file (use “tail -f /var/log/nginx/download.MY_SITE.log” ), you will see that every time the download is paused or the download is completed, a line of log will be added to this file.
The next step is to use rsyslog and “imfil e” and “omprog” modules. You should add these configurations at the end of the rsyslog (/etc/rsyslog.conf) configuration file:

$ModLoad imfile
$ InputFileName /var/log/nginx/download.MY_SITE.log
$InputFileTag nginx-access
$InputFileStateFile state-nginx-access
$InputFileSeverity info
$InputFileFacility local3
$InputFilePollInterval 1
$InputRunFileMonitor

$ModLoad omprog
$actionomprogbinary /usr/share/nginx/MY_SITE/scripts/download.py
$template LogZillaDbInsert," %hostname:::lowercase%\9%pri%\9%programname%\9%msg%\n"
local3.* :omprog:;RSYSLOG_TraditionalFileFormat

Note this line:

/usr/share/nginx/MY_SITE/scripts/download.py

This is the address of the script, every time a log entry is added to the log file Call the script, and in this script will use (in Python code) to provide the entire log entry:

line = sys.stdin.readline()

< p> Then, you can parse the line and find what has been recorded, including the downloaded file size (on each pause/end event). Now, you just need to include the file size in the download URL and then in this script Retrieve it and compare it with the downloaded bytes. If these two numbers are equal, it means that the download has been successfully completed. In addition, you can perform any other operations in this script (for example, expire download links, increase database Downloads,…)

I need to know that resources are completely downloaded from the server. My server is configured with NginX web server, when and I want to do something only when the resource is completely downloaded by the user.

If you use Nginx to process the download file (using XSendfile), you should configure it in Nginx (in ” Add a specific access_log block to the download processing block of the server” block. It will look like this:

location /download_music/ {
internal;
root /usr/share/nginx/MY_SITE/media/music;
access_log /var/log/nginx/download.MY_SITE.log download;
}

access_log The word “download” at the end of the line is actually a log format, you should add it to the nginx main configuration file (/etc/nginx/nginx.conf) in the “http” block. It might look like this:< /p>

http {
...

log_format download'{ "remote_addr": "$remote_addr", "time":"$time_local ", "request":"$request",'
'"traffic":$body_bytes_sent, "x_forwarded_for":"$http_x_forwarded_for" }';

...
}

You can change this format to your desired format (you will use it in the script later). If you monitor this log file (use “tail -f /var/log/nginx /download.MY_SITE.log”), you will see that every time the download is paused or the download is completed, a log line will be added to this file.
The next step is to use rsyslog and the “imfile” and “omprog” modules. You These configurations should be added at the end of the rsyslog (/etc/rsyslog.conf) configuration file:

$ModLoad imfile
$InputFileName /var/log/ng inx/download.MY_SITE.log
$InputFileTag nginx-access
$InputFileStateFile state-nginx-access
$InputFileSeverity info
$InputFileFacility local3
$InputFilePollInterval 1
$InputRunFileMonitor

$ModLoad omprog
$actionomprogbinary /usr/share/nginx/MY_SITE/scripts/download.py
$template LogZillaDbInsert,"%hostname:::lowercase %\9%pri%\9%programname%\9%msg%\n"
local3.* :omprog:;RSYSLOG_TraditionalFileFormat

Note this line:

< /p>

/usr/share/nginx/MY_SITE/scripts/download.py

This is the address of the script, which will be called every time a log entry is added to the log file, and in This script will use (in Python code) to provide the entire log entry:

line = sys.stdin.readline()

Then you can Parse the line and find what has been recorded, including the downloaded file size (on each pause/end event). Now, you just need to include the file size in the download URL, then retrieve it in this script, and add it Compare with the downloaded bytes. If these two numbers are equal, the download has been successfully completed. In addition, you can perform any other operations in this script (for example, expire download links, increase the number of downloads of the database,…)< /p>

Leave a Comment

Your email address will not be published.