squid+apache实现缓存加速

本实例是squid和apache在同一台机器上,squid做前端反向代理,端口为80,apache作为后端web,端口为81

服务器ip:172.16.8.102

1.首先介绍下版本选择,在进行测试之前一定要选定一个合适的squid版本,在此推荐2.7,她和2.6功能相似但更好的支持http1.1,也有3.0以上版本的不少特性。

2.squid2.7安装

cd /usr/local/src

tar -zxvf squid-2.7.STABLE9.tar.gz

cd squid-2.7.STABLE9

 ./configure -prefix=/usr/local/squid2.7 -enable-xmalloc-statistics --enable-async-io=320 --with-maxfd=65536 -enable-useragent-log -enable-referer-log -enable-epoll -disable-poll -enable-large-cache-files -disable-internal-dns -enable-linux-netfilter -enable-truncate -enable-x-accelerator-vary -enable-follow-x-forwarded-for -with-large-files -with-pthreads -enable-storeio="aufs,coss,diskd,ufs" -enable-kill-parent-hack -enable-gnuregex -enable-cache-digests -enable-delay-pools -enable-stacktraces -enable-default-err-language=Simplify_Chinese -enable-err-languages="Simplify_Chinese English" --enable-auth="basic" --enable-basic-auth-helpers="NCSA" --enable-snmp

make && make install

3.创建suqid用户

useradd squid

chown -R squid.squid squid

设置suqid安装文件目录的属性,否则squid可能会无法启动

4.创建缓目录

cd /data

mkdir -p squid/cache

chown -R squid.squid squid

5.创建日志目录

 cd /var/log

mkdir squid

chown -R squid.squid squid

设置suqid日志的属性,否则squid可能会无法启动


5.配置squid.conf

cd /usr/local/squid2.7

vim squid.conf

acl all src all
acl manager proto cache_object
acl localhost src 127.0.0.1/32
acl to_localhost dst 127.0.0.0/8 0.0.0.0/32
acl localnet src 10.0.0.0/8 # RFC1918 possible internal network
acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl SSL_ports port 443
acl Safe_ports port 80 # http
acl Safe_ports port 81 # http
acl Safe_ports port 3128 # http
acl Safe_ports port 8080 # http

acl Safe_ports port 21 # ftp
acl Safe_ports port 443 # https
acl Safe_ports port 70 # gopher
acl Safe_ports port 210 # wais
acl Safe_ports port 1025-65535 # unregistered ports
acl Safe_ports port 280 # http-mgmt
acl Safe_ports port 488 # gss-http
acl Safe_ports port 591 # filemaker
acl Safe_ports port 777 # multiling http
acl CONNECT method CONNECT
http_access allow manager localhost localnet
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow all
icp_access allow localnet
icp_access deny all
http_port 80 accel vhost vport
cache_peer 127.0.0.1 parent 81 0 no-query originserver name=test
cache_peer_access test allow all
hierarchy_stoplist cgi-bin ?
cache_mem 1024 MB
maximum_object_size_in_memory 6 MB
memory_replacement_policy lru
cache_replacement_policy lru
cache_dir ufs /data/squid/cache 1024 16 256

maximum_object_size 6 MB
cache_swap_low 90
cache_swap_high 95
access_log /var/log/squid/access.log
cache_log /var/log/squid/cache.log 

refresh_pattern ^ftp: 144020%10080
refresh_pattern ^gopher: 14400%1440
refresh_pattern -i (/cgi-bin/|\?) 0 0%0
refresh_pattern \.(jpg|png|gif|mp3|xml|html|htm|css|js|aspx) 1440    50%     2880    ignore-reload
refresh_pattern . 020%4320
acl shoutcast rep_header X-HTTP09-First-Line ^ICY.[0-9]
cache_vary on
acl apache rep_header Server ^Apache
broken_vary_encoding allow all
cache_effective_user squid
cache_effective_group squid

