当前位置: 代码网 > 服务器>服务器>缓存 > Nginx+Proxy_cache高速缓存配置

Nginx+Proxy_cache高速缓存配置

2026年01月16日 缓存 我要评论
前言nginx已经具备squid所拥有的web缓存加速功能、清除指定url缓存的功能。而在性能上,nginx对多核cpu的利用,胜过squid不少。另外,在反向代理、负载均衡、健康检查、后端服务器故障

前言

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/ 

四、如何清除缓存:

清除缓存有两种方法,第一种是直接通过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

到此这篇关于nginx+proxy_cache高速缓存配置的文章就介绍到这了,更多相关nginx proxy_cache缓存配置内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com