Here is an example and detailed description.
- Http {
- [...]
- [...]
- Proxy_cache_path/data/nginx/cache/one levels = keys_zone = one: 10 m max_size = 10g;
- Proxy_cache_key "$ host $ request_uri ";
- Server {
- Server_name www. centos. bz centos. bz;
- Root/home/www. centos. bz/web;
- 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;
- # Enable reverse proxy cache and use the cache with zone name one.
- Proxy_cache one;
- # Set the status code to 200 302 expiration time to 10 minutes
- Proxy_cache_valid 200 302 10 m;
- # Set the expiration time of Status Code 404 to 1 minute
- Proxy_cache_valid 404 1 MB;
- }
- # Clear Cache
- Location ~ /Purge (/.*){
- # Allowed IP addresses
- Allow 127.0.0.1;
- Deny all;
- Proxy_cache_purge one $ host $1 $ is_args $ args;
- }
- }
- }
The cache of reverse proxy mainly involves the following commands:
Proxy_cache_path proxy_cache_key proxy_cache proxy_cache_valid.
Proxy_cache_path
This is the directory for caching. 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]
Extensible context:
Http
Parameter description:
[Levels = levels]:
Set the number of cache directory layers, such as levels =, to create a two-tier directory cache. A maximum of three layers can be created. The first-level directory name is the last character of proxy_cache_key md5, and the second-level directory name is the penultimate 2-3 character, for example:
Proxy_cache_key md5 is b7f54b2df7773722d382f4809d42429c, then:
Levels =/data/nginx/cache/c/29/b7f54b2df7773722d382f4809dda-29c
Levels =/data/nginx/cache/c/29/650/b7f54b2df7773722d382f4809dda-29c
Keys_zone = name: size:
Defines the cache region name and size. The cache name is used for the proxy_cache command to set where the cache is placed, for example, proxy_cache one. The cache is placed in the cache zone named one, that is, the specific location specified by proxy_cache_path.
Proxy_cache_key
This command sets the parameter md5 to get the cached file name. The default value is $ scheme $ proxy_host $ request_uri, which is the protocol, host name, and request uri (including parameters) obtain the cached file name by using md5.
Proxy_cache_key is closely related to the following purge cache function.
Can be placed in the context, http server location
Proxy_cache
The reverse proxy cache setting command. Syntax: proxy_cache zone | off. The default value is off. Context: http server location.
You can put it in the specified location to cache the url that matches the location.
Proxy_cache_valid
Set the cache time of the specified status code. Syntax: proxy_cache_valid [code...] time.
In addition, the ngx_cache_purge plug-in must be installed for cache clearing. 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
Refer:
Http://wiki.nginx.org/HttpProxyModule
Http://labs.frickle.com/nginx_ngx_cache_purge/
What problems cannot be solved? Click here for support
For more information, see http://www.centos.bz/2013/08/nginx-proxy-pass-cache-config/.