visible_hostname 172.16.8.102 
icp_port 0
reload_into_ims on
coredump_dir /usr/local/squid2.7/var/cache


所更改的参数解释:

(1)acl Safe_ports port 81 # http
     acl Safe_ports port 3128 # http
     acl Safe_ports port 8080 # http

此处定义可以访问的端口,由于http_access deny !Safe_ports,只要不是在Safe_ports中出现的端口都会被限制,这个可以根据实际情况而定.

(2)http_access allow all

在此我定义的是所有的ip都可以访问squid,这也是为了方便我在测试环境中使用,如果是线上应用请制定相应的访问限制。

(3)http_port 80 accel vhost vport

定义访问squid的端口。

如果不加accel vhost vport说明你的squid默认做为一个缓存服务器,这个时候如果客户端有请求发到了squid,squid起到的是路由功能,把请求转发出去,被真正的web server接收,web server返回响应,当squid接收到响应后,根据响应头决定是否缓存,此时的squid,只是一个cache server。

如果加上accel vhost vport说明你的squidsquid就从一个缓存(cache server)变成了一个web server, 这个时候squid在80端口监听请求,同时和web server的请求端口(vhost vport)绑定,这个时候请求到了squid,squid是不用转发请求的,而是直接要么从缓存中拿数据要么向绑定的端口直接请求数据。另外绑定端口还有一个好处,可以充分利用http 响应头中的到期时间头和etag头。 

     cache_peer 127.0.0.1 parent 81 0 no-query originserver name=test

反向代理81端口,81端口为apache;no-query不做查询,直接获取数据;orginserver 代表是源服务器;name定义反向代理的名字,可以对acl控制

(4)cache_mem 1024 MB

设置所用内存的大小
maximum_object_size_in_memory 6 MB

设置缓存对象所占用的最大内存
memory_replacement_policy lru

cache_replacement_policy lru

替换机制
cache_dir ufs /data/squid/cache 1024 16 256

缓存目录的大小,应该不低于cache_mem
maximum_object_size 6 MB

最大的单个缓存对象

(5)access_log /var/log/squid/access.log
cache_log /var/log/squid/cache.log 

设置squid的日志目录,注意日志权限,否则有可能导致squid无法启动

(6)refresh_pattern \.(jpg|png|gif|mp3|xml|html|htm|css|js|aspx) 1440    50%     2880    ignore-reload

设置jpg等后缀格式的文件在cache中停留的时间

(7)cache_vary on

如果你发现squid缓存命中率很低,即使调整refresh_pattern,maximum_object_size_in_memory,加大内存都没用;利用cachemgr.cgi统计工具中的In-Memory and In-Transit Objects,发现HTML/js/css not_in_memory,而jpg/png等图片都缓存了,则可能是有由于这个参数off导致。

