文档章节

Nginx post action to trigger successfully download file

mickelfeng
 mickelfeng
发布于 2017/03/24 15:30
字数 588
阅读 70
收藏 0

As you know nginx is a lightweigh but very powerful http server, and it has a lot of cool features.

Suppose that you have an nginx server hosts some files, and you want to track how many people has successfully download and how many has fails or cancel. This post will show you how to do that with nginx post_action directive.
 

Requirement :

Let's see the picture below

Topology 

In this LAB, I'll use two servers :

  • Host files server running nginx, IP address = 10.254.10.30 
  • Counting server : host some webservice .php page (running by apache httpd) to count the download session, maybe connect to some database for store the info. IP address = 10.254.10.31 

Steps in detail :

Create some test file on the NginxServer.

NginxServer# cd /usr/share/nginx/html/
NginxServer# dd if=/dev/zero of=file.bin bs=1M count=100
100+0 records in
100+0 records out
104857600 bytes (105 MB) copied, 0.113983 seconds, 920 MB/s

On the client, try download the file.

Client# wget http://10.254.10.30/file.bin

http://10.254.10.30/file.bin
Connecting to 10.254.10.30:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 104857600 (100M) [application/octet-stream]
Saving to: `file.bin'
100%[==========================================================>]
104,857,600 54.4M/s in 1.8s
09:35:34 (54.4 MB/s) - `file.bin' saved [104857600/104857600]

Checking the logfile on the NginxServer

NginxServer# tail /var/log/nginx/access.log 

10.254.10.29 - - [21/Jun/2012:09:35:31 +0700] "GET /file.bin HTTP/1.1" 200 104857600 "-" "Wget/1.13.4 (linux-gnu)" "-"

Yes, you can see the logfile that client has downloaded the file, but you can not know whether the client successfully or fail or cancel the download because in the logfile there is no entry for that infomation.

Let's add some configuration in the NginxServer :

NginXServer# vim /etc/nginx/nginx.conf

location /
{
 root /usr/share/nginx/html;
 index index.html index.htm;
 post_action /afterdownload;
}

location /afterdownload
{
 proxy_pass http://10.254.10.31/counting.php?FileName=$request&ClientIP=$remote_addr&body_bytes_sent=$body_bytes_sent&status=$request_completion;
 internal;
}

