阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

Nginx安装第三方模块file_md5

116次阅读
没有评论

共计 3395 个字符,预计需要花费 9 分钟才能阅读完成。

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 等模块。

下面关于 Nginx 的文章您也可能喜欢,不妨参考下:

CentOS 7.2 下编译安装 PHP7.0.10+MySQL5.7.14+Nginx1.10.1  http://www.linuxidc.com/Linux/2016-09/134804.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

Linux 下编译安装 Nginx 1.8.1 及配置 http://www.linuxidc.com/Linux/2017-02/140495.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

Nginx 的 500,502,504 错误解决方法 http://www.linuxidc.com/Linux/2015-03/115507.htm

Nginx 版本号修改隐藏及记录用户请求需要的时间  http://www.linuxidc.com/Linux/2017-02/141044.htm

CentOS 7 编译安装 Nginx1.10.2 脚本启动失败解决思路 http://www.linuxidc.com/Linux/2017-01/139794.htm

Nginx 的详细介绍:请点这里
Nginx 的下载地址:请点这里

本文永久更新链接地址:http://www.linuxidc.com/Linux/2017-02/141080.htm

正文完
星哥说事-微信公众号
post-qrcode
 
星锅
版权声明:本站原创文章,由 星锅 2022-01-21发表,共计3395字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中