squid安装详解

解压安装

使用下面的命令,把squid安装到/usr/local/squid目录中去。

./configure --prefix=/usr/local/squid--enable-follow-x-forwarded-for --enable-storeio=aufs,ufs--with-maxfd=65536 --with-pthreads --enable-dlmalloc --enable-poll--enable-stacktraces --enable-removal-policies=heap,lru--enable-delay-pools

对应参数的作用能够使用./configure--help得到。
这里大家要注意下,configure前面更有一个./,这个代表当前目录。意思是当前目录下面的configure文档。linux假如要执行当前目录的文档,必须要加上./。假如要执行当前目录下的下级目录里面的文档,能够不用加,比如bin/run。当然,./bin/run也是相同的效果。
接着我们会看到屏幕向下翻滚,出现一大堆checkingfor…的字样。一直等到下面的文字出现,并且停止,那么configure就算完成了。

config.status: creating tools/Makefile
config.status: creating include/autoconf.h
config.status: executing depfiles commands
[root@cnc squid-2.6.STABLE13]#

然后,我们输入make编译源代码,又是一行行的英文翻滚。是否有点感觉做黑客的味道?我们要做的还是继续等待下面的文字出现

make[2]: Leaving directory`/root/squid-2.6.STABLE13/tools'
make[1]: Leaving directory `/root/squid-2.6.STABLE13/tools'
make[1]: Entering directory `/root/squid-2.6.STABLE13'
make[1]: Nothing to be done for `all-am'.
make[1]: Leaving directory `/root/squid-2.6.STABLE13'
[root@cnc squid-2.6.STABLE13]#

这样源代码就算编译完成了。然后我们运行makeinstall安装,等待下面的提示

make[2]: Leaving directory `/root/squid-2.6.STABLE13'
make[1]: Leaving directory `/root/squid-2.6.STABLE13'
[root@cnc squid-2.6.STABLE13]#

这里要注意:假如任何一个步骤出现带有error之类的警告,那么就是编译出错,需要检查到底是什么步骤出现问题并且改正。假如严格按照本文来编译,一般是不会出现错误的。另外,cflags参数使用不当同样会造成编译错误。
假如出现编译错误,必须要先makeclean,然后重新make。(假如错误是在configure这步就无需了)

到这里,squid就算是编译安装完毕了。我们能够到/usr/local/squid目录看看,里面已躺着一大堆文档了。
[root@cnc squid-2.6.STABLE13]# cd /usr/local/squid
[root@cnc squid]# ls -lh
total 72K
drwxr-xr-x 2 root root 4.0K Jul 7 02:27 bin
drwxr-xr-x 2 root root 4.0K Jul 7 02:27 etc
drwxr-xr-x 2 root root 4.0K Jul 7 02:27 libexec
drwx------ 2 root root 16K Jun 15 08:09 lost+found
drwxr-xr-x 3 root root 4.0K Jul 7 02:27 man
drwxr-xr-x 2 root root 4.0K Jul 7 02:25 sbin
drwxr-xr-x 4 root root 4.0K Jul 7 02:25 share
drwxr-xr-x 3 root root 4.0K Jul 7 02:25 var
[root@cnc squid]#

1.配置hosts表
首先我们要明白一下squid在CDN中扮演的角色。squid在CDN中其实只是个代理+缓存。跟一般的代理服务器类似,squid代替用户向真正有内容的服务器进行请求,并且缓存下来。所不同的是,squid是作为一个类似apache相同的web服务器存在,用户访问的时候并不感觉到是访问了一台代理服务器,而是个真实的web服务器。一般情况下,我们把这种方式称之为“反向代理”。
所以,我们需要让squid知道内容要到哪里去取。因为我们已把域名naizhao.com配置了智能DNS,而且这台squid服务器也是网通的,按照正常的dns解析,会把naizhao.com解析为2.2.2.2这个IP(也就是网通机器本身)。但这个IP上面并不存在任何所需要的内容,任何需要的内容其实是在1.1.1.1上(源服务器)。所以我们需要为naizhao.com固定一个IP,也就是1.1.1.1。
让系统得到这个IP的方法有两种,一种是配置hosts表,另外一种是系统里面搭建一个dns服务器。Windows下大家应该会经常去编辑到hosts文档,linux下同样也存在这个文档,我们在前面的vim教程中已提到过。搭建dns系统这个方法工程太过庞大,还是编辑hosts文档相对来说比较简单,所以我们就采用编辑hosts表的方法。
linux下面的hosts表位于/etc/hosts,格式跟Windows大致相同,打开后我们把下面一行加到末尾

1.1.1.1        naizhao.com www.naizhao.com cnc.naizhao.com

假如已按照前面的教程,添加过1.1.1.1这条记录的,修改为上面的样子。
2.配置squid
squid的配置文档位于/usr/local/squid/etc/squid.conf,打开后会有英文的周详说明。暂时我们无需去理解这个文档里面各个配置的含义,相信大家看到一大堆英文都会头大的。大家只需要知道如何把squid按照自己的需求跑起来,并且不出错就好。
我们先用rm命令把squid.conf删掉。不用怕删掉后找不回来,更有一个默认的squid.conf.default在呢。一般的linux程式,包括apache等,都会有一个.default的默认文档,方便大家恢复默认配置,或作为一个修改的参考。
squid.conf删掉后,我们再用visquid.conf,新建这个文档,然后把下面的内容帖进去,保存
http_port 80 vhost vport=80
acl apache rep_header Server ^Apache
broken_vary_encoding allow apache
cache_mem 1024 MB
maximum_object_size 4096 KB
minimum_object_size 0 KB
maximum_object_size_in_memory 256 KB
cache_dir ufs /var/spool/squid 20480 16 256
cache_swap_low 80
cache_swap_high 97
strip_query_terms off
request_body_max_size 5 MB
memory_pools on
memory_pools_limit 50 MB
access_log none
cache_log /var/log/squid/cache.log
cache_store_log none
pid_filename /var/run/squid.pid
hosts_file /etc/hosts
refresh_pattern^ftp:          1440   20%    10080
refresh_pattern^gopher:       1440   0%     1440
refresh_pattern -iaaa\.com         240    100%   1440   ignore-reload
refresh_pattern -iwww\.aaa\.com          240    100%   1440   ignore-reload

refresh_pattern -ibbb\.com         240    100%   1440   ignore-reload
refresh_pattern -iwww\.bbb\.com          240    100%   1440   ignore-reload
refresh_pattern -i .gif 180 20% 10080 override-expire ignore-reloadreload-into-ims
refresh_pattern -i .jpg 180 20% 10080 override-expire ignore-reloadreload-into-ims
refresh_pattern.                          120 50%   1440
acl all src 0.0.0.0/0.0.0.0
acl p-manager proto cache_object
acl s-localhost src 127.0.0.1/255.255.255.255
acl d-localhost dst 127.0.0.0/8
acl d-domains dstdomain .naizhao.com
acl p-ssl port 443 563
acl p-safe port 80 443 563
acl m-conn method CONNECT
acl m-purge method PURGE
acl n-maxconn maxconn 15
http_access allow all

http_access allow p-manager s-localhost
http_access allow m-purge
http_access deny !p-safe
http_access deny m-conn !p-ssl
http_access deny n-maxconn
http_access allow p-manager
http_access allow d-domains
http_reply_access allow all
acl r-url urlpath_regex realtime
cache deny r-url
icp_access allow all
follow_x_forwarded_for allow all
acl_uses_indirect_client offrange_offset_limit -1
dns_timeout 2 seconds
forward_timeout 10 seconds
connect_timeout 10 seconds
peer_connect_timeout 6 seconds
read_timeout 10 seconds
request_timeout 6 seconds
persistent_request_timeout 16 seconds
cache_mgr silence.z@gmail.com
cache_effective_user nobody
cache_effective_group nobody
visible_hostname cnc.naizhao.com
logfile_rotate 0
always_direct allow all
error_directory/usr/local/squid/share/errors/Simplify_Chinese
coredump_dir /var/spool/squid


需要说明的几个参数:
cache_mem 1024 MB(squid使用的内存大小,注意必须要留下500M左右的内存给系统使用,建议系统内存2G,然后分配1G给squid)

cache_dir ufs /var/spool/squid 20480 16 256(缓存的目录,放在/var/spool/squid下面,大小是20G,第一级目录是16个,第二级目录是256个。因为一个目录下面放置的文档和目录数量有限制,所以这里必须要配置多个目录,以适合squid存放大量文档的需要。目录数千万别配置太大,否则会引起系统死机)

memory_pools_limit 50 MB(内存池,用来存放缓存文档的链表,方便高速查找文档存放的对应位置,无需太大。不懂就别管了)

cache_log/var/log/squid/cache.log(log文档的存放位置,必须要有读写权限,稍后说到)

refresh_pattern -inaizhao\.com         240    100%   1440   ignore-reload
refresh_pattern -iwww\.naizhao\.com          240    100%   1440   ignore-reload
refresh_pattern -i .gif 180 20% 10080 override-expire ignore-reloadreload-into-ims
refresh_pattern -i .jpg 180 20% 10080 override-expire ignore-reloadreload-into-ims
(上面的几行指定了文档缓存的时间,能够指定某个域名,也能够指定某个文档类型。有更加多的域名或文档类型就按照需要添加吧。-i是忽略大小写,naizhao.com和.gif是文档类型,240是最小缓存的时间,1440是最大缓存时间,都是分钟。这些先不用管,有需要的时候调整就好了。我们的口号是:先跑起来)

acl d-domains dstdomain.naizhao.com(允许访问的域名列表。假如要跑多个网站就添加多几个,用空格分开。naizhao.com前面有个英文的.,表示任何*.naizhao.com都允许。假如没有这个点,那么就只允许naizhao.com,访问www.naizhao.com就会提示访问被拒绝。)

cache_mgr silence.z@gmail.com(管理员的邮件地址,改为自己的)

cache_effective_user nobody
cache_effective_group nobody
(用来跑squid的用户和用户组。为了避免使用root帐号造成的不安全因素,大部分linux程式都会先用root运行,然后马上把运行的用户从root转换到其他用户,这样就算程式有漏洞被黑了,黑客也只能得到这个普通用户的权限,对系统的危害将大大降低。保持默认即可)

visible_hostname cnc.naizhao.com(服务器输出的机器名字,多台squid分布的时候这个很重要,通过配置多个不同的名字,很容易就能够找出哪台服务器有问题)

coredump_dir /var/spool/squid(squid挂掉后,临终遗言要放到哪里。不用管,一般人看不懂)

修改好配置文档后,我们还需要做一些最后的工作。

具体参数根据自己的服务器来配置

修改/etc/hosts  加入对应的ip和域名


先建一个log目录,用来存放log文档。按照squid.conf的配置,我们输入下面的命令

mkdir /var/log/squid
chown nobody:nobody /var/log/squid

chown命令是配置任何者和组。刚才说了,squid将会使用用户nobody组nobody来运行。nobody:nobody冒号前面的是用户,后面的是用户组。
更有squid的缓存目录,同样需要配置权限为nobody:nobody

chown nobody:nobody /var/spool/squid

查看命令是否成功,能够使用ls -lh

ls -lh /var/spool/
……
drwxr-xr-x 3 nobody nobody 4.0K Jun 15 08:19 squid

剩下最后一步,初始化squid的缓存目录结构。(也就是刚才说的16个一级目录,256个二级目录。本身这些文档夹不存在,需要通过squid的命令建立)

squid的命令在/usr/local/squid/sbin下,我们进入这个目录,然后看看squid命令的帮助

./squid -h
Usage: squid [-hvzCDFNRYX] [-d level] [-s | -l facility] [-fconfig-file] [-u port] [-k signal]
      -d level Write debugging to stderr also.
      -f file   Use given config-fileinstead of
                /usr/local/squid/etc/squid.conf
      -h       Print help message.
      -kreconfigure|rotate|shutdown|interrupt|kill|debug|check|parse
                Parse configuration file, then send signal to
                running copy (except -k parse) and exit.
      -s | -l facility
                Enable logging to syslog.
      -u port   Specify ICP port number(default: 3130), disable with 0.
      -v       Print version.
      -z       Create swap directories
      -C       Do not catch fatal signals.
      -D       Disable initial DNS tests.
      -F       Don't serve any requests until store is rebuilt.
      -N       No daemon mode.
      -R       Do not set REUSEADDR on port.
      -S       Double-check swap during rebuild.
      -X       Force full debugging.
      -Y       Only return UDP_HIT or UDP_MISS_NOFETCH during fast reload.

我们能够看到-z命令就是建立缓存目录的,所以我们输入

./squid -z
2007/07/07 07:52:03| Creating Swap Directories

没有任何错误提示,说明目录已建立成功了,我们能够用ls /var/spool/squid看看效果。

最后,我们让squid跑起来

./squid -Nd 10

2007/07/07 07:53:50| Starting Squid Cache version 2.6.STABLE13for i686-pc-linux-gnu...
2007/07/07 07:53:50| Process ID 3135
2007/07/07 07:53:50| With 1024 file descriptors available
2007/07/07 07:53:50| Using poll for the IO loop
2007/07/07 07:53:50| Performing DNS Tests...
2007/07/07 07:53:50| Successful DNS name lookup tests...
2007/07/07 07:53:50| DNS Socket created at 0.0.0.0, port 1026, FD4
2007/07/07 07:53:50| Adding nameserver 211.147.6.3 from/etc/resolv.conf
2007/07/07 07:53:50| Adding nameserver 61.233.9.9 from/etc/resolv.conf
2007/07/07 07:53:50| Unlinkd pipe opened on FD 8
2007/07/07 07:53:50| Swap maxSize 20971520 KB, estimated 1613193objects
2007/07/07 07:53:50| Target number of buckets: 80659
2007/07/07 07:53:50| Using 131072 Store buckets
2007/07/07 07:53:50| Max Mem size: 1048576 KB
2007/07/07 07:53:50| Max Swap size: 20971520 KB
2007/07/07 07:53:50| Store logging disabled
2007/07/07 07:53:50| Rebuilding storage in /var/spool/squid(DIRTY)
2007/07/07 07:53:50| Using Least Load store dir selection
2007/07/07 07:53:50| Set Current Directory to/var/spool/squid
2007/07/07 07:53:50| Loaded Icons.
2007/07/07 07:53:52| Accepting accelerated HTTP connections at0.0.0.0, port 80, FD 7.
2007/07/07 07:53:52| Accepting ICP messages at 0.0.0.0, port 3130,FD 9.
2007/07/07 07:53:52| WCCP Disabled.
2007/07/07 07:53:52| Ready to serve requests.
2007/07/07 07:53:52| Done scanning /var/spool/squid (0entries)
2007/07/07 07:53:52| Finished rebuilding storage from disk.
2007/07/0707:53:52|        0 Entries scanned
2007/07/0707:53:52|        0 Invalid entries.
2007/07/0707:53:52|        0 With invalid flags.
2007/07/0707:53:52|        0 Objects loaded.
2007/07/0707:53:52|        0 Objects expired.
2007/07/0707:53:52|        0 Objects cancelled.
2007/07/0707:53:52|        0 Duplicate URLs purged.
2007/07/0707:53:52|        0 Swapfile clashes avoided.
2007/07/07 07:53:52|   Took 2.0seconds (   0.0objects/sec).
2007/07/07 07:53:52| Beginning Validation Procedure
2007/07/07 07:53:52|   CompletedValidation Procedure
2007/07/07 07:53:52|   Validated0 Entries
2007/07/07 07:53:52|  store_swap_size = 0k
2007/07/07 07:53:53| storeLateRelease: released 0 objects

打开浏览器,输入http://2.2.2.2,会看到AccessDenied的错误提示,说明squid已正常跑起来了。

因为指定了-d参数,所以现在squid会把任何的调试信息都输出终端。按下键盘的ctrl+c,然后输入killallsquid杀死squid,能够终止squid的进程。

------------------

/usr/local/squid/sbin/squid -z 建立缓存目录

/usr/local/squid/sbin/squid -DNsY & 启动

./configure --prefix=/usr/local/squid \
--sysconfdir=/etc/squid --enable-arp-acl --enable-cache-digests--enable-err-language="Traditional_Chinese"--enable-default-err-language="Traditional_Chinese"--enable-kill-parent-hack --enable-linux-netfilter --enable-snmp--enable-underscores --enable-gnuregex --enable-async-io=80--enable-icmp --enable-poll --disable-ident-lookups


apt-get install build-essential


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值