基於Squid3.0的反向代理加速實現
Squid是一個更專用的代理服務器,性能和效率會比Apache的mod_proxy高很多。
下載squid3.0
http://www.squid-cache.org/Versions/v3/3.0/squid-3.0.STABLE15.tar.gz
squid的編譯安裝:
groupadd squid
adduser -g squid -d /dev/null -s /sbin/nologin squid
tar xzvf squid-3.0.STABLE15.tar.gz
cd squid-3.0.STABLE15
./configure --prefix=/usr/local/squid --sysconfdir=/usr/local/squid/etc --enable-arp-acl --enable-linux-netfilter --enable-pthreads --enable-err-language="Simplify_Chinese" --enable-storeio='ufs,aufs,null' --enable-default-err-language="Simplify_Chinese" --enable-auth="basic" --enable-baisc-auth-helpers="NCSA" --enable-underscore --enable-cachemgr --enable-async-io --with-large-files --with-filedescriptors=65536 –enable-epoll
make
make install
squid的配置
cd /usr/local/squid
mkdir cache
chown -R squid.squid var
chown -R squid.squid cache
vi /usr/local/squid/etc/squid.conf
在/etc/hosts中:加入內部的DNS解析,比如:
192.168.1.103 www.zhang.com
192.168.1.104 hehe.aa.zhang.com
---------------------cut here----------------------------------
# visible name
visible_hostname cache.zhang.com
# cache config: space use 1G and memory use 256M
cache_dir ufs /usr/local/squid/cache 1024 16 256
cache_mem 256 MB
maximum_object_size 20000 KB
maximum_object_size_in_memory 4096 KB
cache_effective_user squid
cache_effective_group squid
##設置端口
http_port 80 vhost vport
#2.5版本的反向代理配置
httpd_accel_host virtual
httpd_accel_single_host off
httpd_accel_port 80
httpd_accel_uses_host_header on
httpd_accel_with_proxy on
#2.5以上的反向代理加速配置
#代理到本機的80端口的服務,僅僅做爲原始內容服務器
cache_peer 127.0.0.1 parent 80 0 no-query originserver
# accelerater my domain only
acl acceleratedHostA dstdomain .example1.com
acl acceleratedHostB dstdomain .example2.com
acl acceleratedHostC dstdomain .example3.com
# access arc
acl all src 0.0.0.0/0.0.0.0
acl QUERY urlpath_regex cgi-bin .php .cgi .avi .wmv .rm .ram .mpg .mpeg .zip .exe
cache deny QUERY
# Allow requests when they are to the accelerated machine AND to the
# right port with right protocol
http_access allow acceleratedHostA
http_access allow acceleratedHostB
http_access allow acceleratedHostC
# log
emulate_httpd_log on
cache_store_log none
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh
pid_filename /var/log/squid/var/logs/squid.pid
cache_access_log /usr/local/squid/log/access.log combined
cache_log /usr/local/squid/log/cache.log
pid_filename /usr/local/squid/var/logs/squid.pid
# manager
acl manager proto cache_object
http_access allow manager all
#cachemgr_passwd pass all
###配置多個服務器的代理
###cache_peer
cache_peer x.x.x.x parent 80 0 no-query originserver name=foo
cache_peer y.y.y.y parent 80 0 no-query originserver name=bar
###cache_peer_domain
cache_peer_domain foo www.carl.com
cache_peer_domain bar www.zhang.com
acl sites dstdomain .carl.com
acl sites dstdomain .zhang.com
###cache_peer_access
cache_peer_access foo allow sites
cache_peer_access bar allow sites
http_access allow all
----------------------cut here---------------------------------
附加功能:
#僅僅允許80端口的代理
acl Safe_ports port 80 # http
http_access deny !Safe_ports
http_access allow all
#設置防圖片盜鏈的,其中aaa,和bbb分別是虛擬主機的域名,referer中必須包含有aaa或者bbb的域名才能訪問圖片
acl picurl url_regex -i \.bmp$ \.png$ \.jpg$ \.gif$ \.jpeg$
acl mystie1 referer_regex -i aaa
http_access allow mystie1 picurl
acl mystie2 referer_regex -i bbb
http_access allow mystie2 picurl
測試配置文件:
#/usr/local/squid/sbin/squid -k parse
創建緩存目錄:
/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)
more /usr/local/squid/var/logs/page_zs_access_log |grep TCP_MEM_HIT
該指令可以看到在squid運行過程中,有那些文件被squid緩存到內存中,並返回給訪問用戶
more /usr/local/squid/var/logs/page_zs_access_log |grep TCP_HIT
該指令可以看到在squid運行過程中,有那些文件被squid緩存到cache目錄中,並返回給訪問用戶
more /usr/local/squid/var/logs/page_zs_access_log |grep TCP_MISS
該指令可以看到在squid運行過程中,有那些文件沒有被squid緩存,而是現重原始服務器獲取並返回給訪問用戶
可以查看下命中率及其他相關信息
#/usr/local/squid/bin/squidclient -p 80 -h localhost mgr:info
有的時候我們可能還想手動刪除一些緩存,這也可以使用squidclient命令來實現:
/usr/local/squid/sbin/squidclient -h HOST -p PORT -m PURGE URL
使用簡單的負載均衡:
cache_peer 192.168.1.103 parent 80 0 no-query originserver round-robin name=carl1
cache_peer 127.0.0.1 parent 80 0 no-query originserver round-robin name=carl2
cache_peer_domain carl1 www.carl.com
cache_peer_domain carl2 www.carl.com
配置實例:CentOS release 5.4環境下測試通過!
1、單域名!
cache_mgr [email protected]
cache_effective_user squid
cache_effective_group squid
cache_mem 256 MB
cache_swap_low 90
cache_swap_high 95
cache_dir ufs /usr/local/squid/var/cache 1000 16 256
maximum_object_size 20000 KB
maximum_object_size_in_memory 4096 KB
cache_access_log /usr/local/squid/var/logs/access.log
cache_log /usr/local/squid/var/logs/cache.log
cache_store_log /usr/local/squid/var/logs/store.log
visible_hostname test.squid.com
http_port 80 accel vhost vport
cache_peer 192.168.1.103 parent 80 0 no-query originserver
acl carl dstdomain .carl.com
acl Purge method PURGE
acl Manager proto cache_object
acl Localhost src 127.0.0.1/32
http_access allow carl
http_access allow Purge Localhost
http_access allow Manager Localhost
http_access deny Manager
http_access deny All
2、多域名!
cache_mgr [email protected]
cache_effective_user squid
cache_effective_group squid
cache_mem 256 MB
cache_swap_low 90
cache_swap_high 95
cache_dir ufs /usr/local/squid/var/cache 1000 16 256
cache_access_log /usr/local/squid/var/logs/access.log
cache_log /usr/local/squid/var/logs/cache.log
cache_store_log /usr/local/squid/var/logs/store.log
visible_hostname test.squid.com
http_port 80 vhost vport
cache_peer 192.168.1.103 parent 80 0 no-query originserver name=carl
cache_peer 127.0.0.1 parent 81 0 no-query originserver name=zhang
cache_peer_domain carl www.carl.com
cache_peer_domain zhang www.zhang.com
acl sites1 dstdomain .carl.com
acl sites2 dstdomain .zhang.com
acl Purge method PURGE
acl Manager proto cache_object
acl Localhost src 127.0.0.1/32
#cache_peer_access deny All
cache_peer_access carl allow sites1
cache_peer_access zhang allow sites2
http_access allow sites1
http_access allow sites2
http_access allow Purge Localhost
http_access allow Manager Localhost
http_access deny Manager
http_access deny All
squid羣集:
cache_mgr [email protected]
cache_effective_user squid
cache_effective_group squid
cache_mem 256 MB
cache_swap_low 90
cache_swap_high 95
cache_dir ufs /usr/local/squid/var/cache 1000 16 256
cache_access_log /usr/local/squid/var/logs/access.log
cache_log /usr/local/squid/var/logs/cache.log
cache_store_log /usr/local/squid/var/logs/store.log
visible_hostname 192.168.1.121
http_port 192.168.1.121:80 vhost vport
icp_port 3130
cache_peer 192.168.1.159 parent 80 0 no-query originserver name=carl1
#cache_peer 127.0.0.1 parent 80 0 no-query originserver name=carl2
cache_peer_domain carl1 www.carl.com
#cache_peer_domain carl2 www.carl.com
cache_peer 192.168.1.108 sibling 80 3130 name=cache1
cache_peer 192.168.1.121 sibling 80 3130 name=cache2
acl Safe_ports port 80
acl Safe_ports port 3130
acl sites dstdomain .carl.com
acl Purge method PURGE
acl Manager proto cache_object
acl Localhost src 127.0.0.1/32
#cache_peer_access carl allow sites1
#cache_peer_access zhang allow sites2
#cache_peer_access deny All
http_access allow sites
http_access allow Safe_ports
http_access allow Purge Localhost
http_access allow Manager Localhost
http_access deny Manager
http_access deny All