Here give an example, and detailed.
HTTP {
[...]
[...]
Proxy_cache_path/data/nginx/cache/one levels=1:2 keys_zone=one:10m max_size=10g;
Proxy_cache_key "$host $request_uri";
server {
server_name www.centos.bz centos.bz;
Root/home/www.centos.bz/web;
Index index.php index.html index.htm;
Location/{
Proxy_pass http://127.0.0.1:8080;
Proxy_set_header Host "Www.centos.bz";
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
#开启反向代理缓存 and use the cache with zone name one.
Proxy_cache one;
#设置状态码为200 302 expiration of 10 minutes
Proxy_cache_valid 302 10m;
#设置状态码404的过期时间为1分钟
Proxy_cache_valid 404 1m;
}
#清除缓存
Location ~/purge (/.*) {
#允许的IP
Allow 127.0.0.1;
Deny all;
Proxy_cache_purge one $host $1$is_args$args;
}
}
}
The caching of reverse proxies mainly involves the following commands:
Proxy_cache_path Proxy_cache_key Proxy_cache proxy_cache_valid.
Proxy_cache_path
This is the directory where the cache is set and the syntax is as follows:
Proxy_cache_path path [levels = levels] Keys_zone = name:size [inactive = time] [max_size = size] [Loader_files = NUMBER] [loader_sleep = time] [loader_threshold = time]
The context that can be placed:
http
Parameter explanation:
[levels = levels]:
Set the number of cache directory tiers, such as Levels=1:2, to create a two-tier directory cache that creates up to three layers. The first-level directory name takes the last character of the Proxy_cache_key MD5, and the second-level directory name takes the penultimate 2-3 characters, such as:
Proxy_cache_key MD5 as b7f54b2df7773722d382f4809d65029c, then:
Levels=1:2 for/data/nginx/cache/c/29/b7f54b2df7773722d382f4809d65029c
Levels=1:2:3 for/data/nginx/cache/c/29/650/b7f54b2df7773722d382f4809d65029c
Keys_zone = name:size:
Defines the cache region name and size, where the cache name is used for the proxy_cache instruction settings where the cache is placed, such as Proxy_cache one, where the cache is placed in a buffer zone named one, that is, the specific location specified by Proxy_cache_path. The
Proxy_cache_key
directive is to set what parameters md5 the cached file name, the default is $scheme$proxy_host$request_uri, that is, the protocol, host name, request URI (including parameters) MD5 the cached file name. The
Proxy_cache_key has a lot to do with the following clear caching functionality (purge cache).
can be placed in context, HTTP server location
Proxy_cache
Reverse proxy cache settings directive, syntax Proxy_cache Zone | off, default to OFF. Context: HTTP server location. The
can be placed in the specified location so that the URL that matches this location is cached. The
Proxy_cache_valid
setting specifies the cache time for the status code, and the syntax Proxy_cache_valid [code ...] times.
In addition, the clear cache requires the installation of plug-in Ngx_cache_purge, the installation method is as follows:
Cd/tmp
wget http://labs.frickle.com/files/ngx_cache_ purge-2.1.tar.gz
Tar xzf ngx_cache_purge-2.1.tar.gz
cd/tmp
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 - -add-module=/tmp/ngx_cache_purge-2.1
Make && make install