Yes, after finishing a download session (no matter what if successful | fails | user cancel), the nginx server will make a proxy call to some external URL (in this case, i've point it to the CountingServer).

Let's download the file again.

Client# wget http://10.254.10.30/file.bin

http://10.254.10.30/file.bin
Connecting to 10.254.10.30:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 104857600 (100M) [application/octet-stream]
Saving to: `file.bin'
100%[=========================================================>]

104,857,600 52.4M/s in 1.9s
10:14:08 (52.4 MB/s) - `file.bin' saved [104857600/104857600]

Check the CountingServer log.
 

CountingServer# tail /var/log/httpd/access.log

10.254.10.30 - - [21/Jun/2012:10:14:06 +0700] "GET /counting.php?FileName=GET /file.bin HTTP/1.1&ClientIP=10.254.10.29&body_bytes_sent=104857600&status=OK HTTP/1.0" 200 10 "-" "Wget/1.13.4 (linux-gnu)"

Yeah. The access log is very clearly. The file has been successfully downloaded (status=OK).

Let's download the file again, and cancel it when processing (by pressing Ctrl+c)

Client# wget http://10.254.10.30/file.bin

http://10.254.10.30/file.bin
Connecting to 10.254.10.30:80... connected.
HTTP request sent, awaiting response... 200 OK
Length: 104857600 (100M) [application/octet-stream]
Saving to: `file.bin'
63% [=========================================> ] 66,981,354 53.2M/s

Check the log of the CountingServer.
 

CountingServer# tail /var/log/httpd/access.log

10.254.10.30 - - [21/Jun/2012:10:13:39 +0700] "GET /counting.php?FileName=GET /file.bin HTTP/1.1&ClientIP=10.254.10.29&body_bytes_sent=75427840&status= HTTP/1.0" 200 10 "-" "Wget/1.13.4 (linux-gnu)"

The entry "status= " has been blanked instead of "OK". That mean the progress was terminated when going on (maybe user cancel)

So, by using nginx post_action directive, you can now trigger an action after client finish downloading.

http://blog.163.com/liuzhizhi123@126/blog/static/10412096620154511568245/

本文转载自:http://www.tipstuff.org/2012/08/Nginx-post-action-to-trigger-successfully-download-file.html

mickelfeng

mickelfeng

粉丝 237
博文 2802
码字总数 605868
作品 0
成都
高级程序员
私信 提问
The detailed analysis of WordPress 5.0 RCE

Author:LoRexxar'@Knownsec 404 Team Chinese Version:https://paper.seebug.org/822/ On February 20th, the RIPS team released the article WordPress 5.0.0 Remote Code Execution(CV......

知道创宇404实验室
05/17
0
0
Xcode Server + Slack = Rockstar Combo for iOS CI/CD

Xcode Server is a Continuous Integration service provided by Apple to analyze, build, test, and archive iOS apps. Xcode Server has been recently inbuilt with Xcode 9, which made......

Shashikant Jagtap
2017/12/12
0
0
zabbix 从0开始学习的步骤

注明:以下学习过程参考了很多文档,如有问题,请留言联系!谢谢 一、系统版本 CentOS-6.5-x86_64 二、环境 1、主机名 1.1 hostname 1.2 vim /etc/sysconfig/network HOSTNAME= 2、网卡 vim ...

fanzh565656
2016/11/30
0
0
Nginx学习之五:Nginx第三方模块

第三方模块 这些模块不被官方所支持,亦未必能跨越Nginx的版本兼容。不过其中有些模块证明是对多数用户是有用的。只在于个人的风险问题。 编译第三方模块 第三方模块和 Nginx资源是一起被编译...

boonya
2014/07/04
4.4K
0
lnmp安装自动化脚本

说明:centos6 64位,php5.2.17,nginx0.8.54,mysql5.1.60 脚本文件: lnmp.sh #!/bin/sh softinstalldir=/opt/softworeinstall nginxdir=$softinstalldir/nginx phpdir=$softinstalldir/ph......

kimgshe
2013/07/18
0
0

没有更多内容

加载失败,请刷新页面

加载更多

Spring Boot 2 实战:使用 Spring Boot Admin 监控你的应用

1. 前言 生产上对 Web 应用 的监控是十分必要的。我们可以近乎实时来对应用的健康、性能等其他指标进行监控来及时应对一些突发情况。避免一些故障的发生。对于 Spring Boot 应用来说我们可以...

码农小胖哥
38分钟前
4
0
ZetCode 教程翻译计划正式启动 | ApacheCN

原文:ZetCode 协议:CC BY-NC-SA 4.0 欢迎任何人参与和完善:一个人可以走的很快,但是一群人却可以走的更远。 ApacheCN 学习资源 贡献指南 本项目需要校对,欢迎大家提交 Pull Request。 ...

ApacheCN_飞龙
49分钟前
4
0
CSS定位

CSS定位 relative相对定位 absolute绝对定位 fixed和sticky及zIndex relative相对定位 position特性:css position属性用于指定一个元素在文档中的定位方式。top、right、bottom、left属性则...

studywin
58分钟前
6
0
从零基础到拿到网易Java实习offer,我做对了哪些事

作为一个非科班小白,我在读研期间基本是自学Java,从一开始几乎零基础,只有一点点数据结构和Java方面的基础,到最终获得网易游戏的Java实习offer,我大概用了半年左右的时间。本文将会讲到...

Java技术江湖
昨天
5
0
程序性能checklist

程序性能checklist

Moks角木
昨天
7
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部