Use proxy cache to cache nginx static Resources

Source: Internet
Author: User
Preface

Nginx is a high-performance HTTP server that uses proxy cache to cache static resources. The principle is to store static resources on a local hard disk according to certain rules, and cache common resources in the memory to speed up the response of static resources.

Configure Proxy Cache

The following is the nginx configuration snippet:

Proxy_temp_path/usr/local/nginx/proxy_temp_dir 1 2; # keys_zone = cache1: 100 M indicates that this zone name is cache1, the allocated memory size is 100 MB #/usr/local/nginx/proxy_cache_dir/cache1 indicates the directory where files in the cache1 zone will be stored # levels = indicates the first directory of the cache directory it is 1 character, the second-level directory contains 2 characters, that is, the format of/usr/local/nginx/proxy_cache_dir/cache1/A/1B # inactive = 1D indicates that the cache files in this zone are not accessed within one day, the file will be deleted by the cache manager process # max_size = 10g indicates that the hard disk capacity of this zone is 10gbproxy_cache_path/usr/local/nginx/proxy_cache _ DIR/cache1 levels = keys_zone = cache1: 100 m inactive = 1D max_size = 10g; server {Listen 80; SERVER_NAME * .example.com; # add $ upstream_cache_status log_format format1 '$ remote_addr-$ remote_user [$ time_local] ''" $ request "$ Status $ response'' "$ http_referer" "$ http_user_agent" to the log format" $ upstream_cache_status '; access_log log/access. log fomat1; # $ upstream_cache_status indicates the resource cache status, which has three types: Hit Miss expired. State add_header X-Cache $ upstream_cache_status; location ~. (JPG | PNG | GIF | CSS | JS) $ {proxy_pass http: // 127.0.0.1: 81; # Set the zone proxy_cache cache1 of the resource cache; # Set the cached key proxy_cache_key $ host $ URI $ is_args $ ARGs; # Set the response with status codes 200 and 304 to cache, and the cache time is 10 minutes. proxy_cache_valid 200 304 10 m; expires 30d ;}}
Install 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

After the original compilation parameters, add--add-module=/usr/local/ngx_cache_purge-1.2

$ ./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 restart

$ /usr/local/nginx/sbin/nginx -s quit$ /usr/local/nginx/sbin/nginx
Configure purge

The following is the purge configuration snippet in nginx.

Location ~ /Purge (/. *) {# allowed IP addresses allow 127.0.0.1; deny all; proxy_cache_purge cache1 $ host $1 $ is_args $ ARGs ;}
Clear Cache

Usage:

$ wget http://example.com/purge/uri

WhereuriIs the URI of a static resource. If the cached resource URL ishttp://example.com/js/jquery.js, Then accesshttp://example.com/purge/js/jquery.jsThe cache is cleared.

Hit rate

Save the following code as hit_rate.sh:

#!/bin/bash# author: Jeremy Wei <[email protected]># proxy_cache hit rateif [ $1x != x ] 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!"    fielse    echo "usage: ./hit_rate.sh file_path"fi

Usage

$ ./hit_rate.sh /usr/local/nginx/log/access.log


This article is from the "Lonely (Technology Group: 1991706)" blog, please be sure to keep this source http://304076020.blog.51cto.com/7503470/1545246

Use proxy cache to cache nginx static Resources

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.