Preface
Nginx is a high-performance HTTP server, which can be cached by proxy cache for static resources. The principle is that the static resources in accordance with certain rules of the local hard disk, and will be in memory to cache commonly used resources, so as to speed up the response of static resources. Configure proxy Cache
The following is a nginx configuration fragment:
Proxy_temp_path/usr/local/nginx/proxy_temp_dir 1 2; #keys_zone =cache1:100m that this zone name is called CACHE1, the allocated memory size is 100MB #/usr/local/nginx/proxy_cache_dir/cache1 Indicates the directory that cache1 this zone file to hold #levels =1:2 indicates that the first level directory of the cache directory is 1 characters and the second level directory is 2 characters, that is,/usr/local/nginx/proxy_cache_dir/cache1/a/ 1b This form #inactive =1d means that the cached file in this zone is not accessed within 1 days, then the file is deleted by the cache manager process #max_size =10g indicates that the zone hard disk is 10GB Proxy_cache
_path/usr/local/nginx/proxy_cache_dir/cache1 levels=1:2 keys_zone=cache1:100m inactive=1d max_size=10g;
server {Listen 80;
server_name *.example.com; #在日志格式中加入 $upstream _cache_status log_format format1 ' $remote _addr-$remote _user [$time _local] ' "$request" $
Status $body _bytes_sent ' "$http _referer" "$http _user_agent" $upstream _cache_status ';
Access_log Log/access.log FOMAT1;
# $upstream _cache_status represents the state of the resource cache, with hit MISS expired three states Add_header X-cache $upstream _cache_status; Location ~.
(JPG|PNG|GIF|CSS|JS) $ {proxy_pass http://127.0.0.1:81; #设置Zone Proxy_cache cache1 of resource caching;
#设置缓存的key Proxy_cache_key $host $uri$is_args$args;
#设置状态码为200和304的响应可以进行缓存, and the cache time is 10 minutes proxy_cache_valid 304 10m;
Expires 30d;
}
}
Installing the Purge module
The purge module is used to clear the cache
$ wget http://labs.frickle.com/files/ngx_cache_purge-1.2.tar.gz
$ tar-zxvf ngx_cache_purge-1.2.tar.gz
View compilation parameters
$/usr/local/nginx/sbin/nginx-v
Add--add-module=/usr/local/ngx_cache_purge-1.2 after the original compilation parameters
$./configure--user=www--group=www--prefix=/usr/local/nginx \
--with-http_stub_status_module--with-http_ssl_ Module \
--with-http_realip_module--add-module=/usr/local/ngx_cache_purge-1.2
$ make && make Install
Exit Nginx and reboot
$/usr/local/nginx/sbin/nginx-s quit
$/usr/local/nginx/sbin/nginx
Configure purge
The following is a purge configuration fragment in Nginx
Location ~/purge (/.*) {
#允许的IP
allow 127.0.0.1;
Deny all;
Proxy_cache_purge cache1 $host $1$is_args$args;
}
Clear Cache
How to use:
$ wget Http://example.com/purge/uri
Where the URI is a static resource URI, and if the URL of the cached resource is http://example.com/js/jquery.js, then accessing Http://example.com/purge/js/jquery.js clears the cache. hit rate
Save the following code as hit_rate.sh:
#!/bin/bash
# author:jeremy Wei <shuimuqingshu@gmail.com>
# Proxy_cache hit rate
if [$1x!= x] then
if [E $] then
hit= ' cat | grep HIT | wc-l '
all= ' cat $ | wc-l '
hit_rate= ' echo ' scale=2; ($HIT/$ALL) * 100 "| BC '
Echo ' Hit rate= $Hit _rate% '
else
echo ' not exsist! '
Fi
Else
echo "usage:./hit_rate.sh file_path"
fi
How to use
$./hit_rate.sh/usr/local/nginx/log/access.log
Reference:
Http://wiki.nginx.org/HttpProxyModule