---
title: "使用Nginx的proxy_cache缓存功能取代Squid"
author: "cnsong"
date: "2017-03-13T18:13:58Z"
tags: []
categories: ["操作系统"]
slug: "nginx-proxy-cache-squid"
draft: false
lastmod: "2017-03-13T18:13:58Z"
id: 2362014
url: "https://cnsong.iteye.com/blog/2362014"
---

Nginx从0.7.48版本开始，支持了类似Squid的缓存功能。这个缓存是把URL及相关组合当作Key，用md5编码哈希后保存在硬盘上，所以它可以支持任意URL链接，同时也支持404/301/302这样的非200状态码。虽然目前官方的Nginx Web缓存服务只能为指定URL或状态码设置过期时间，不支持类似Squid的PURGE指令，手动清除指定缓存页面，但是，通过一个第三方的Nginx模块，可以清除指定URL的缓存。

 

　　Nginx的Web缓存服务主要由proxy\_cache相关指令集和fastcgi\_cache相关指令集构成，前者用于反向代理时，对后端内容源服务器进行缓存，后者主要用于对FastCGI的动态程序进行缓存。两者的功能基本上一样。

 

　　Nginx的proxy\_cache和fastcgi\_cache已经比较完善，加上第三方的ngx\_cache\_purge模块（用于清除指定URL的缓存），已经可以完全取代Squid。而且十分稳定，速度不逊于 Squid。

 

 

一、所需软件

yum -y install gcc gcc-c++ autoconf  openssl openssl-devel psmisc zip

pcre-8.40.tar.gz ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/

ngx\_cache\_purge-1.6.tar.gz http://labs.frickle.com/files/

Tengine-2.0.0.tar.gz http://tengine.taobao.org/download.html

 

 

二、编译安装

/usr/sbin/groupadd www

/usr/sbin/useradd -g www -s /sbin/nologin www

mkdir /data/proxy\_temp\_dir

mkdir /data/proxy\_cache\_dir

tar zxvf pcre-8.40.tar.gz

tar zxvf ngx\_cache\_purge-1.6.tar.gz

tar zxvf tengine-2.0.0.tar.gz

cd tengine-2.0.0/

./configure --user=www --group=www --add-module=../ngx\_cache\_purge-1.6 --prefix=/usr/local/nginx --with-http\_stub\_status\_module --with-http\_ssl\_module  --with-pcre=../pcre-8.40 

#注意：--with-pcre=../pcre-8.37指向的是源码包解压的路径，而不是安装的路径，否则会报错,

make && make install

cd ../

 

三、nginx配置文件

##################################################################

user  www www;

 

worker\_processes 8;

 

error\_log  /usr/local/nginx/logs/nginx\_error.log  crit;

 

pid        /usr/local/nginx/nginx.pid;

 

#Specifies the value for maximum file descriptors that can be opened by this process. 

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/plain application/x-javascript text/css application/xml;

  gzip\_vary on;

 

  #注：proxy\_temp\_path和proxy\_cache\_path指定的路径必须在同一分区

  proxy\_temp\_path   /data/proxy\_temp\_dir;

  #设置Web缓存区名称为cache\_one，内存缓存空间大小为200MB，1天没有被访问的内容自动清除，硬盘缓存空间大小为30GB。

  proxy\_cache\_path  /data/proxy\_cache\_dir  levels=1:2   keys\_zone=cache\_one:200m inactive=1d max\_size=30g;

  

  upstream backend\_server {

    server   192.168.8.43:80 weight=1 max\_fails=2 fail\_timeout=30s;

    server   192.168.8.44:80 weight=1 max\_fails=2 fail\_timeout=30s;

    server   192.168.8.45:80 weight=1 max\_fails=2 fail\_timeout=30s;

  }

 

  server

  {

    listen       80;

    server\_name  www.yourdomain.com;

    index index.html index.htm;

    root  /data0/htdocs/www;  

 

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

         #以域名、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://backend\_server;

         expires      1d;

    }

    

    #用于清除缓存，假设一个URL为http://192.168.8.42/test.txt，通过访问http://192.168.8.42/purge/test.txt就可以清除该URL的缓存。

    location ~ /purge(/.\*)

    {

     #设置只允许指定的IP或IP段才可以清除URL缓存。

     allow            127.0.0.1;

     allow            192.168.0.0/16;

     deny            all;

     proxy\_cache\_purge    cache\_one   $host$1$is\_args$args;

    }    

 

    #扩展名以.php、.jsp、.cgi结尾的动态应用程序不缓存。

    location ~ .\*\\.(php|jsp|cgi)?$

    {

         proxy\_set\_header Host  $host;

         proxy\_set\_header X-Forwarded-For  $remote\_addr;

         proxy\_pass http://backend\_server;

    }

 

    access\_log  off;

  }

}

##################################################################

启用Nginx

/usr/local/nginx/sbin/nginx