The HTTP protocol adds CONTENT-MD5 HTTP headers, but Nginx does not support this feature, and the authorities explicitly say it will not add to this feature. Because each request needs to read the entire file to calculate MD5 value, the performance of the Nginx is absolutely not willing to do things that violate the purpose of the software. But in some applications, you need to verify the correctness of the file, some people download the current file, and then calculate the MD5 value to compare to the current file is correct. It's not just a waste of bandwidth, but a lot of time. Have the demand has the solution, the Netizen developed the FILE-MD5 module.
1. Download 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 have installed the Nginx, just add the FILE-MD5 module.
3. Configure FILE-MD5
3.1 MD5 Append to HTTP response header
server {
listen ;
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 for download request will be added CONTENT-MD5 in response to HTTP headers, the value is MD5 for this file, see the following test:
# curl-i Test.ttlsa.com/download/1.exe
http/1.1 OK
server:nginx
date:wed Feb, 2014 03:00:05 GMT
content-type:application/octet-stream
content-length:1535488
Last-modified:mon, Feb 2014 10:08:10 GMT
connection:keep-alive
ETag: "530b1a0a-176e00"
Content-md5:6adda4a06dbad3ac9b53a08f4ff9c4f8
accept-ranges:bytes
You can see CONTENT-MD5:6ADDA4A06DBAD3AC9B53A08F4FF9C4F8, this is the MD5 value of the 1.exe file.
3.2 Direct response MD5 value to content
server {
listen ;
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;
}
}
}
This is the direct use of ECHO output MD5 value (Echo module requires additional installation), simply add the parameter &md5=true after the downloaded file to get the MD5 value, in the process, the parameters can be defined. Let's test it here.
# Curl Test.ttlsa.com/download/1.exe?md5=true
6adda4a06dbad3ac9b53a08f4ff9c4f8
Get the MD5 value directly and get the same MD5 as the first method.
4. Finally
Using the Nginx module is also a method that has insufficient support, and each request requires a new calculation of the MD5 value. To reduce his stress, you can cache the Nginx, or borrow memcache and use modules like Perl or LUA, and hope that you continue to support operational survival time.
Project Address: HTTPS://GITHUB.COM/CFSEGO/FILE-MD5
Project Documentation: Https://github.com/cfsego/file-md5/blob/master/README
Ps:nginx Large File Download optimization
by default, the Proxy_max_temp_file_size value is 1024MB, that is, the back-end server file is not more than 1G can be cached to the Nginx agent hard disk, if more than 1G, then the file is not cached, Instead, it is forwarded directly to the client. If the proxy_max_temp_file_size is set to 0, the temporary cache is not used.
In a large file environment, if you want to enable temporary caching, you can modify the configuration and change the value to what you want.
Modify Nginx Configuration
Location/
{
...
Proxy_max_temp_file_size 2048m;
...
}
Restart Nginx
#/usr/local/nginx-1.7.0/sbin/nginx-s Reload