Nginx reverse proxy cache Configuration

Source: Internet
Author: User

Here is an example and detailed description.

  1. Http {
  2. [...]
  3. [...]
  4. Proxy_cache_path/data/nginx/cache/one levels = keys_zone = one: 10 m max_size = 10g;
  5. Proxy_cache_key "$ host $ request_uri ";
  6. Server {
  7. Server_name www. centos. bz centos. bz;
  8. Root/home/www. centos. bz/web;
  9. Index. php index.html index.htm;
  10. Location /{
  11. Proxy_pass http: // 127.0.0.1: 8080;
  12. Proxy_set_header Host "www. centos. bz ";
  13. Proxy_set_header X-Forwarded-For $ proxy_add_x_forwarded_for;
  14. # Enable reverse proxy cache and use the cache with zone name one.
  15. Proxy_cache one;
  16. # Set the status code to 200 302 expiration time to 10 minutes
  17. Proxy_cache_valid 200 302 10 m;
  18. # Set the expiration time of Status Code 404 to 1 minute
  19. Proxy_cache_valid 404 1 MB;
  20. }
  21. # Clear Cache
  22. Location ~ /Purge (/.*){
  23. # Allowed IP addresses
  24. Allow 127.0.0.1;
  25. Deny all;
  26. Proxy_cache_purge one $ host $1 $ is_args $ args;
  27. }
  28. }
  29. }

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:

  1. Cd/tmp
  2. Wget http://labs.frickle.com/files/ngx_cache_purge-2.1.tar.gz
  3. Tar xzf ngx_cache_purge-2.1.tar.gz
  4. Cd/tmp
  5. Wget http://nginx.org/download/nginx-1.4.2.tar.gz
  6. Tar xzf nginx-1.4.2.tar.gz
  7. Cd nginx-1.4.2
  8. ../Configure -- prefix =/usr/local/nginx -- add-module =/tmp/ngx_cache_purge-2.1
  9. 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/.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.