早就看过用squid加速apache的文章,就是懒的去玩,今天闲来郁闷,突然想玩玩,所以就有了本文(本文不算是原创,都是建立他人的基础上凑起来的,算是整理吧!)
系统:redhat as 4
apache :httpd-2.0.52-9.ent
squid :squid-2.5.STABLE6-3.4E.3
1.安装
安装squid很简单:
# yum -y install squid
配置squid
修改:/etc/squid/squid.conf成下面的
http_port 80
icp_port 0
acl QUERY urlpath_regex cgi-bin
no_cache deny QUERY
cache_mem 16 MB
cache_dir ufs /tmp 256 16 256
log_icp_queries off
buffered_logs on
emulate_httpd_log on
redirect_rewrites_host_header off
half_closed_clients off
acl all src 0.0.0.0/0.0.0.0
http_access allow all
cache_mgr admin
cache_effective_user squid
cache_effective_group squid
httpd_accel_host virtual
httpd_accel_uses_host_header on
httpd_accel_port 81
这里解释一下
http_port”参数指定Squid监听
浏览器客户请求的
端口号,这里当然是80了
cache_dir ufs /tmp 256 16 256
cache_dir参数设定使用的存储系统的类型。一般情况下都类型应该是ufs,目录应该是“/tmp”,在该目录下使用的缓冲值为256MB,允许在“/tmp”下创建的第一级子目录数为16,每个第一级子目录下可以创建的第二级子目录数量为256
选项“httpd_accel_host”和“httpd_accel_port”定义了真正的Web
服务器的
主机名和端口号。在这里的配置中,真正的HTTP
服务器运行的状况是虚拟
主机:virtual而运行的端口是:81。
选项“httpd_accel_uses_host_header”是什么意思就不清楚了,还没看说明
3.修改apache的配置文档
这里就不说apache的安装了!
注意3个地方:
(1)
Port 80这里改成:Port 81
(2)
NameVirtualHost 221.6.145.41:81
如何没有这句,就加上这句!
(3)虚拟主机:
ServerAdmin
webmaster@ourlinux.net
DocumentRoot /var/www/yum.ourlinux.net
ServerName apt.ourlinux.net
ErrorLog logs/yum.ourlinux.net-error_log
CustomLog logs/yum.ourlinux.net-access_log common
ServerAdmin
webmaster@ourlinux.net
DocumentRoot /var/www/apt.ourlinux.net
ServerName apt.ourlinux.net
ErrorLog logs/yum.ourlinux.net-error_log
CustomLog logs/yum.ourlinux.net-access_log common
ip部分要一致!
4.启动!
先启动httpd
再启动squid
作者: emptyhua 2006-4-18 19:30 回复此发言
--------------------------------------------------------------------------------
2 回复:用squid加速apache[转]
传统的Squid反向
代理加速十分依赖last-modified这类HTTP首部信息。
不幸的是
微软的SharePoint无法生成带有恰当首部的HTTP响应。
更不幸的是这回遇到的SharePoint Portal在整合了多个自定义WebPart之后非常之慢,首页响应甚至超过了三秒钟。
下面介绍一路旁门左道,用squid加apache解决在这种不规范情形下的逆向缓存。
$ rpm -q httpd squid
httpd-2.0.40-21.11
squid-2.5.STABLE1-3.9
首先把apache bind到localhost:88。
接着将squid配置为普通的反向代理加速,这里就不细说了。
然后把以下部分添加到squid.conf:
cache_peer 127.0.0.1 parent 88 0 proxy-only no-query
acl cachable urlpath_regex "/etc/squid/cachable.lst"
cache_peer_access 127.0.0.1 allow cachable
cache_peer_access 127.0.0.1 deny all
编写如下脚本,丢到crontab:
SITE=www0.xxx.org
cd /var/www/
#wget -r -l 2 -N -A *.aspx,*.js,*.css,*.htc,*.gif,*.jpg,*.png,*.ico,*.swf http://$SITE/default.aspx
#use the line above to cache more stuff
wget -r -l 1 -N -A *.aspx,*.js,*.css,*.htc http://$SITE/default.aspx
ln -sf $SITE html
rm -rf cache/
cp -Rf $SITE cache
ln -sf cache html
rm -rf $SITE/
cd /var/www/html/
find . -type f | cut -b 2- | replace . . | awk '{print "^"$1"$"}' > /etc/squid/cachable.lst
/etc/init.d/squid reload
下面是加速前后的速度对比,从五秒多到二十多毫秒,
用户体验得到极大改善,
加速之前:
$ ab -n 5
http://www0.xxx.org/default.aspx
...
Server Software: Microsoft-IIS/6.0
Server Hostname: www0.xxx.org
Server Port: 80
Document Path: /default.aspx
Document Length: 127408 bytes
Concurrency Level: 1
Time taken for tests: 26.455610 seconds
Complete requests: 5
Failed requests: 0
Write errors: 0
Total transferred: 639175 bytes
HTML transferred: 637040 bytes
Requests per second: 0.19 [#/sec] (mean)
Time per request: 5291.122 [ms] (mean)
...
加速以后:
$ ab -n 5
http://cache.xxx.org/default.aspx
...
Server Software: Apache/2.0.40
Server Hostname: cache.xxx.org
Server Port: 80
Document Path: /default.aspx
Document Length: 127408 bytes
Concurrency Level: 1
Time taken for tests: 0.132630 seconds
Complete requests: 5
Failed requests: 0
Write errors: 0
Total transferred: 638600 bytes
HTML transferred: 637040 bytes
Requests per second: 37.70 [#/sec] (mean)
Time per request: 26.526 [ms] (mean)
...
默认的squid配置不会缓存带有HTTP认证信息的请求,只要你的squid本身不跟NTLM过不去,那登陆操作倒是应该没有什么特别的问题。