Nginx cache Function

Source: Internet
Author: User

 

Label: Hit

Purge
Proxy_cache
Nginx reprinted on http://linuxgp.blog.51cto.com/1708668/589824

I. nginx's proxy_cache module is said to be much better than squid for multi-core support.
Required software:
Wget http://labs.frickle.com/files/ngx_cache_purge-1.3.tar.gz
Pcre,nginx-0.8.29.tar.gz
# Purge Module
Http://labs.frickle.com/nginx_ngx_cache_purge/

Install
Tar-zxvf ngx_cache_purge-1.0.tar.gz
Tar-zxvf nginx-0.8.29.tar.gz
CD nginx-0.8.29
./Configure -- user = WWW -- group = WWW -- add-module = ../ngx_cache_purge-1.0
-- Prefix =/usr/local/GP/nginx -- with-http_stub_status_module
With-http_ssl_module
Make & make install

Ii. Configuration
1. Create a cache directory under the same partition path
Mkdir-P/data0/proxy_temp_path
Mkdir-P/data0/proxy_cache_path

2. The directory proxy_temp is used to store temporary files. It is under nginx by default. Check whether WWW has the permission to write data,
If the file cannot be generated in this directory, the reverse proxy will fail. You can also
In nginx configuration, set proxy_temp_path to specify the directory for storing temporary files.
3. proxy_cache_path
/Data0/proxy_cache_path levels = keys_zone = cache_one: 200 m
Inactive = 1D max_size = 500 m;
Levels sets the directory hierarchy
Keys_zone: Set the cache space name and shared memory size (Hotspot content is stored in memory)
If inactive is not accessed within the specified time period, it will be deleted for one day.
Max_size Maximum Cache Space

All active keys and metadata are stored in the shared memory-Cache zone ),
Define the name and size of the cache region in keys_zone.

Iii. nginx. conf write only key steps

# Hit and expired hit states in log format settings
$ Upstream_cache_status
# It is said that the file is written to temp and then moved to the cache.
Proxy_temp_path/data0/proxy_temp_path;
Proxy_cache_path/data0/proxy_cache_path levels = keys_zone = cache_one: 200 m
Inactive = 10 m max_size = 500 m;

Upstream my_server_pool {
Server 115.79.90.78: 80;
}

Server
{
Listen 80;
SERVER_NAME file.51yuncai.com;
Location/
{
Proxy_set_header host $ host;
Proxy_set_header X-forwarded-for $ remote_addr;
Proxy_pass http: // my_server_pool;
}

# Set the cached file type. We recommend that you set the cache time on the backend through expires cache-control: Max-age = [ses]

Location ~ . * \. (GIF | JPG | PNG | BMP | SWF | JS | CSS) $
{
Proxy_cache cache_one;

# Forcibly set the cache time on the cache end
Proxy_cache_valid 200 304 12 h;
Proxy_cache_valid 301 302 1 m;
Proxy_cache_valid any 1 m;

Proxy_cache_key $ host $ URI $ is_args $ ARGs;

#####
Add_header X-Cache $ upstream_cache_status;
Proxy_set_header X-forwarded-for $ remote_addr;
Proxy_pass http: // my_server_pool;
}

Location ~ /Purge (/.*)
{
Allow 127.0.0.1;
Allow 192.168.1.0/24;
Deny all;
Proxy_cache_purge cache_one $ host $1 $ is_args $ ARGs;
}
}

Iv. Test
Access http: // xxxx/logo.jpg

Hit rate:
#! /Bin/bash
# Author: gupeng
# Proxy_cache hit rate
If ["$1 "! = '']; Then
If [-e "$1"]; then
Hit = 'cat $1 | grep hit | WC-l'
All = 'cat $1 | WC-l'
Hit_rate = 'echo "scale = 2; ($ hit/$ all) * 100" | BC'
Echo "hit rate = $ hit_rate %"
Else
Echo "$1 not exsist! "
Fi
Else
Echo "Usage:./hit_rate.sh file_path"
Fi

V. Description of hit status

$ Upstream_cache_status
Appeared in 0.8.3. Possible values:

· Miss
· Expired-expired, request was passed to backend request is sent to the backend
· Updating-expired, stale response was used due to proxy/fastcgi_cache_use_stale

Updating is updating. The old response will be used.
· Stale-expired, stale response was used due to proxy/fastcgi_cache_use_stale

Get an expired response
· Hit

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.