基於Apache mod_proxy的反向代理緩存加速實現 Apache包含了mod_proxy模塊,可以用來實現代理 服務器 ,針對後台服務器的反向加速 安裝apache 1.3.x 編譯時: --enable-shared=max --enable-module=most 注:Apache 2.x中mod_proxy已經被分離成mod_proxy和mod
基於Apache mod_proxy的反向代理緩存加速實現 Apache包含了mod_proxy模塊,可以用來實現代理
服務器,針對後台服務器的反向加速
安裝apache 1.3.x 編譯時:
--enable-shared=max --enable-module=most
注:Apache 2.x中mod_proxy已經被分離成mod_proxy和mod_cache:同時mod_cache有基於文件和基於內存的不同實現
創建/var/www/proxy,設置apache服務所用戶可寫
mod_proxy配置樣例:反相代理緩存+緩存
架設前台的www.example.com反向代理後台的www.backend.com的8080端口服務。
修改:httpd.conf
<VirtualHost *>
ServerName www.example.com
ServerAdmin
[email protected] # reverse proxy setting
ProxyPass / http://www.backend.com:8080/
ProxyPassReverse / http://www.backend.com:8080/
# cache dir root
CacheRoot "/var/www/proxy"
# max cache storage
CacheSize 50000000
# hour: every 4 hour
CacheGcInterval 4
# max page expire time: hour
CacheMaxExpire 240
# Expire time = (now - last_modified) * CacheLastModifiedFactor
CacheLastModifiedFactor 0.1
# defalt expire tag: hour
CacheDefaultExpire 1
# force complete after precent of content retrived: 60-90%
CacheForceCompletion 80
CustomLog /usr/local/apache/logs/dev_a
clearcase/" target="_blank" >ccess_log combined
</VirtualHost>
基於Squid的反向代理加速實現 Squid是一個更專用的代理服務器,性能和效率會比Apache的mod_proxy高很多。
如果需要combined格式日志補丁:
http://www.squid-cache.org/mail-archive/squid-dev/200301/0164.html
squid的編譯:
./configure --enable-useragent-log --enable-referer-log --enable-default-err-language=Simplify_Chinese --enable-err-languages="Simplify_Chinese English" --disable-internal-dns
make
#make install
#cd /usr/local/squid
make dir cache
chown squid.squid *
vi /usr/local/squid/etc/squid.conf
在/etc/hosts中:加入內部的DNS解析,比如:
192.168.0.4 www.chedong.com
192.168.0.4 news.chedong.com
192.168.0.3 bbs.chedong.com
---------------------cut here----------------------------------
# visible name
visible_hostname cache.example.com
# cache config: space use 1G and memory use 256M
cache_dir ufs /usr/local/squid/cache 1024 16 256
cache_mem 256 MB
cache_effective_user squid
cache_effective_group squid
http_port 80
httpd_accel_host virtual
httpd_accel_single_host off
httpd_accel_port 80
httpd_accel_uses_host_header on
httpd_accel_with_proxy on
# accelerater my domain only
acl acceleratedHostA ds
tdomain .example1.com
acl acceleratedHostB dstdomain .example2.com
acl acceleratedHostC dstdomain .example3.com
# accelerater http protocol on port 80
acl acceleratedProtocol protocol HTTP
acl acceleratedPort port 80
# access arc
acl all src 0.0.0.0/0.0.0.0
# Allow requests when they are to the accelerated machine AND to the
# right port with right protocol
http_access allow acceleratedProtocol acceleratedPort acceleratedHostA
http_access allow acceleratedProtocol acceleratedPort acceleratedHostB
http_access allow acceleratedProtocol acceleratedPort acceleratedHostC
# logging
emulate_httpd_log on
cache_store_log none
# manager
acl manager proto cache_object
http_access allow manager all
cachemgr_passwd pass all
----------------------cut here---------------------------------
創建緩存目錄:
/usr/local/squid/sbin/squid -z
啟動squid
/usr/local/squid/sbin/squid
停止squid:
/usr/local/squid/sbin/squid -k shutdown
啟用新配置:
/usr/local/squid/sbin/squid -k reconfig
通過crontab每天0點截斷/輪循日志:
0 0 * * * (/usr/local/squid/sbin/squid -k rotate)
附:SQUID性能測試試驗 phpMan.php是一個基於php的man page server,每個man
page需要調用後台的man命令和很多頁面格式化工具,系統負載比較高,提供了Cache
Friendly的URL,以下是針對同樣的頁面的性能測試資料:
測試環境:Redhat 8 on Cyrix 266 / 192M Mem
測試程序:使用apache的ab(apache benchmark):
測試條件:請求50次,並發50個連接
測試項目:直接通過apache 1.3 (80端口) vs squid 2.5(8000端口:加速80端口)
測試1:無CACHE的80端口動態輸出:
ab -n 100 -c 10 http://www.chedong.com:81/phpMan.php/man/kill/1
This is ApacheBench, Version 1.3d <$Revision: 1.2 $> apache-1.3
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd,
http://www.zeustech.net/
Copyright (c) 1998-2001 The Apache Group, http://www.apache.org/
Benchmarking localhost (be patient).....done
Server Software:
Apache/1.3.23
Server Hostname: localhost
Server
Port:
80
Document Path:
/phpMan.php/man/kill/1
Document Length: 4655 bytes
Concurrency Level: 5
Time taken for tests: 63.164 seconds
Complete requests: 50
Failed requests: 0
Broken pipe errors: 0
Total transferred: 245900 bytes
HTML transferred: 232750 bytes
Requests per second: 0.79 [#/sec] (mean)
Time per request: 6316.40 [ms]
(mean)
Time per request: 1263.28 [ms]
(mean, across all concurrent requests)
Transfer rate:
3.89 [Kbytes/sec] received
Connnection Times (ms)
min mean[+/-sd] median max
Connect: 0
29 106.1 0 553
Processing: 2942 6016
1845.4 6227 10796
Waiting:
2941 5999 1850.7 6226 10795
Total:
2942 6045 1825.9 6227 10796
Percentage of the requests served within a certain time (ms)
50% 6227
66% 7069
75% 7190
80% 7474
90% 8195
95% 8898
98% 9721
99% 10796
100% 10796 (last request)
測試2:SQUID緩存輸出
/home/apache/bin/ab -n50 -c5
"http://localhost:8000/phpMan.php/man/kill/1"
This is ApacheBench, Version 1.3d <$Revision: 1.2 $> apache-1.3
Copyright (c) 1996 Adam Twiss, Zeus Technology Ltd,
http://www.zeustech.net/
Copyright (c) 1998-2001 The Apache Group, http://www.apache.org/
Benchmarking localhost (be patient).....done
Server Software:
Apache/1.3.23
Server Hostname: localhost
Server
Port:
8000
Document Path:
/phpMan.php/man/kill/1
Document Length: 4655 bytes
Concurrency Level: 5
Time taken for tests: 4.265 seconds
Complete requests: 50
Failed requests: 0
Broken pipe errors: 0
Total transferred: 248043 bytes
HTML transferred: 232750 bytes
Requests per second: 11.72 [#/sec] (mean)
Time per request: 426.50 [ms] (mean)
Time per request: 85.30 [ms] (mean,
across all concurrent requests)
Transfer rate:
58.16 [Kbytes/sec] received
Connnection Times (ms)
min mean[+/-sd] median max
Connect:
0 1
9.5 0 68
Processing:
7 83 537.4
7 3808
Waiting:
5 81 529.1
6 3748
Total:
7 84 547.0
7 3876
Percentage of the requests served within a certain time (ms)
50% 7
66% 7
75% 7
80% 7
90% 7
95% 7
98% 8
99% 3876
100% 3876 (last request)
結論:No Cache / Cache = 6045 / 84 = 70