squid分流到tomcat,实现负载
测试环境:两Ip 192.168.1.1 和192.168.1.2
192.168.1.1(squid+tomcat)
192.168.1.2 (tomcat)
一、两机器安装tomcat,不详细写了
二、squid的安装
1.下载squid-2.7.STABLE9-20100914.tar.gz
2.安装tar -xvzf squid-2.7.STABLE9-20100914.tar.gz
cd squid-2.7.STABLE9-20100914
./configure --prefix=/usr/local/squid
具体编译参数可查看http://blog.wmdiy.com/?p=100
3.配置
vi /usr/local/squid/etc/squid.conf
http_port 80 vhost vport
visible_hostname text.com
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 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
acl purge method PURGE
http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow localnet
http_access allow all
icp_access allow localnet
icp_access deny all
http_access allow purge localhost
http_access deny purge
cache_mem 4096 MB
#设置Squid所能使用的内存共400M
cache_dir ufs /opt/cache 40960 16 256
#磁盘缓存的类型和目录,大小,一二级目录的设置,这里磁盘缓存大小是20G
cache_swap_low 90
cache_swap_high 95
maximum_object_size 2000 KB
#最大缓存文件大小,超过这个值则不缓存,这个值因人而异
maximum_object_size_in_memory 128 KB
#装入内存缓存的文件大小,超过则不缓存
cache_store_log none
emulate_httpd_log on
#打开emulate_httpd_log选项,将使Squid仿照Aapche的日志格式
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %h" "%{User-Agent}>h" %Ss:%Sh
acl QUERY urlpath_regex cgi-bin
acl ddcache urlpath_regex \getb**.jsp\?
no_cache allow ddcache
no_cache deny QUERY
cache_peer 127.0.0.1 parent 80800 no-query no-digest originserver name=www1 round-robin
cache_peer 192.168.1.2 parent 8080 0 no-query no-digest originserver name=www2 round-robin
cache_peer_domain www1 www.xxx.com
cache_peer_domain www2 www.xxx.com
cache_peer_access www1 allow all
cache_peer_access www2 allow all
cache_effective_user squid
cache_effective_group squid
#squid使用的用户组和用户名
hierarchy_stoplist cgi-bin ?
access_log /usr/local/squid/var/logs/access.log squid
refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern -i (/cgi-bin/|\?) 0 0% 0
refresh_pattern . 0 20% 4320
refresh_pattern -i searchNP\?1440 25% 10080 override-expire override-lastmod reload-into-ims ignore-reloadignore-no-cache ignore-private
refresh_pattern -i \.jsp\?1440 25% 10080 override-expire override-lastmod reload-into-ims ignore-reloadignore-no-cache ignore-private
refresh_pattern -i \.gif$ 1440 50% 2880 ignore-reload
refresh_pattern -i \.jpg$ 1440 50% 2880 ignore-reload
refresh_pattern -i \.jpeg$1440 50% 2880 ignore-reload
refresh_pattern -i \.png$ 1440 50% 2880 ignore-reload
acl shoutcast rep_header X-HTTP09-First-Line ^ICY.
upgrade_http0.9 deny shoutcast
acl apache rep_header Server ^Apache
broken_vary_encoding allow apache
coredump_dir /usr/local/squid/var/cache
(这个可以参考同目录下的squid.conf.default)
三、启动squid
1.squid -k parse
看不到输出,配置文件有效,你能继续后面的步骤。然而,如果配置文件包含错误,squid会有提示
2.初始化cache目录.(对自己定义的cache目录,squid用户要有读写权限,系统要有squid用户)
squid -z
3.启动 (之前要对存放log的目录squid有读写权限,否则squid不会启动)
squid -s(调试可用 squid -Nd1)
基本配置完成
四、测试 http://www.xxx.com/index.jsp
把默认安装的两个tomcat中的index.jsp稍作不同,可以在访问时看到两个页面在轮循
squid的功能是委强大的 ,还有很多全命令,如下:
*取得squid运行状态信息: squidclient -p 80 mgr:info
*取得squid内存使用情况: squidclient -p 80 mgr:mem
*取得squid已经缓存的列表: squidclient -p 80 mgr:objects. use it carefully,it may crash
*取得squid的磁盘使用情况: squidclient -p 80 mgr:diskd
*强制更新某个url:squidclient -p 80 -m PURGE http://www.php-oa.com/static.php
*更多的请查看:squidclient -h 或者 squidclient -p 80 mgr:
页:
[1]