nginx获取大文件MD5值(nginx模块ngx_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
    # wget http://nginx.org/download/nginx-1.4.2.tar.gz
    # tar -xzf nginx-1.4.2.tar.gz
    # cd nginx-1.4.2
    # ./configure --prefix=/usr/local/nginx-1.4.2  --add-module=../file-md5-master
    # make
    # make isntall
如果你已经安装了nginx,仅需要增加file-md5模块即可,具体参考《 nginx如何安装第三方模块》 3. 配置file-md5 3.1 MD5追加到http响应头中
    server {
        listen       80;
        server_name  test.ttlsa.com;
        root /data/site/test.ttlsa.com;
    
        # for add content-md5 to http header
        location ~ /download
        {
                add_header    Content-MD5    $file_md5;
        }
    }
所有请求download的请求,都会在响应http头部增加Content-MD5,值为这个文件的MD5,看如下测试:
    # curl -I test.ttlsa.com/download/1.exe   
    HTTP/1.1 200 OK
    Server: nginx
    Date: Wed, 26 Feb 2014 03:00:05 GMT
    Content-Type: application/octet-stream
    Content-Length: 1535488
    Last-Modified: Mon, 24 Feb 2014 10:08:10 GMT
    Connection: keep-alive
    ETag: "530b1a0a-176e00"
    Content-MD5: 6adda4a06dbad3ac9b53a08f4ff9c4f8
    Accept-Ranges: bytes
大家可以看到Content-MD5: 6adda4a06dbad3ac9b53a08f4ff9c4f8,这个就是1.exe文件的MD5值. 3.2 直接响应MD5值到内容中
    server {
        listen       80;
        server_name  test.ttlsa.com;
        root /data/site/test.ttlsa.com;

        # for add content-md5 to http header
        location ~ /download
        {
            if ( $arg_md5 ~* "true" ){
                echo $file_md5;
            }
        }
    }
这边直接使用echo输出MD5值(echo模块需要额外安装),只需在下载的文件后面加上参数&md5=true即可得到MD5值,使用过程中,参数可以随心定义。下面来测试一下。
    # curl test.ttlsa.com/download/1.exe?md5=true   
    6adda4a06dbad3ac9b53a08f4ff9c4f8
直接得到md5值,与第一种方法得到同样的MD5。 4. 最后 使用nginx模块也是一种方法,这种方法有个不足支持,每个请求都需要从新计算一次MD5值。想减小他的压力,可以在nginx加缓存,或者借用memcache以及使用perl或者lua等模块,希望大家继续支持运维生存时间。 项目地址:https://github.com/cfsego/file-md5 项目文档:https://github.com/cfsego/file-md5/blob/master/README 网站:运维生存时间

转载于:https://my.oschina.net/766/blog/211249

猜你喜欢

转载自blog.csdn.net/weixin_33798152/article/details/91547478