`
wataxi
  • 浏览: 201665 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

squid配置

阅读更多

一、概述
使用Apache2 做为源站,提供所有html、css、js、images文件的源。
前端使用Squid3.0做为反 向代理,提供文件内容的缓存。
在Apache2里,设置文件的过期信息(Expires头,Max-age头).如果这两个头都没有,则 SQUID认为内容不能被缓存。
一般地,图片文件和Flash,js文件可以设置为2个月,普通HTML文件设置为1周,其他为1天。具体根据网 站情况定。
在Apache2里启用HTTP压缩,对于html\css\js等文本类型的文件进行压缩。图片文件 jpg\gif 因为本身就是压缩
算法格式,因此再压缩没有必要,压缩率低还浪费系统CPU。注意,对于启用压缩的文件类型增加Vary头信息,
以让 SQUID能缓存压缩过的内容,否则SQUID会对所有压缩过的内容不做缓存。

 

二、Apache2的安装和设置
2.1 在安装操作系统时,注意安装 zlib,zlib-devel 这两个模块。

2.2 按照正常的安装顺序安装apache2,假设安装目录为 /usr/local/apache, 安装文件解压目录为 /usr/local/httpd-2.0.54,
修改 httpd.conf 以支持SSI。

2.3 安装 mod_expires, mod_headers
编译mod_expires
/usr/local/apache/bin/apxs -i -a -c /usr/local/httpd-2.0.54/modules/metadata/mod_expires.c
/usr/local/apache/bin/apxs -i -a -c /usr/local/httpd-2.0.54/modules/metadata/mod_headers.c

以上命令自动修改httpd.conf,激活模块

2.4 修改 httpd.conf,以设置Expires和Header

<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 days"
ExpiresByType text/html "access plus 1 weeks"
ExpiresByType image/gif "access plus 2 months"
ExpiresByType image/jpeg "access plus 2 months"
ExpiresByType application/x-shockwave-flash "access plus 2 months"
ExpiresByType application/x-javascript "access plus 2 months"
</IfModule>

<IfModule mod_headers.c>
# YEAR
<FilesMatch "\.(flv|gif|ico|jpeg)$">
Header set Cache-Control "max-age=2592000"
</FilesMatch>

# WEEK
<FilesMatch "\.(pdf|swf|js|css)$">
Header set Cache-Control "max-age=604800"
</FilesMatch>

# DAY
<FilesMatch "\.(htm|html|shtml)$">
Header set Cache-Control "max-age=604800"
</FilesMatch>
</IfModule>

要注意的是,如果Expires设置的过期时间比较短,比如5分钟,则务必保证SQUID服务器与APACHE服务器的时钟是相同的,
最好 启用时钟同步服务来同步时钟。否则可能发生所有请求都被转发到源站的情况!

2.5 安装 mod_deflate

vi /usr/local/apache/bin/apr-config
修改LDFLAGS=" " 为 LDFLAGS="-lz"
/usr/local/apache/bin/apxs -i -c /usr/local/httpd-2.0.54/modules/filters/mod_deflate.c

2.6 修改 httpd.conf,注意,mod_deflate编译时不会自动在httpd.conf里激活。

LoadModule deflate_module modules/mod_deflate.so
SetOutputFilter DEFLATE

DeflateCompressionLevel 9
AddOutputFilterByType DEFLATE text/html text/plain text/xml application/x-httpd-php

BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

# Don't compress images
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png|swf)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .(?:exe|t?gz|zip|bz2|sit|rar)$ no-gzip dont-vary
SetEnvIfNoCase Request_URI .pdf$ no-gzip dont-vary

# Make sure proxies don't deliver the wrong content
Header append Vary User-Agent env=!dont-vary

DeflateFilterNote Input input_info
DeflateFilterNote Output output_info
DeflateFilterNote Ratio ratio_info
LogFormat '"%r" %{output_info}n/%{input_info}n (%{ratio_info}n%%)' deflate
CustomLog logs/deflate_log.log deflate

2.7 测试APACHE的Expires头和压缩是否已经工作。
使用Fiddler或HttpWatch,在IE里打开Apache上的 文件,查看HTTP响应头,是否包含Expires和Cache-Control: max-age=头。
以及Content-Encoding 头的值是否为 gzip或deflate。
修改IE的高级设置里的 HTTP1.1 设置,不启用http1.1,然后重启IE后再访问apache,测试对于 http1.0的客户端,Apache
是否能正确的返回未压缩过的内 容。
Http1.1 和 1.0的区别,除了在请求头里的协议版本号不同外,1.1还增加了一个 Accept-Encoding: gzip, deflate 头,以
通知服务器自己能接收压缩过的内容。
在通过SQUID加速的情况下,SQUID会把同一个URL的压缩版本和非 压缩版本当做是两个不同的资源对待。一定程度上会浪费
缓存空间。不过好在现在http1.0的客户端少之又少,问题不大。

 

三、SQUID的安装和设置

3.1 安装操作系统时保证 gcc,g++已经安装。

3.2 为了性能,修改操作系统临时端口范围和文件描述符限制。

3.3 安装SQUID3.0
tar zxvf squid-3.0.STABLE13.tar.gz
cd squid-3.0.STABLE13
./configure --prefix=/usr/local/squid \
--disable-carp \
--with-aufs-threads=32 \
--with-pthreads \
--enable-storeio='ufs,aufs,null' \
--enable-disk-io='AIO,Blocking' \
--enable-removal-policies='heap,lru' \
--disable-wccp \
--enable-kill-parent-hack \
--disable-snmp --disable-poll \
--disable-select \
--enable-auth=basic \
--with-aio \
--disable-ident-lookup \
--with-filedescriptors=65536
make
make install

3.3 修改 etc/squid.conf 配置文件
例子配置:
visible_hostname mycacheserver 
#给服务器一个名字

http_port 80 vhost vport       
#squid 监听的端口,vhost 表示支持虚拟主机方式

cache_mem 512 MB
#根据操作系统情况设置内存缓存的大小,一般不要操作物理内存总量的 50%

maximum_object_size_in_memory 2048 KB
memory_replacement_policy lru
cache_dir ufs /tmp 512 16 256
#设置磁盘缓存所在的目录,请保证这个目录是存在的,且具有权限。

max_open_disk_fds 0
minimum_object_size 0 KB
maximum_object_size 32768 KB
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh
access_log /usr/local/squid/var/logs/page_zs_access_log combined
pid_filename /usr/local/squid/var/logs/squid.pid
cache_store_log none

cache_peer 192.168.2.5 parent 80 0 no-query no-digest originserver forceddomain=www.turbocms.com name=www
# 192.168.2.5 parent 83 代表 web服务器的地址和端口
# forceddomain 表示向源服务器发送HOST头。
# name 只是取一个名字,在后面的指令里用这个来引用

cache_peer_domain www www.turbocms.com
#squid 根据客户端发过来的这个HOST头来区分用哪个虚拟主机来处理。

cache_peer_access www allow all
http_access allow all
acl QUERY urlpath_regex cgi-bin .php .cgi .avi .wmv .rm .ram .mpg .mpeg .zip .exe
cache deny QUERY

#以下两条命令防止用户按F5时的 If-Modified-Since 请求被转发到源站
refresh_pattern -i .jpg$ 1440 50% 129600 override-expire override-lastmod reload-into-ims ignore-reload ignore-no-cache ignore-private
refresh_pattern -i .gif$ 1440 50% 129600 override-expire override-lastmod reload-into-ims ignore-reload ignore-no-cache ignore-private

3.4 启动 SQUID3

sbin/squid -z  创建缓存目录(这步必须在修改完squid.conf,指定了缓存目录之后执行)
sbin/squid -k parse 测试conf文件
sbin/squid 启动SQUID

3.5 其他命令
sbin/squid -k reconfigure 重新载入配制
sbin/squid -k restart
sbin/squid -k shutdown

清空所有缓存,在停止状态下:
echo "" > /tmp/swap.state
mv /tmp /tmp-old
mkdir /tmp
chmod -R 777 /tmp
squid -z
squid
rm -fr /tmp-old

bin/squidclient -p 80 -h localhost mgr:info  查看结果信息

http://www.squid-cache.org/Versions/v3/3.0/cfgman , 详细配置方法

3.6 测试SQUID的缓存是否工作。
使用Fiddler或HttpWatch可以查看响应头,发现 X-Cache: HIT from xxxxx,则表示命中缓存。
查看SQUID的日志 var/logs/access.log,如果发现 TCP_MISS,表示未命中缓存,请求被转发到源站。
如果发现 TCP_HIT,则表示命中缓存。正常情况下,第一次访问,会是TCP_MISS,再次访问就都应该是
TCP_HIT了,如果发现大量的 TCP_MISS,可能设置有问题,检查设置,否则源站会吃不消。

 

四、对于首页和频道列表页,应当定时清除CACHE(每5分钟),以及其他HTML文件被修改后应当清空CACHE。

4.1 首先配置SQUID,允许特定的IP地址上访问PURGE方法。在etc/squid.conf中增加:

#允许特定IP访问PURGE方法
acl Purge method PURGE
acl test src 192.168.2.100
http_access allow Purge test
http_access deny Purge

4.2 在源站服务器上安装TOMCAT或PHP环境。CMS使用HTTP方式部署文件到源站服务器。然后在JSP或PHP里收到
文件后就 向SQUID发一条PURGE指令。JSP里的代码可以为(PHP的请上网搜索):

<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="java.net.*,java.io.*"%>
<%
//要PURGE的页面
String strUrl="";
strUrl = request.getParameter("u");

String hostname = "192.168.2.123";
int port = 80;
InetAddress addr = InetAddress.getByName(hostname);
Socket socket = new Socket(addr, port);

BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), "UTF8"));
wr.write("PURGE " + strUrl + " HTTP/1.1\r\n");
wr.write("HOST:" + hostname + "\r\n");
wr.write("\r\n");
wr.flush();

//接收返回的结果
BufferedReader rd = new BufferedReader(new InputStreamReader(socket.getInputStream()));
String line;
while ((line = rd.readLine()) != null) {
out.println(line);
out.println("<br/>");
}
wr.close();
rd.close();
%>

 

五、对于源站采用IIS的情况
在IIS启用了压缩后,返回的头里 Expires的值为 Wed, 01 Jan 1997 12:00:00 GMT,所以SQUID会立即过期,从而无法缓存。
解决的方法为,修改 C:\windows\system32\inetsrv\MetaBase.xml,找到
HcExpiresHeader="Wed, 01 Jan 1997 12:00:00 GMT"
将其修改为 HcExpiresHeader="Wed, 01 Jan 2038 12:00:00 GMT"

另外,对于SQUID的早期版本或者Windows平台下的SQUID,他发往源站的HTTP请求始终使用http1.0,所以压缩不生效,
可 以修改 MetaBase.xml,找到:
HcNoCompressionForHttp10="TRUE"
HcNoCompressionForProxies="TRUE"
HcSendCacheHeaders="FALSE"
将 其修改为:
HcNoCompressionForHttp10="FALSE"
HcNoCompressionForProxies="FALSE"
HcSendCacheHeaders="TRUE"

 

The End

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics