The principle of building image caching mechanism is to reduce the burden of database and speed up the response of static resources.
Steps:
1.vim
/usr/local/nginx/conf/nginx
.conf
2.
http{
...
...
#设置Web缓存区名称为cache_one,内存缓存空间大小为200MB,1天没有被访问的内容自动清除,硬盘缓存空间
大小为30GB
proxy_cache_path
/var/imgcachetest
levels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;
}
Where/var/imgcachetest is the mkdir image cache directory. 3.
server{
...
location / {
proxy_cache cache_one;
proxy_cache_valid 200 304 12h;
#以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http:
//127
.0.0.1:9000;
expires 1d;
}e
}
4. When you access the test picture, a picture cache is generated in the/var/imgcachetest directory, complete.
Nginx+proxy_cache Picture Cache