FreeBSD(squid缓存服务器配置篇)
squid缓存服务器在这个实验环境中有两台(192.168.10.30,192.168.10.31)做成squid集群,我们首先安装一台,然后复制一台,再做配置,这样省事,哈!
squid01服务器网络配置
# ee /etc/rc.conf
ifconfig_lnc0="inet 192.168.10.30 netmask 255.255.255.0"
defaultrouter="192.168.10.10"
hostname="squid01.rd.bj"
# ee /etc/hosts
192.168.10.30 squid01.rd.bj # ee /etc/resolv.conf
nameserver 192.168.10.40
网络配置好后,我们开始安装squid,先把的藏身处找到:
# whereis squid30
squid30: /usr/ports/www/squid30
# cd /usr/ports/www/squid30
# make config #配置安装选项
# make showconfig #我的配置选项
===> The following configuration options are available for squid-3.0.1:
SQUID_LDAP_AUTH=off "Install LDAP authentication helpers"
SQUID_SASL_AUTH=off "Install SASL authentication helpers"
SQUID_DELAY_POOLS=off "Enable delay pools"
SQUID_SNMP=on "Enable SNMP support"
SQUID_CARP=on "Enable CARP support"
SQUID_SSL=off "Enable SSL support for reverse proxies"
SQUID_PINGER=off "Install the icmp helper"
SQUID_DNS_HELPER=off "Use the old 'dnsserver' helper"
SQUID_HTCP=on "Enable HTCP support"
SQUID_VIA_DB=off "Enable forward/via database"
SQUID_CACHE_DIGESTS=on "Enable cache digests"
SQUID_WCCP=on "Enable Web Cache Coordination Prot. v1"
SQUID_WCCPV2=on "Enable Web Cache Coordination Prot. v2"
SQUID_STRICT_HTTP=on "Be strictly HTTP compliant"
SQUID_IDENT=on "Enable ident (RFC 931) lookups"
SQUID_REFERER_LOG=on "Enable Referer-header logging"
SQUID_USERAGENT_LOG=on "Enable User-Agent-header logging"
SQUID_ARP_ACL=off "Enable ACLs based on ethernet address"
SQUID_IPFW=off "Enable transparent proxying with IPFW"
SQUID_PF=off "Enable transparent proxying with PF"
SQUID_IPFILTER=off "Enable transp. proxying with IPFilter"
SQUID_ICAP=off "Enable ICAP client functionality"
SQUID_ESI=off "Enable ESI support (experimental)"
SQUID_AUFS=off "Enable the aufs storage scheme"
SQUID_COSS=off "Enable the COSS storage scheme"
SQUID_KQUEUE=on "Use kqueue(2) (experimental)"
SQUID_LARGEFILE=on "Support log and cache files >2GB"
SQUID_STACKTRACES=off "Create backtraces on fatal errors"
SQUID_DEBUG=off "Enable debugging options"
===> Use 'make config' to modify these settings
# make install clean
Squid 安装好了,现在开始配置她了。安装完后,在/usr/local/etc/squid下有squid的配置文件squid.conf和 squid.conf.default,这两个都是一样的,我们先删除掉squid.conf,在建个新的squid.conf,然后把我们的配置信息写 在里面,这样的话更加清晰一点,省得在原来的配置文件中改,里面内容那么多,改到后来,自己都不知道改了那些东西,糊涂了,如果对配置有什么不清楚的还是 可以参考squid.conf.default文件的。
# cd /usr/local/etc/squid
# rm -rf squid.conf
# touch squid.conf
# ee squid.conf
加入:
http_port 192.168.10.30:3128 vhost vport defaultsite=www.rd.bj
cache_mem 64 MB
cache_swap_low 90
cache_swap_high 95
maximum_object_size 20 MB
minimum_object_size 0 KB
maximum_object_size_in_memory 5 MB
ipcache_size 1024
ipcache_low 90
ipcache_high 95
fqdncache_size 1024
#acl all src 0.0.0.0/0.0.0.0 #好像squid30已经内置了
acl gsrc src 192.168.10.20 192.168.10.30 192.168.10.31 192.168.10.40
http_access allow gsrc
acl Manager proto cache_object
acl Localhost src 127.0.0.1
http_access allow Manager Localhost
acl Safe_ports port 3128 80 3130 3131
http_access deny !Safe_ports
http_access allow all
visible_hostname squid01.rd.bj
cache_mgr [email protected]
cache_effective_user squid
cache_effective_group squid
tcp_recv_bufsize 65536 bytes
cache_peer 192.168.10.31 sibling 3128 3131
cache_peer_access 192.168.10.31 allow all
cache_peer 192.168.10.40 parent 80 0 no-query originserver no-digest
access_log /usr/local/squid/logs/access.log
cache_dir diskd /usr/local/squid/cache 256 16 256 Q1=60 Q2=50
error_directory /usr/local/etc/squid/errors/Simplify_Chinese
icp_port 3130
htcp_port 0
icp_query_timeout 2000
icp_access allow all
digest_generation on
log_icp_queries on
icp_hit_stale on
保存退出后,我们验证一下配置文件是否正确:
# rehash
# squid parse
如果没有什么信息出现的话,恭喜你,配置无误!
OK,现在我们先根据配置文件创建缓存目录。
# squid -z
2007/12/27 12:43:55| Creating Swap Directories
2007/12/27 12:43:55| /usr/local/squid/cache exists
2007/12/27 12:43:55| Making directories in /usr/local/squid/cache/00
2007/12/27 12:43:55| Making directories in /usr/local/squid/cache/01
2007/12/27 12:43:55| Making directories in /usr/local/squid/cache/02
2007/12/27 12:43:55| Making directories in /usr/local/squid/cache/03
2007/12/27 12:43:56| Making directories in /usr/local/squid/cache/04
2007/12/27 12:43:56| Making directories in /usr/local/squid/cache/05
2007/12/27 12:43:57| Making directories in /usr/local/squid/cache/06
2007/12/27 12:43:58| Making directories in /usr/local/squid/cache/07
2007/12/27 12:43:58| Making directories in /usr/local/squid/cache/08
2007/12/27 12:43:59| Making directories in /usr/local/squid/cache/09
2007/12/27 12:43:59| Making directories in /usr/local/squid/cache/0A
2007/12/27 12:43:59| Making directories in /usr/local/squid/cache/0B
2007/12/27 12:43:59| Making directories in /usr/local/squid/cache/0C
2007/12/27 12:44:00| Making directories in /usr/local/squid/cache/0D
2007/12/27 12:44:00| Making directories in /usr/local/squid/cache/0E
2007/12/27 12:44:01| Making directories in /usr/local/squid/cache/0F
这样就创建完成了。
在这里我补充一点,如果创建缓存目录失败的,多半是目录读写权限的问题。因为我们是ports安装的,所以系统会跟我们预先创建好squid用户和用户 组。并把家目录指向/usr/local/squid。所以我们这里一般不会有问题。但是,如果大家是用下载的源码自行编译又想把缓存目录换到别的位置或 是用个新硬盘存放,那改怎么办呢?我慢慢跟你道来。
我们现在以新增一块硬盘为例进行讲解。
首先我们格式化新硬盘
可以利用sysinstal来完成,具体操作可以参考FressBSD安装的那个章节有介绍,这里不说了。格式化好硬盘后,因为我们是用来存放缓存文件的,所以我们对该块硬盘做点小优化。
# newfs -b 8192 -f 1024 /dev/ad1s1d
# tunefs -n enable /dev/ad1s1d # 启用Soft Updates,必需在硬盘没有被挂载的时候或在单用户模式下才可以应用
# mount /dev/ad1s1d /cache
# ee /etc/fstab # 加载硬盘
/dev/ad1s1d /cache ufs rw 2 2
补充小知识:
FreeBSD 系统在block size 为8K或16K时有最好的表现,而使用newfs 进行格式化时,预设的 block size 为16K。然而,当我们的服务器是做为数据库用途时,由于数据库的存取是随机存取,所以在 block size 为 8K 时会有比较好的表现。如果我们将block size设为 16K 以上,比较容易造成空间浪费及空间破碎的问题,进而造成效能低落。
当我们的分区主要存放大量小型文件时,我们会将block size 调小一点,如 8K 或是 4K。而在 newfs 时,我们也会指定fragment size,它的值最好是 block size 的八分之一,例如当block size 为8K 时,我们会使用newfs -b 8192 -f 1024 来格式化硬盘。如果您使用的是/stand/sysinstall 来格式化硬盘,您可以在格式化硬盘时按N来设置newfs option。
当我们的分区主要用来存放少量大文件时,例如数据库,我们可以使用 newfs 参数 -i 设定 inode 所占空间大一点以减少 inode 的数量 (也就是可以建立的文件及目录数量),让系统在不正常关机后,开机时进行 fsck 时可以快一点。不过在修改inode 数量时要注意,否则您可能会遇到硬盘空间明明未满却无法新增文件的窘境。若要使用大型的inode,FreeBSD建议的inode 大小为32768、65536、或 262144,再大的话只会降低效能。
新硬盘安装好了以后,我们现在来做配置:
# pw groupadd squid
# pw useradd squid -s /usr/sbin/nologin -d /nonexistent -g squid
# mkdir /cache/www.rd.bj #建立缓存目录
# chown -R squid:squid /cache/www.rd.bj
好了,现在只需要把squid.conf里的
cache_dir diskd /usr/local/squid/cache 256 16 256 Q1=60 Q2=50
改成
cache_dir diskd /cache/www.rd.bj 256 16 256 Q1=60 Q2=50
保存退出,在创建新的缓存目录
# squid -z
好了就补充到这里吧,我们继续我们的探索之旅。
现在squid的配置文件已经测试无误了,那我们还等什么,赶紧测试一下服务叻!
# squid -CDNd1
2007/12/27 14:01:56| Starting Squid Cache version 3.0.STABLE1 for i386-portbld-freebsd6.3...
2007/12/27 14:01:56| Process ID 810
2007/12/27 14:01:56| With 32768 file descriptors available
2007/12/27 14:01:56| DNS Socket created at 0.0.0.0, port 49839, FD 5
2007/12/27 14:01:56| Adding nameserver 192.168.10.40 from /etc/resolv.conf
2007/12/27 14:01:56| User-Agent logging is disabled.
2007/12/27 14:01:56| Referer logging is disabled.
2007/12/27 14:01:56| Unlinkd pipe opened on FD 9
2007/12/27 14:01:56| Local cache digest enabled; rebuild/rewrite every 3600/3600 sec
2007/12/27 14:01:56| Swap maxSize 262144 KB, estimated 20164 objects
2007/12/27 14:01:56| Target number of buckets: 1008
2007/12/27 14:01:56| Using 8192 Store buckets
2007/12/27 14:01:56| Max Mem size: 8192 KB
2007/12/27 14:01:56| Max Swap size: 262144 KB
2007/12/27 14:01:57| Rebuilding storage in /usr/local/squid/cache (DIRTY)
2007/12/27 14:01:57| Using Least Load store dir selection
2007/12/27 14:01:57| Current Directory is /root
2007/12/27 14:01:57| Loaded Icons.
2007/12/27 14:01:57| Accepting accelerated HTTP connections at 192.168.10.30, port 3128, FD 11.
2007/12/27 14:01:57| Accepting ICP messages at 0.0.0.0, port 3130, FD 12.
2007/12/27 14:01:57| HTCP Disabled.
2007/12/27 14:01:57| Configuring Sibling 192.168.10.31/3128/3131
2007/12/27 14:01:57| Configuring Parent 192.168.10.40/80/0
2007/12/27 14:01:57| Ready to serve requests.
2007/12/27 14:02:04| Done scanning /usr/local/squid/cache swaplog (0 entries)
2007/12/27 14:02:04| Finished rebuilding storage from disk.
2007/12/27 14:02:04| 0 Entries scanned
2007/12/27 14:02:04| 0 Invalid entries.
2007/12/27 14:02:04| 0 With invalid flags.
2007/12/27 14:02:04| 0 Objects loaded.
2007/12/27 14:02:04| 0 Objects expired.
2007/12/27 14:02:04| 0 Objects cancelled.
2007/12/27 14:02:04| 0 Duplicate URLs purged.
2007/12/27 14:02:04| 0 Swapfile clashes avoided.
2007/12/27 14:02:04| Took 6.97 seconds ( 0.00 objects/sec).
2007/12/27 14:02:04| Beginning Validation Procedure
2007/12/27 14:02:04| Completed Validation Procedure
2007/12/27 14:02:04| Validated 25 Entries
2007/12/27 14:02:04| store_swap_size = 0
2007/12/27 14:02:04| storeLateRelease: released 0 objects
启动没有什么问题,来我们用浏览器访问一下吧!
看到这样的页面说明Squid服务器是正常的,之所以出现错误页面,那是因为我们那台提供web服务的虚拟机没有启动的原因。
正式启动可以使用:
# squid -D
OK,我们的Squid01的配置就到这里, 现在我们关闭虚拟机,然后拷贝一份squid01为squid02,用来配置另一台Squid服务器。
复制好squid02后,我们现在开始配置了, 应为squid01的大部分工作都已经做好了, 所以squid02的配置就比较方便了,只要是配置文件中IP地址和机器名的修改。
服务器网络配置
# ee /etc/rc.conf
ifconfig_lnc0="inet 192.168.10.31 netmask 255.255.255.0"
defaultrouter="192.168.10.10"
hostname="squid02.rd.bj"
# ee /etc/hosts
192.168.10.31 squid02.rd.bj
# ee /usr/local/etc/squid/squid.conf
最后修改好的配置文件为:
http_port 192.168.10.31:3128 vhost vport defaultsite=www.rd.bj
cache_mem 64 MB
cache_swap_low 90
cache_swap_high 95
maximum_object_size 20 MB
minimum_object_size 0 KB
maximum_object_size_in_memory 5 MB
ipcache_size 1024
ipcache_low 90
ipcache_high 95
fqdncache_size 1024
#acl all src 0.0.0.0/0.0.0.0
acl gsrc src 192.168.10.20 192.168.10.30 192.168.10.31 192.168.10.40
http_access allow gsrc
acl Manager proto cache_object
acl Localhost src 127.0.0.1
http_access allow Manager Localhost
acl Safe_ports port 3128 80 3130 3131
http_access deny !Safe_ports
http_access allow all
visible_hostname squid01.rd.bj
cache_mgr [email protected]
cache_effective_user squid
cache_effective_group squid
tcp_recv_bufsize 65536 bytes
cache_peer 192.168.10.30 sibling 3128 3130
cache_peer_access 192.168.10.30 allow all
cache_peer 192.168.10.40 parent 80 0 no-query originserver no-digest
access_log /usr/local/squid/logs/access.log
cache_dir diskd /usr/local/squid/cache 256 16 256 Q1=60 Q2=50
error_directory /usr/local/etc/squid/errors/Simplify_Chinese
icp_port 3131
htcp_port 0
icp_query_timeout 2000
icp_access allow all
digest_generation on
log_icp_queries on
icp_hit_stale on
保存退出后,按照squid01的测试方法,对squid02进行测试,如果结果和squid01一样,那我们的squid缓存服务器也就暂告一段落了。