1 mod_zip
最近公司有个需求要求在下载的附件中加入公司的广告,公司的附件主要时zip和rar文件。一共大概有接近200万个,老板让我写一个脚本去在这些附件中添加广告。 我大概估计了一下。这样做可能不太好。于是想通过在下载时把附件和广告打成一个包处理。上网搜索了一下nginx,有一个mod_zip模块可以实现这个功能,在安装时 遇到一些问题,但是使用还是比较简单的,不需要对nginx进行任何配置,只需要php添加相应到header就可以了。
下载mod_zip模块
git clone https://github.com/evanmiller/mod_zip
配置编译
./configure --user=www \ --group=www \ --add-module=../ngx_cache_purge-2.1 \ --prefix=/usr/local/webserver/nginx144 \ --with-http_stub_status_module \ --with-http_ssl_module \ --with-pcre \ --with-openssl=../openssl-1.0.1g \ --add-module=../mod_zip make 编译时可能会有下面的错误报错 ../mod_zip/ngx_http_zip_file.c: 在函数‘ngx_http_zip_generate_pieces’中: ../mod_zip/ngx_http_zip_file.c:331: 错误:‘else’前需要‘}’ cc1: warnings being treated as errors ../mod_zip/ngx_http_zip_file.c:202: 错误:未使用的变量‘cd_piece’ ../mod_zip/ngx_http_zip_file.c: 在文件层: ../mod_zip/ngx_http_zip_file.c:389: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token ../mod_zip/ngx_http_zip_file.c:391: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token ../mod_zip/ngx_http_zip_file.c:392: 错误:expected identifier or ‘(’ before ‘if’ ../mod_zip/ngx_http_zip_file.c:396: 错误:数据定义时没有类型或存储类 ../mod_zip/ngx_http_zip_file.c:396: 错误:在‘cd_piece’的声明中,类型默认为‘int’ ../mod_zip/ngx_http_zip_file.c:396: 错误:‘ctx’未声明(不在函数内) ../mod_zip/ngx_http_zip_file.c:396: 错误:‘piece_i’未声明(不在函数内) ../mod_zip/ngx_http_zip_file.c:397: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token ../mod_zip/ngx_http_zip_file.c:398: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token ../mod_zip/ngx_http_zip_file.c:399: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token ../mod_zip/ngx_http_zip_file.c:401: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token ../mod_zip/ngx_http_zip_file.c:403: 错误:expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘->’ token ../mod_zip/ngx_http_zip_file.c:405: 错误:expected identifier or ‘(’ before ‘return’ ../mod_zip/ngx_http_zip_file.c:406: 错误:expected identifier or ‘(’ before ‘}’ token make[1]: *** [objs/addon/mod_zip/ngx_http_zip_file.o] 错误 1 make[1]: Leaving directory `/data/soft/installnginx144/nginx-1.4.4' make: *** [build] 错误 2
修改源文件 vim ../mod_zip/ngx_http_zip_file.c 到331行改为 } else #endif if(vv->len) { 修改makefile文件 vim objs/Makefile 删除 -Werror 编译 make && make install 这时应该可以编译成功了
安装好以后只需修改php文件即可实现功能
//如果是本地文件
<?php
header('X-Archive-Files: zip');
header('Content-Type: application/octet-stream');
header('X-Accel-Chareset: utf-8');
header('Content-Disposition: attachment; filename="test.zip"');
echo "1a6349c5 24 /file1.txt file1.txt
5d70c4d3 25 /file2.txt file2.txt";
?>
//如果文件大小不确定可以用函数获取文件大小
function getFileSize($url) {
$url = parse_url($url);
if(!!$fp = @fsockopen($url['host'],empty($url['port'])?80:$url['port'],$error)) {
fputs($fp,"GET ".(empty($url['path'])?'/':$url['path'])." HTTP/1.1\r\n");
fputs($fp,"Host:$url[host]\r\n\r\n");
while(!feof($fp)) {
$tmp = fgets($fp);
if(trim($tmp) == '') break;
if(preg_match('/Content-Length:(.*)/si',$tmp,$arr)) return trim($arr[1]);
}
return null;
} else {
return null;
}
header('Content-Disposition: attachment; filename="'.$attachname.'"');
header('Content-type: '.$fileext);
header('X-Archive-Files: zip');
$Len=getFileSize('http://d.paipai.fm/'.$filename);
echo '5d70c4d3 25 /file2.txt file2.txt
- '.$Len.' /'.$filename.' '.$attachname.'.'.$fileext;
安装这个插件最大到启示就是不要迷信开源的代码里不会有简单到错误,的确遇到了就大胆到修改一下。