nginx安装第三方模块file_md5

HTTP协议新增了Content-MD5 HTTP头,但是nginx并不支持这个功能,而且官方也明确表示不会增加这项功能,为什么呢?因为每次请求都需要读取整个文件来计算MD5值,以性能著称的nginx绝对不愿意干出违背软件宗旨的事情。但是有些应用中,需要验证文件的正确性,有些人通过下载当前文件,然后计算MD5值来比对当前文件是否正确。不仅仅浪费带宽资源也浪费了大把的时间。有需求就有解决方案,网友开发了file-md5模块。


1. 下载模块file-md5

# cd /usr/local/src

# wget https://github.com/cfsego/file-md5/archive/master.zip -O file-md5-master.zip

# unzip file-md5-master.zip


2. 安装模块file-md5,之前已经安装过nginx,所以只添加模块,make即可;

注意:不用make install;


[root@localhost ~]# cd /usr/local/src/nginx-1.6.2

[root@localhost nginx-1.6.2]# ./configure --prefix=/usr/local/nginx --with-pcre --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --add-module=../file-md5-master

[root@localhost nginx-1.6.2]#make

[root@localhost nginx-1.6.2]#cp objs/nginx /usr/local/nginx/sbin/

[root@localhost nginx-1.6.2]# /usr/local/nginx/sbin/nginx


3. 配置file-md5

3.1 MD5追加到http响应头中

server

{

listen 80;

server_name 192.168.20.10;

index index.html index.htm index.php;

root /data/test;

location ~ \.php$ {

include fastcgi_params;

fastcgi_pass 127.0.0.1:9000;

fastcgi_index index.php;

fastcgi_param SCRIPT_FILENAME /data/test$fastcgi_script_name;

}


       location ~ /download

       {

               add_header    Content-MD5    $file_md5;

       }

}


所有请求download的请求,都会在响应http头部增加Content-MD5,值为这个文件的MD5,看如下测试:

[root@localhost sbin]# md5sum /data/test/download/lanmp.sh

0899115c968bdfc004fcc956750ab8f2


[root@localhost sbin]# curl -I 192.168.20.10/download/lanmp.sh

HTTP/1.1 200 OK

Server: nginx/1.6.2

Date: Wed, 22 Feb 2017 08:23:34 GMT

Content-Type: application/octet-stream

Content-Length: 8276

Last-Modified: Wed, 22 Feb 2017 08:21:46 GMT

Connection: keep-alive

ETag: "58ad4a1a-2054"

Content-MD5: 0899115c968bdfc004fcc956750ab8f2

Accept-Ranges: bytes


测试同一个文件,不在download目录下,不会有MD5出现;

[root@localhost sbin]# curl -I 192.168.20.10/lanmp.sh

HTTP/1.1 200 OK

Server: nginx/1.6.2

Date: Wed, 22 Feb 2017 08:27:19 GMT

Content-Type: application/octet-stream

Content-Length: 8276

Last-Modified: Wed, 22 Feb 2017 08:27:10 GMT

Connection: keep-alive

ETag: "58ad4b5e-2054"

Accept-Ranges: bytes


3.2 直接响应MD5值到内容中

server {


       location ~ /download

       {

           if ( $arg_md5 ~* "true" ){

               echo $file_md5;

           }

       }

}


这边直接使用echo输出MD5值(echo模块需要额外安装),只需在下载的文件后面加上参数&md5=true即可得到MD5值,使用过程中,参数可以随心定义。


安装echo模块,需要重新编译,方法同上;

#wget https://github.com/openresty/echo-nginx-module/archive/v0.60.zip -O echo-nginx-module.zip

#unzip echo-nginx-module.zip

# ./configure --prefix=/usr/local/nginx --with-pcre --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --add-module=../file-md5-master --add-module=../echo-nginx-module-0.60


curl测试一下:

[root@localhost sbin]# curl 192.168.20.10/download/lanmp.sh?md5=true

0899115c968bdfc004fcc956750ab8f2


直接得到md5值,与第一种方法得到同样的MD5。


4. 最后

使用nginx模块也是一种方法,这种方法有个不足支持,每个请求都需要从新计算一次MD5值。想减小他的压力,可以在nginx加缓存,或者借用memcache以及使用perl或者lua等模块。




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值