構建Nginx Cache高性能緩存系統

隨着Nginx web服務器獲得愈來愈多的SA的青睞,Nginx的cache功能已經具有Squid所擁有的Web緩存加速功能、清除指定URL緩存的功能。javascript

而在性能上,Nginx對多核CPU的利用,賽過Squid很多。另外,在反向代理、負載均衡、健康檢查、後端服務器故障轉移、Rewrite重寫、易用性上,Nginx也比Squid強大得多。php

這使得一臺Nginx能夠同時做爲負載均衡服務器與Web緩存服務器來使用。css


1、 Nginx(Ngx_cache)安裝:html

ulimit -SHn 65535
安裝pcre
yum install pcre pcre-devel -y
#下載並安裝ngnix以及緩存清除模塊
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

配置ngnix.conf
java

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_dir levels=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 200 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;
      }
  }
}

3、Nginx Cache測試:node

#啓動Nginx服務,/usr/local/nginx/sbin/nginxnginx

#訪問咱們的WEB站點,而後在/data/proxy_cache_dir目錄會看到緩存的子目錄(以數字、字母組成)web

4、如何清除緩存:shell

清除緩存有兩種方法,第一種是直接經過nginx.conf配置文件定義的/purge虛擬目錄去清除,第二種方法能夠經過shell腳本去批量清除: 附上Shell腳本清空緩存的內容:後端

#! /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 判斷腳本是否有輸入,沒有輸入而後退出
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,對輸入的文件進行換行處理,利於grep查找匹配相關內容
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
相關文章
相關標籤/搜索