这是因为apache在 response header 中返回了一个vary:Accept-encoding ,则squid在存储缓存文件时需要将“浏览器”request header 信息中的Accept-encoding字段的值(gzip,deflate之类)作为缓存key的一部分,因此对于不同的Accept-encoding字段值,都需要保存不同的文件。(IE与firefox的请求头的Accept-encoding字段值中就有一个空格的差别下次

请求到squid的时候,需要先找到一个缓存文件的索引文件,根据索引文件中的不同的Accep-encoding值再去找相应的缓存文件。 cache vary off,那么经过gzip压缩后含有vary头的,都不会被cache了,所以和上述缓存策略没什么影响,而jpg本来是被压缩过,不含vary,自然会被cache了。

(8)cache_effective_user squid
cache_effective_group squid

设置squid的用户和组

(9)icp_port 0

禁用icp邻居,如果你想使用squid集群可以更改这个参数

(10)reload_into_ims on

 开启这个全局参数,可将客户端发来的no-cache转化为If-Modified-Since去处理

这个参数的设置,可以参考此博客http://blog.sina.com.cn/s/blog_56d8ea9001018xev.html

(11)hierarchy_stoplist cgi-bin ?

此为默认参数,任何包含问号或cgi-bin字符串的请求匹配该列表,变成不可层叠。

 Squid内在的将每个客户端请求标记为层叠或不可层叠。不可层叠的请求看起来不会导致cache命中。例如,POST请求的响应几乎从不会被cache。在squid能简单的连接到原始服务器时,转发不可cache目标的请求到邻居cache,纯粹是浪费资源。
某些区分层叠和不可层叠请求的规则,在squid里难于编码。例如,POST和PUT方式总是不可层叠的。然而,hierarchy_stoplist指令允许你定制这种算法。它包含一个字符串列表,当在URI里发现它们时,squid将请求标记为不可层叠。


更改完配置文件后,可以进行初始化缓存目录和启动squid了

/usr/local/squid2.7/sbin/squid -z

/usr/local/squid2.7/sbin/squid 

lsof -i:80

COMMAND  PID  USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
squid   1399 squid   17u  IPv4 9965038      0t0  TCP *:http (LISTEN)

说明启动成功,如果发现启动没成功,则检查配置文件

6.cachemgr.cgi统计工具

vim /usr/local/squid2.7/etc/cachemgr.conf

localhost:80

80端口为squid的http_port端口

cd /var/www/html

mkdir squid/cgi-bin

cp /usr/local/squid2.7/libexec/cachemgr.cgi /var/www/html/squid/cgi-bin

在apache中设置相应的访问

vim /etc/httpd/conf.d/squid.conf

ScriptAlias /squid/cgi-bin/cachemgr.cgi /usr/local/squid2.7/libexec/cachemgr.cgi

# Only allow access from localhost by default
<Location /squid/cgi-bin/cachemgr.cgi>
 order allow,deny
# allow from localhost.localdomain
 allow from all
 # Add additional allowed hosts as needed
 # allow from .example.com
</Location>

service httpd restart使配置文件生效。

由于Apache使用的是81端口,我们直接用81端口访问即可

http://172.16.8.102:81/squid/cgi-bin/cachemgr.cgi

由于我们没有设置用户名密码直接访问即可,但是应用到线上则必须设置。

7.apache配置

站点的访问配置我在直接用的是我们一个测试站点,在这不做过多介绍。但在此要介绍下apache的mod_expoires模块,此模块可以减少10%左右的重复请求,让重复的用户对指定的页面请求结果都CACHE在本地,根本不向服务器发出请求。

检查apache按安装有mod_expires模块,因此我们只需要在/etc/httpd/conf.d/mod_expires.conf中进行配置即可。

vim /etc/httpd/conf.d/mod_expires.conf

<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 12 hours"
ExpiresByType text/html "access plus 3 days"
ExpiresByType text/plain "access plus 3 days"
ExpiresByType text/css  "access plus 7 days"
ExpiresByType image/gif "access plus 30 days"
ExpiresByType image/png "access plus 30 days"
ExpiresByType image/jpeg "access plus 30 days"
ExpiresByType image/jpg "access plus 30 days"
ExpiresByType image/x-icon "access plus 30 days"
ExpiresByType video/x-flv  "access plus 30 days"
ExpiresByType application/x-shockwave-flash "access plus 30 days"
</IfModule>

其中对所有文件可以缓存的文件都默认设置为12小时,对text/image/video等类型的文件重新设置成相应的缓存时间。

设置完成后service httpd restart 即可。


最后我们访问测试,然后查看缓存命中了。


另,在apache前端加squid后,我的负载能够达到4000,但是squid消耗的cpu也有点高啊。

[root@localhost webbench-1.5]# webbench -c 4000 -t 30 http://172.16.8.102/Login.php
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://172.16.8.102/Login.php
4000 clients, running 30 sec.

Speed=685846 pages/min, 4664574 bytes/sec.
Requests: 342923 susceed, 0 failed.


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值