[nginx]统计文件下载是否完整思路(flask)

时间:2023-03-09 03:02:29
[nginx]统计文件下载是否完整思路(flask)

有一个需求是统计文件是否被用户完整下载,因为是web应用,用js没有找到实现方案,于是搜索下nginx的实现方案,把简单的探索过程记录下。

实验一

  • 最原始的思路,查看日志,下载了一个文件之后我们看日志的传输的文件大小跟文件原始的大小是否一致
  • 测试要下载的文件的大小

[nginx]统计文件下载是否完整思路(flask)

  • 一次完整下载的log 跟一次没下载完成的log,可以通过对比传输字节的大小来判断

[nginx]统计文件下载是否完整思路(flask)

这种方式就是根据日志来做统计,每隔一段时间分析日志得到结果,有些麻烦,时效性不好。

实验二:

找了相关的博客

大概的流程:

[nginx]统计文件下载是否完整思路(flask)

主要的工作就是2个

1 修改nginx的配置,把下载文件的信息转发到统计服务或者url

2 统计服务记录和判断文件下载状态

这里的重点是使用nginx 的post_action参数, 在下载请求结束之后把下载的情况发送给另一个统计服务,由统计服务来判断文件下载的情况

配置类似

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;
}

然后写个一个flask 来接收统计请求

    #!/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统计用户下载文件字节

    '''

    from flask import Flask, request
    app = Flask(__name__)

    @app.route("/counting")
    def counting():
        req = request.args.get("FileName")
        clientip = request.args.get("ClientIP")
        size = request.args.get("body_bytes_sent")
        status = request.args.get("status")
        print "request  ", req
        print "ip  ", clientip
        print "size  ", size
        print "status  ", status
        return "ok"

    if __name__ == "__main__":
        app.run(port=8888, debug=True)

访问的日志

lzz@ubuntu:code$ python counting_file.py
 * Running on http://127.0.0.1:8888/
 * Restarting with reloader
request   /index.html
ip   10.0.1.16
size   0
status   OK
127.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.zip
ip   10.0.1.16
size   225280
status
127.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 -

只要在flask中做处理就可以统计用户下载的情况了。

上面的文章也说了,当用户使用多个连接下载的时候可能就有问题了,会重复统计,结果也会不准确,所以还有很多改进空间.

声明:

本文出自 “orangleliu笔记本” 博客,转载请务必保留此出处http://blog.csdn.net/orangleliu/article/details/44219213

作者orangleliu 采用署名-非商业性使用-相同方式共享协议