Build an Nginx Cache high-performance Cache system

Source: Internet
Author: User

Build an Nginx Cache high-performance Cache system

As Nginx web servers become more and more popular with SA, Nginx's cache function already has the Web cache acceleration function of Squid and the function of clearing the specified URL cache.

In terms of performance, Nginx's use of multi-core CPU is much better than Squid. In addition, Nginx is much more powerful than Squid in reverse proxy, Server Load balancer, health check, backend server failover, Rewrite, and ease of use.

This allows an Nginx instance to be used as both the Server Load balancer server and the Web cache server.

I. Nginx (Ngx_cache) installation:

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

Ulimit-SHn 65535

Yum install 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

 

Ii. Nginx Cache Configuration:

User www;

Worker_processes 8;

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

Pid/usr/local/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 32 k;

Large_client_header_buffers 4 32 k;

Client_max_body_size 300 m;

 

Sendfile on;

Tcp_nopush on;

Keepalive_timeout 60;

Tcp_nodelay on;

Client_body_buffer_size 512 k;

 

Proxy_connect_timeout 5;

Proxy_read_timeout 60;

Proxy_send_timeout 5;

Proxy_buffer_size 16 k;

Proxy_buffers 4 64 k;

Proxy_busy_buffers_size 128 k;

Proxy_temp_file_write_size 128 k;

 

Gzip on;

Gzip_min_length 1 k;

Gzip_buffers 4 16 k;

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 = keys_zone = cache_one: 200 m inactive = 1d max_size = 30g;

 

Upstreambackend_server {

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

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

}

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 200 304 12 h;

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;

}

}

}

 


Iii. Nginx Cache test:

# Start the Nginx service,/usr/local/nginx/sbin/nginx

# Visit our WEB site, and then you will see the cached subdirectory in the/data/proxy_cache_dir directory (consisting of digits and letters)

For example:

4. How to clear the cache:

There are two methods to clear the cache. The first method is to clear the/purge virtual directory defined in the nginx. conf configuration file directly, and the second method is to clear the cache in batches using shell scripts:

Attach the Shell script to clear the cache content:

#! /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 To determine whether the script has input, no input, and then exit

If

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

Echo "Please Insert clean Nginx cache File, Example: $0 index.html index. js"

Sleep 2 & exit

Fi

Echo "The file: $ FILE to be clean nginx Cache, please waiting ....."

 

# Wrap processing for the input file, for grep lookup, to Wrap the input file, facilitating grep searching for Matching content

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

Do

Grep-ra $ I $ {CACHE_DIR} | awk-F ': ''{print $1}'>/tmp/cache_list.txt

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

Do

Rm-rf $ j

Echo "$ I $ j is Deleted Success! "

Done

Done

-------------------------------------- Split line --------------------------------------

Deployment of Nginx + MySQL + PHP in CentOS 6.2

Build a WEB server using Nginx

Build a Web server based on Linux6.3 + Nginx1.2 + PHP5 + MySQL5.5

Performance Tuning for Nginx in CentOS 6.3

Configure Nginx to load the ngx_pagespeed module in CentOS 6.3

Install and configure Nginx + Pcre + php-fpm in CentOS 6.4

Nginx installation and configuration instructions

Nginx log filtering using ngx_log_if does not record specific logs

-------------------------------------- Split line --------------------------------------

Nginx details: click here
Nginx: click here

This article permanently updates the link address:

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.