[Nginx] Statistics file download is complete idea (flask)

Source: Internet
Author: User
Tags php tutorial

There is a requirement is whether the statistical files are fully downloaded by the user, because it is a Web application, with JS did not find the implementation of the scheme, so search the implementation of Nginx, the simple exploration process recorded.

Experiment One

    • The original idea, look at the log, after downloading a file, we look at the log transfer file size is consistent with the original size of the file
    • Test the size of the file to be downloaded

    • Log with a full download and a log with no download, you can compare the size of the transmitted bytes to determine

This way is based on the log to do statistics, every time to analyze the log to get results, some trouble, timeliness is not good.

Experiment Two:

Found the relevant blog

    • Counting-100-completed-downloads
      Using Post_action (the way it is used below)

    • Nginx-post-action-to-trigger-successfully-download-file problems encountered Nginx-post-action-proxy-pass-track-downloading-file

    • Using X-accel-redirect

      • Nginx-x-accel-redirect-php-rails English
      • Download control using Nginx X-accel-redirect header Chinese

The approximate process:

The main job is 2.
1 Modify Nginx configuration, the download file information forwarded to the Statistics Service or URL
2 Statistics Service record and determine file download status

The focus here is to use the Nginx post_action parameter to send the download to another statistic service after the download request is completed, and the statistical service to determine the file download situation.

Configuration Similar

location / {    limit_rate 20k;    post_action @afterdownload;}location @afterdownload {    proxy_pass http://127.0.0.1:8888/counting?FileName=$uri&ClientIP=$remote_addr&body_bytes_sent=$body_bytes_sent&status=$request_completion;    internal;}

Then write a flask to receive the statistics request

#!/usr/bin/python#-*-coding:utf-8-*-#############################File Name:counting_file.py#Author: Orangleliu#Mail: orangleliu@gmail.com#Created time:2015-03-11 16:41:05#License: MIT############################" nginx Statistics user download file bytes" fromFlaskImportFlask, request app = Flask (__name__)@app. Route ("/counting") defcounting():req = Request.args.get ("FileName") ClientIP = Request.args.get ("ClientIP") size = Request.args.get ("Body_bytes_sent") status = Request.args.get ("Status")Print"Request", reqPrint"IP", ClientIPPrint"Size", sizePrint"Status", statusreturn"OK"if__name__ = ="__main__": App.run (port=8888, debug=True)

Log of Access

lzz@ubuntu:code$ python counting_file.py * Running on http://127.0.0.1:8888/ * Restarting with reloaderrequest   /index.htmlip   10.0.1.16size   0status   OK127.0.0.1 - - [12/Mar/2015 10:42:59] "GET /counting?FileName=/index.html&ClientIP=10.0.1.16&body_bytes_sent=0&status=OK HTTP/1.0" 200 -request   /Pillow-2.3.0.zipip   10.0.1.16size   225280status127.0.0.1 - - [12/Mar/2015 10:43:14] "GET /counting?FileName=/Pillow-2.3.0.zip&ClientIP=10.0.1.16&body_bytes_sent=225280&status= HTTP/1.0" 200 -

As long as the processing in the flask can be counted to download the situation of users.
The above article also said, when users use multiple connections download may have problems, will be repeated statistics, the results will be inaccurate, so there is a lot of room for improvement.

Statement:
This article is from the "Orangleliu Notebook" blog, reproduced please be sure to keep this source http://blog.csdn.net/orangleliu/article/details/44219213
Author Orangleliu using Attribution-NonCommercial-sharing protocol in the same way

The above describes the [nginx] statistical file download is a complete idea (flask), including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.