Constructing Nginx cache high Performance caching system

Source: Internet
Author: User

With the Nginx Web server getting more and more favor of SA, Nginx cache function already has the Web caching acceleration function that squid owns, and clears the function of specifying URL cache.

In the performance, Nginx on the use of multi-core CPU, more than a lot of squid. In addition, Nginx is much more powerful than squid in reverse proxies, load balancing, health checks, backend server failover, rewrite rewriting, and ease of use.

This allows an nginx to be used both as a Load Balancer server and as a Web cache server.

First, Nginx (ngx_cache) Installation:

First download the Nginx cache module, ngx_cache_purge the corresponding version, here to download the nginx-1.4 version, different versions corresponding to different Nginx version, installation should pay attention to.

Ulimit-shn 65535

Yum Install Pcre pcre-devel-y

wget http://nginx.org/download/nginx-1.0.11.tar.gz

Http://labs.frickle.com/files/ngx_cache_purge-1.4.tar.gz

Tarz XVF ngx_cache_purge-1.4.tar.gz

Tarz XVF nginx-1.0.11.tar.gz

Useradd www

CD nginx-1.0.11/

./configure--user=www--group=www--add-module=. /ngx_cache_purge-1.4--prefix=/usr/local/nginx--with-http_stub_status_module--with-http_ssl_module

Make && make install

Second, Nginx cache configuration:

User www www;

Worker_processes 8;

Error_log/data/logs/nginx/error.log Crit;

Pid/usr/local/nginx/nginx.pid;

Worker_rlimit_nofile 65535;

Events

{

Use Epoll;

Worker_connections 65535;

}

http

{

Include Mime.types;

Default_type Application/octet-stream;

CharSet Utf-8;

Server_names_hash_bucket_size 128;

Client_header_buffer_size 32k;

Large_client_header_buffers 4 32k;

Client_max_body_size 300m;

Sendfile on;

Tcp_nopush on;

Keepalive_timeout 60;

Tcp_nodelay on;

Client_body_buffer_size 512k;

Proxy_connect_timeout 5;

Proxy_read_timeout 60;

Proxy_send_timeout 5;

Proxy_buffer_size 16k;

Proxy_buffers 4 64k;

Proxy_busy_buffers_size 128k;

Proxy_temp_file_write_size 128k;

gzip on;

Gzip_min_length 1k;

Gzip_buffers 4 16k;

Gzip_http_version 1.1;

Gzip_comp_level 2;

Gzip_types text/plainapplication/x-javascript text/css Application/xml;

Gzip_vary on;

Proxy_temp_path/data/proxy_temp_dir;

Proxy_cache_path/data/proxy_cache_dirlevels=1:2 keys_zone=cache_one:200m inactive=1d max_size=30g;

Upstreambackend_server {

Server 127.0.0.1:8800 weight=1 max_fails=2 fail_timeout=30s;

Server 127.0.0.1:8801 weight=1 max_fails=2 fail_timeout=30s;

}

Server

{

Listen 80;

server_name localhost;

Index index.html index.htm;

root/data/webapps/www;

Location/

{

Proxy_next_upstream http_502 http_504 error timeout invalid_header;

Proxy_cache Cache_one;

Proxy_cache_valid 304 12h;

Proxy_cache_key $host $uri$is_args$args;

Proxy_set_header Host $host;

Proxy_set_header x-forwarded-for $remote _addr;

Proxy_pass Http://backend_server;

Expires 1d;

}

Location ~/purge (/.*)

{

Auth_basic "TDT Center CACHE Center";

AUTH_BASIC_USER_FILE/TMP/HTPASSWD;

Allow 127.0.0.1;

Allow 192.168.1.0/24;

Deny all;

Proxy_cache_purge Cache_one $host $1$is_args$args;

}

Location ~. *\. (php|jsp|cgi)? $

{

Proxy_set_header Host $host;

Proxy_set_header x-forwarded-for $remote _addr;

Proxy_pass Http://backend_server;

}

}

}


Third, Nginx cache test:

# start Nginx service,/usr/local/nginx/sbin/nginx

# Visit our web site and then see the cached subdirectories (in numbers, letters) in the/data/proxy_cache_dir directory

Such as:

650) this.width=650; "src=" http://chinaapp-wordpress.stor.sinaapp.com/uploads/2014/07/111.jpg "width=" 726 "height= "184" alt= "111.jpg"/>

Iv. How to clear the cache:

There are two ways to clear the cache, the first is to clear through the/purge virtual directory defined by the nginx.conf configuration file, and the second method can be bulk purged through the shell script:

Enclose the shell script to empty the contents of the cache:

#! /bin/sh

#Auto clean Nginx Cache Shell Scripts

#2013 -06-12 WUGK

#Define Path

cache_dir=/data/www/proxy_cache_dir/

File= "$*"

#To Determine whether the input script , if Not,then exit determines if the script has input, does not enter and then exits

If

["$#"-eq "0"];then

echo "Please Insert the clean Nginx cache File, Example: $ index.html index.js"

Sleep 2 && exit

Fi

echo "The file: $FILE to being clean nginx Cache, please waiting ..."

#Wrap processing for the input file, for grep lookup , line-wrapping of input files facilitates grep to find matching related content

For i in ' echo $FILE |sed ' s//\n/g '

Do

Grep-ra $i ${cache_dir}| Awk-f ': ' {print $ ' >/tmp/cache_list.txt

For j in ' Cat/tmp/cache_list.txt '

Do

RM-RF $j

echo "$i $j is Deleted Success!"

Done

Done


This article is from the "Wu Guangko-focus on automated Operations" blog, please be sure to keep this source http://wgkgood.blog.51cto.com/1192594/1593564

Constructing Nginx cache high Performance caching system

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.