---
title: "代理加速服务平台"
author: "neatchenheng"
date: "2010-03-19T13:42:43Z"
tags: ["Access", "Apache", "CSS", "Cache", "应用服务器"]
categories: ["编程语言"]
slug: "untitled"
draft: false
lastmod: "2010-03-19T13:42:43Z"
id: 619763
url: "https://itshenry.iteye.com/blog/619763"
---

Apache是高效的WEB服务器，对于处理大量静态请求的应用环境来说，如果引入Squid做为代理加速缓存，可以更好的提高服务器的性能。下面提供一套代理加速整合草案，并做为[Hero Game服务平台](http://blog.inseekar.cn/?p=30)的一部分。

Squid和Apache运行在[larks](http://blog.inseekar.cn/?p=3)(192.168.0.108)上。下面将分别介绍Squid和Apache的安装和配制。

    （一）Squid的安装和配制：

1、下载：squid-3.0.STABLE16-1.el5.x86\_64.rpm

2、安装：rpm -ivh squid-3.0.STABLE16-1.el5.x86\_64.rpm （注：如果提示有冲突，请删掉旧版本. rpm -q squid ， 查询系统已有的软件包，然后 rpm -e 删掉冲突软件包）

3、配制：编辑squid.conf文件(vim /etc/squid/squid.conf)

#定位到TAG : acl 定义规则

acl localnet src 192.168.0.0/16

acl mysites dstdomain .inseekar.cn

acl nullref referer\_regex .

acl myreferer referer\_regex -i ^http://.\*\\.inseekar\\.cn

acl nolog urlpath\_regex -i \\.gif$ \\.jpg$ \\.jpeg$ \\.png$ \\.swf$ \\.css$ \\.js$

acl nullref referer\_regex .

acl myreferer referer\_regex -i ^http://.\*\\.inseekar\\.cn

acl nolog urlpath\_regex -i \\.gif$ \\.jpg$ \\.jpeg$ \\.png$ \\.swf$ \\.css$ \\.js$

#定位到TAG: http\_access定义访问规则

http\_access allow manager localnet

http\_access allow !nullref

http\_access deny !myreferer nolog

http\_access allow mysites

    #定位到TAG: http\_port定义代理端口

    http\_port 80 accel defaultsite=hero.inseekar.cn vhost vport transparent

    #定位到 TAG: cache\_peer定义代理缓存单元

    cache\_peer 192.168.0.108 parent 81 0 no-query originserver no-digest default          name=hero

    #定位到 TAG: cache\_peer\_domain定义代理缓存域

    cache\_peer\_domain hero hero.inseekar.cn

   #定位到TAG: cache\_peer\_access定义代理缓存访问规则

   cache\_peer\_access hero deny !mysites

   #定位到TAG: cache\_mem       (bytes) 定义缓存可使用的最大内存

   cache\_mem 256 MB

   #定位到 TAG: maximum\_object\_size\_in\_memory   (bytes)定义最大缓存对象

   maximum\_object\_size\_in\_memory 1024 KB

  #定位到TAG: cache\_dir定义缓存目录

  cache\_dir ufs /var/spool/squid 1024 16 256

 #定位到TAG: logformat定义日志格式（这里使用了awstat的格式，以便分析访问数据）

  logformat combined %&gt;a %ui %un \[%tl] “%rm %ru   HTTP/%rv” %Hs %&lt;st   “%{Referer}&gt;h” “%{User-Agent}&gt;h” %Ss:%Sh %{host}&gt;h

  #定位到 TAG: access\_log 定义日志路径和格式

  access\_log /var/log/squid/access.log combined

  #定位到 TAG: refresh\_pattern定义相应文件的缓存策略

refresh\_pattern \\.gif$ 10080 80% 20160

refresh\_pattern \\.GIF$ 10080 80% 20160

refresh\_pattern \\.jpg$ 10080 80% 20160

refresh\_pattern \\.JPG$ 10080 80% 20160

refresh\_pattern \\.jpeg$ 10080 80% 20160

refresh\_pattern \\.JPEG$ 10080 80% 20160

refresh\_pattern \\.png$ 10080 80% 20160

refresh\_pattern \\.swf$ 10080 80% 20160

refresh\_pattern \\.css$ 10080 80% 20160

refresh\_pattern \\.js$ 10080 80% 20160

refresh\_pattern \\.jsp$ 0 0% 0

refresh\_pattern \\.sk$ 0 0% 0

refresh\_pattern \\.s$ 0 0% 0

refresh\_pattern \\.php$ 0 0% 0

refresh\_pattern \\.cgi$ 0 0% 0

#定位到TAG: cachemgr\_passwd定义cache管理器的使用范畴

cachemgr\_passwd disable shutdown reconfigure offline\_toggle

cachemgr\_passwd larks all

（上面是squid做为代理加速的基本配制，如欲详细了解，请阅读配制文件本身自带的说明文档）

4、初始化Squid

bash&gt; /usr/sbin/squid -z

5、启动Squid

bash&gt; /usr/sbin/squid

6、关闭Squid

bash&gt; /usr/sbin/squid -k shutdown

7、 查看Squid运行状态

bash&gt; /usr/sbin/squidclient -h larks -p 80 mgr:info@larks

8、如果通过apache来访问squid的运行情况，则还要做下面的设置：

a. 在squid.conf文件里将本地网络加入到localnet中，并允许访问cachemgr,如：

acl localnet src 192.168.0.0/16

http\_access allow manager localnet

b.在/etc/squid/cachemgr.conf编辑：

192.168.0.108:80

c、登陆http://blog.inseekar.cn/Squid/cgi-bin/cachemgr.cgi，选择Cache Server : 192.168.0.80:80 ；Manager Name : 任意（但不为空）；Password : larks (在 squid.conf 中配制， 如：cachemgr\_passwd larks all)

(二) Apache的安装和配制

1、apache是系统安装时默认安装的。对于目前的应用基本都能满足。

2、因为squid 和 apache 运行在同一台机器上，所以这里要把apache 的运行端口改成：

Listen 81 (编辑/etc/httpd/conf/httpd.conf)

3、加入虚拟主要住信息:

&lt;VirtualHost \*:81&gt;

ServerAdmin admin@inseekar.cn

DocumentRoot /home/inseekarPhp/hero

ServerName hero.inseekar.cn

ProxyPass /js/ !

ProxyPass /css/ !

ProxyPass /images/ !

ProxyPass /flash/ !

ProxyPass / ajp://192.168.0.114:8009/

ErrorLog logs/hero.inseekar.cn-error\_log

CustomLog logs/hero.inseekar.cn-access\_log common

&lt;/VirtualHost&gt;

下面附两份配制文件样例：

squid.conf

httpd.conf