您的位置:首页 > 运维架构 > 反向代理

squid+apache实现缓存加速

2016-01-19 15:49 651 查看
本实例是squid和apache在同一台机器上,squid做前端反向代理。port为80,apache作为后端web,port为81

serverip: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的port。

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

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

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

反向代理81port。81port为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

开启这个全局參数。可将client发来的no-cache转化为If-Modified-Since去处理

这个參数的设置,能够參考此博客http://blog.sina.com.cn/s/blog_56d8ea9001018xev.html

(11)hierarchy_stoplist cgi-bin ?

此为默认參数。不论什么包括问号或cgi-bin字符串的请求匹配该列表,变成不可层叠。

Squid内在的将每一个client请求标记为层叠或不可层叠。不可层叠的请求看起来不会导致cache命中。比如。POST请求的响应差点儿从不会被cache。在squid能简单的连接到原始server时,转发不可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

80port为squid的http_portport

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使用的是81port,我们直接用81port訪问就可以

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

因为我们没有设置usernamepassword直接訪问就可以,可是应用到线上则必须设置。

7.apache配置

网站的訪问配置我在直接用的是我们一个測试网站,在这不做过多介绍。但在此要介绍下apache的mod_expoires模块。此模块能够降低10%左右的反复请求,让反复的用户对指定的页面请求结果都CACHE在本地,根本不向server发出请求。

检查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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: