阿里云-云小站(无限量代金券发放中)
【腾讯云】云服务器、云数据库、COS、CDN、短信等热卖云产品特惠抢购

构建Nginx Cache高性能缓存系统

144次阅读
没有评论

共计 4231 个字符,预计需要花费 11 分钟才能阅读完成。

随着 Nginx web 服务器得到越来越多的 SA 的青睐,Nginx 的 cache 功能已经具备 Squid 所拥有的 Web 缓存加速功能、清除指定 URL 缓存的功能。

而在性能上,Nginx 对多核 CPU 的利用,胜过 Squid 不少。另外,在反向代理、负载均衡、健康检查、后端服务器故障转移、Rewrite 重写、易用性上,Nginx 也比 Squid 强大得多。

这使得一台 Nginx 可以同时作为负载均衡服务器与 Web 缓存服务器来使用。

一、Nginx(Ngx_cache)安装:

首先下载 Nginx 缓存模块,ngx_cache_purge 相应版本,这里下载 nginx-1.4 版本,不同版本对应不同的 Nginx 版本,安装的时候要留心。

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

 

二、Nginx Cache 配置:

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 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;

      }

  }

}

 

三、Nginx Cache 测试:

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

# 访问我们的 WEB 站点,然后在 /data/proxy_cache_dir 目录会看到缓存的子目录(以数字、字母组成)

如下图:

构建 Nginx Cache 高性能缓存系统

四、如何清除缓存:

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

————————————– 分割线 ————————————–

CentOS 6.2 实战部署 Nginx+MySQL+PHP http://www.linuxidc.com/Linux/2013-09/90020.htm

使用 Nginx 搭建 WEB 服务器 http://www.linuxidc.com/Linux/2013-09/89768.htm

搭建基于 Linux6.3+Nginx1.2+PHP5+MySQL5.5 的 Web 服务器全过程 http://www.linuxidc.com/Linux/2013-09/89692.htm

CentOS 6.3 下 Nginx 性能调优 http://www.linuxidc.com/Linux/2013-09/89656.htm

CentOS 6.3 下配置 Nginx 加载 ngx_pagespeed 模块 http://www.linuxidc.com/Linux/2013-09/89657.htm

CentOS 6.4 安装配置 Nginx+Pcre+php-fpm http://www.linuxidc.com/Linux/2013-08/88984.htm

Nginx 安装配置使用详细笔记 http://www.linuxidc.com/Linux/2014-07/104499.htm

Nginx 日志过滤 使用 ngx_log_if 不记录特定日志 http://www.linuxidc.com/Linux/2014-07/104686.htm

————————————– 分割线 ————————————–

Nginx 的详细介绍 :请点这里
Nginx 的下载地址 :请点这里

正文完
星哥说事-微信公众号
post-qrcode
 0
星锅
版权声明:本站原创文章,由 星锅 于2022-01-20发表,共计4231字。
转载说明:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
【腾讯云】推广者专属福利,新客户无门槛领取总价值高达2860元代金券,每种代金券限量500张,先到先得。
阿里云-最新活动爆款每日限量供应
评论(没有评论)
验证码
【腾讯云】云服务器、云数据库、COS、CDN、短信等云产品特惠热卖中