Nginx+Proxy_cache高速缓存配置

前言* Nginx已经具备Squid所拥有的Web缓存加速功能、清除指定URL缓存的功能。而在性能上,Nginx对多核CPU的利用,胜过Squid不少。另外,在反向代理、负载均衡、健康检查、后端服务器故障转移、Rewrite重写、易用性上,Nginx也比Squid强大得多。这使得一台Nginx可以同时作为“负载均衡服务器”与“Web缓存服务器”来使用。

一、 安装nginx和ngx-purge:

ulimit -SHn 65535

cd /usr/local/nginx

tar zxvf ngx_cache_purge-1.4.tar.gz

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

make && make install
cd ../

二、 Nginx Cache配置:

http  {  \\添加以下内容 ,不能定义在server{}上下文中 }

.......

#定义从后端服务器接收的临时文件的存放路径

proxy_temp_path /data/proxy_temp_dir;

#设置Web缓存区名称cache_one,内存缓存空间100MB,1天没有被访问的内容自动清除,硬盘缓存空间10GB。

proxy_cache_path /nginx/cache/first levels=1:2:1 keys_zone=cache_one:100m inactive=1d max_size=10g;   


upstream backend_server {
server 10.1.1.1:8080 weight=1 max_fails=2 fail_timeout=30s;
server 10.1.1.2:8080 weight=1 max_fails=2 fail_timeout=30s;

server
{
listen 80;
server_name localhost;
index index.html index.htm;
root  html;


location /
{

#如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
proxy_next_upstream http_502 http_504 error timeout invalid_header;
proxy_cache cache_one;

#对不同的HTTP状态码设置不同的缓存时间
proxy_cache_valid 200 304  2h;
#以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
proxy_cache_key $host$uri$is_args$args;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_pass http://127.0.0.1;
expires 1d;
}

location ~ /purge(/.*)
{

#设置只允许指定的IP或IP段输入正确的密码才可以清除URL缓存。
auth_basic “Please Insert User And Password”;
auth_basic_user_file /tmp/htpasswd;
allow 127.0.0.1;
allow 10.1.1.0/24;
deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
}

........


三、ginx Cache测试:

#启动Nginx服务,/usr/local/nginx/sbin/nginx 

#然后配置好resin端口设置为8080  

#如果需要刷新缓存的url地址为:  http://10.1.1.10/purge/ 



如下图:

http://img1.51cto.com/group_attachment/201112131323751485261.png

四、如何清除缓存:

    清除缓存有两种方法,第一种是直接通过nginx.conf配置文件定义的/purge虚拟目录去清除,第二种方法可以通过shell脚本去批量清除:

    附上Shell脚本清空缓存的内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#! /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  in  ` echo  $FILE | sed  's//\n/g' `
do
    grep  -ra  $i  ${CACHE_DIR}|  awk  -F ':'  '{print $1}'   /tmp/cache_list .txt
     for  in  ` cat /tmp/cache_list .txt`
   do
     rm   -rf  $j
     echo  "$i  $j  is  Deleted Success !"
   done
done

本文转自奔跑在路上博客51CTO博客,原文链接http://blog.51cto.com/qiangsh/1557047如需转载请自行联系原作者


qianghong000

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值