nginx get large files MD5 value (nginx module ngx_file_md5)

Added HTTP protocol Content-MD5 HTTP header, but nginx does not support this feature, and the official made it clear that this feature will not increase, why? Because each request needs to read the entire file to calculate the MD5 value, performance-known nginx absolutely not willing to do things contrary to the purpose of the software. However, some applications, the need to verify the correctness of the document, some by downloading the current file, and then calculates the MD5 value to the current file is correct comparison. Not only a waste of bandwidth resources are also wasted a lot of time. There is a demand solutions, users develop a file-md5 module. 1. Download the module 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. Installation module 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
If you already have installed nginx, only need to add file-md5 module to the specific reference to " nginx how to install third-party module in" Configuring file-md5 3.1 MD5 appended to the http response header
    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;
        }
    }
All requests download requests are in response to the http header MD5 increase Content-MD5, the value of this document, see the following test:
    # 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
We can see Content-MD5: 6adda4a06dbad3ac9b53a08f4ff9c4f8, this is the MD5 value 1.exe file MD5 value of 3.2 in direct response to the content.
    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;
            }
        }
    }
Here directly MD5 value output echo (echo module requires additional installation), just behind the downloaded files & md5 = true add parameters to obtain the MD5 value, during use, the parameters can be defined heart. Let's test it.
    # curl test.ttlsa.com/download/1.exe?md5=true   
    6adda4a06dbad3ac9b53a08f4ff9c4f8
Md5 value directly, with the first method to obtain the same MD5. 4. Finally, a method is used nginx module, this method has insufficient support, each request requires a recalculation of the MD5 value. Want to reduce the pressure on him, you can add cache nginx, or borrow and use memcache lua or perl modules, we hope you continue to support the operation and maintenance of survival time. Project address: https: //github.com/cfsego/file-md5 project documentation: https: //github.com/cfsego/file-md5/blob/master/README website: operation and maintenance of survival time

Reproduced in: https: //my.oschina.net/766/blog/211249

Guess you like

Origin blog.csdn.net/weixin_33798152/article/details/91547478