squid完全攻略(一)squid优化后详细安装步骤
2014-07-08 18:11:09 点击:

Squid工作原理Squid是Linux下一个缓存Internet数据的代理服务器软件,它接收用户的下载申请并自动处理所下载的数据。即当一个用户下载www.i...
Squid工作原理
Squid是Linux下一个缓存Internet数据的代理服务器软件,它接收用户的下载申请并自动处理所下载的数据。即当一个用户下载www.idcshare的页面,他请求squid为他取得这个页面,suqid会连接到申请www.idcshare的网站并请求该主页,接着把主页传给用户并自己保留一份.当下一次有其他用户请求同样页面时,squid会从磁盘中读到保存那份,这样数据就会迅速传输到用户的机器上.
 
Squid支持以下协议
1) 客户端协议:HTTP .FTP.Gopher.WAIS.SSL
2) 缓存及管理协议:ICP(Internet Cache协议). HTCP(超文本缓冲协议).SNNP. Cache Digests
 
运行流程图:

1) Client端向Server端发送一个资料需求封包;
2) Server端接收之后,先比对这个封包的『来源』与预计要前往的『目标』网站是否为可接受?如果来源与目标都是合法的,或者说,来源与目标网站我们的Proxy都能帮忙取得资料时,那么Server端会预计开始替Client取得资料。
3) Server首先会到自己的硬碟里面,也就是所谓的cache 查看一下有没有Client端所需要的资料,如果有的话,那就将资料直接送到Client端而不经过向Internet要求资料的程序;
4) 将数据传回给客户端使用
5)在经过1, 2, 3查寻知道cache没有资料,或者资料过期之后, Proxy会向Internet上面的目标网站要求资料;
6)在将资料取回之后, proxy会先将取得的资料『储存一份到cache当中』;
7)最后才将资料传回给Client端使用。
 
代理服务器优点与类型
优点:
1)       通过缓存增加访问速度
2)       提供用私有ip访问internet的方法
3)       提高网络的安全性
4)       方便对用户的管理
类型:
1) 标准代理服务器

 

2) 透明代理服务器

 

3) 反向代理服务器

 

安装前期准备工作
1)源代码编译优化
一般情况下,优化参数都是通过export命令设定CFLAGSCXXFLAGS,然后configure的时候会自动读入,make的时候会自动使用所选的优化参数。通过添加优化参数,让程序针对某个CPU型号和某个系统进行最大优化,减少文件大小、CPU使用率、内存使用率。优化后的程序,将不再具有跨系统跨平台的能力。
首先查看CPU信息,然后根据CPU信息到http://gentoo-wiki.com/Safe_Cflags去获取CPU优化参数
技巧:直接是打不开http://gentoo-wiki.com/Safe_Cflagshttp://www.proxyie.cn/输入http://gentoo-wiki.com,打开后点左侧栏的搜索,输入cpu,要翻墙,打开
 
 
[root@server soft]# cat /proc/cpuinfo |more 
processor       : 0
vendor_id       : GenuineIntel
cpu family      : 6
model           : 23
model name      : Intel(R) Xeon(R) CPU           E5420 @ 2.50GHz
stepping        : 10
cpu MHz         : 2493.978
cache size      : 6144 KB
physical id     : 0
siblings        : 4
core id         : 0
cpu cores       : 4
apicid          : 0
fdiv_bug        : no
hlt_bug         : no
f00f_bug        : no
coma_bug        : no
fpu             : yes
fpu_exception   : yes
cpuid level     : 13
wp              : yes
flags           : fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush d
ts acpi mmx fxsr sse sse2 ss ht tm pbe nx lm constant_tsc pni monitor ds_cpl vmx est tm2 cx16 xtpr
lahf_lm
bogomips        : 4987.95
 
 
配置:
[root@server soft]#export CHOST="i686-pc-linux-gnu"
[root@server soft]#export CFLAGS="-march=Prescott -O2 -pipe -fomit-frame-pointer"
[root@server soft]#export CXXFLAGS="${CFLAGS}"
[root@server soft]#env

查看设置环境变量是否生效
红色标记,说明生效了.
 
3)修改描述符参数值
文件描述符用来限制一个程序能同时打开的文件数目,默认是1024。也就是说,如果不修改文件描述符,那么一个程序只能同时打开1024个文件,而Squid会同时打开成千上万个文件,所以远远是不够的,为了保证最高的效率和响应速度,要更改文件描述符的大小.
两种情况:a.用户每次访问squidsquid会按照需要打开文件,然后读取文件内容再返回给用户。
b.squid预先打开访问频率高的文件,用户访问squidsquid直接把内容返回给用户。相对来说,后面的方法可以对用户的请求进行更加快的反应。
首先修改以下两个文件。
/usr/include/bits/typesizes.h
/usr/include/linux/posix_types.h

vi打开上面的文件查找
#define __FD_SETSIZE 1024
1024改为65536,然后保存,这是Linux能接受的最大数值。

刚才编辑的这两个文件是C/C++程序里面的头文件,编译squid的时候会被自动引用。除了这两个文件以外,我们还需要对当前环境进行设置。也就是你用ssh登录到系统时的一些设定。每个登录进程都可以进行单独的设置,当关闭目前登录回话,环境变量的设置就失效了.
ulimit用来设置当前环境的一些资源限制。这个命令是对环境进行设置,所以退出当前登录进程后命令将会失效。
我们输入下面的命令
[root@server soft]#ulimit -Hs 65536
[root@server soft]#ulimit -n 65536
也可以永久修改这些限制,开机自动生效,配置如下:
vi /etc/security/limits.conf,添加以下内容
*                soft   nofile          65536 
*                hard   nofile          65536
H参数是硬性限制,s是堆栈上限,n是文件描述符上限。
注释:
ulimit 参数介绍:
-H 设置硬件资源限制.
-S 设置软件资源限制.
-a 显示当前所有的资源限制.
-c size:设置core文件的最大值.单位:blocks
-d size:设置数据段的最大值.单位:kbytes
-f size:设置创建文件的最大值.单位:blocks
-l size:设置在内存中锁定进程的最大值.单位:kbytes
-m size:设置可以使用的常驻内存的最大值.单位:kbytes
-n size:设置内核可以同时打开的文件描述符的最大值.单位:n
-p size:设置管道缓冲区的最大值.单位:kbytes
-s size:设置堆栈的最大值.单位:kbytes
-t size:设置CPU使用时间的最大上限.单位:seconds
-v size:设置虚拟内存的最大值.单位:kbytes
安装与配置
安装
[root@server soft]# wget http://www.squid-cache.org/Versions/v3/3.0/squid-3.0.STABLE24.tar.gz
[root@server soft]# tar -zxvf squid-3.0.STABLE24.tar.gz
[root@server soft]# cd squid-3.0.STABLE24
[root@serversquid-3.0.STABLE24]#./configure --prefix=/usr/local/squid\--enable-gnuregex\--enable-icmp\--enable-linux-netfilter\--enable-default-err-language="Simplify_Chinese"\--enable-kill-parent-hack\--enable-cache-digests\--enable-dlmalloc\--enable-poll\--enable-async-io=240\--enable-delay-pools\--with-filedescriptors=65536\--enable-snmp\--enable-arp-acl\--mandir=/usr/share/man/--with-large-files
 
注释
可以用命令查看
[root@viong squid-3.0.STABLE24]# ./configure --help
--enable-gnuregex
在访问控制列表和其他配置指令里,squid使用正则表达式作为匹配机制。GNU的正则表达式库包含在squid的源代码包里;它可以在没有内建正则表达式的操作系统中使用。./configure脚本侦察你系统中的正则表达式库,假如必要,它可以激活使用GNU正则表达式。如果因为某些理由,你想强制使用GNU正则表达式,比如在proxy未来的规划当中。可能利用到正则表示法的方式来抵挡一些恶意的网站,你可以将这个选项加到./configure命令后.
--enable-icmp
squid能利用ICMP消息来确定回环时间尺寸,非常象ping程序。你能使用该选项来激活这些功能。
--enable-linux-netfilter
Netfilter是linux 2.4系列内核的包过滤器名字。假如你想在linux2.4或以后的版本中使用HTTP拦截功能,那么激活该选项。
--enable-default-err-language=lang
该选项设置error_directory指令的默认值。例如,假如你想使用荷兰语,你能这样指定:
% ./configure --enable-default-err-language=Dutch
你也能在squid.conf里指定error_directory指令,在附录A中有描述。假如你忽略该选项,英语是默认错误语言。
--enable-kill-parent-hack
在我们关掉squid的时候,连同parent process 一起关掉
--enable-cache-digests
Cache消化是ICP的另一个替代,但有着截然不同的特性
--enable-dlmalloc[=LIB]
在一些系统上,内建的内存分配机制(malloc)在使用squid时表现不尽人意。使用--enable-dlmalloc选项将squid源代码包中的dlmalloc包编译和链接进来。假如你的系统中已安装dlmalloc,你能使用=LIB参数指定库的路径。
--enable-poll
unix提供两个相似的函数用以在I/O事件里扫描开放文件描述符:select()和poll()../configure脚本通常能非常好的计算出何时使用poll()来代替select().假如你想强制使用poll(),那么指定该选项。
 
--enable-async-io[=N_THREADS]
异步I/O是squid技术之一,用以提升存储性能。aufs模块使用大量的线程来执行磁盘I/O操作。该代码仅仅工作在linux和solaris系统中。=N_THREADS参数改变squid使用的线程数量。如果网站的服务器配置很高,可以尝试将这个数字改为160以上。如果是小网站的话,可以考虑降低到40左右.
请注意--enable-async-io是打开其他三个./configure选项的快捷方式,它等同于:
--with-aufs-threads=N_THREADS
--with-pthreads
--enable-storeio=ufs,aufs
 
--enable-delay-pools
延时池是squid用于传输形状或带宽限制的技术。该池由大量的客户端IP地址组成。当来自这些客户端的请求处于cache丢失状态,他们的响应可能被人工
 --with-filedescriptors=65536
支持最大文件描述符
--enable-snmp
简单网络管理协议(SNMP)是监视网络设备和服务器的流行方法。该选项导致编译过程去编译所有的SNMP相关的代码,包括一个裁切版本的CMU SNMP库。
--enable-arp-acl
允许基于MAC地址的存取过滤
--mandir=/usr/share/man
指定man的安装目录
--with-large-files
让log支持大于2G
 
[root@server squid-3.0.STABLE24]# make; make install
make[3]: Leaving directory `/root/soft/squid-3.0.STABLE24/tools'
make[2]: Leaving directory `/root/soft/squid-3.0.STABLE24/tools'
make[1]: Leaving directory `/root/soft/squid-3.0.STABLE24/tools'
make[1]: Entering directory `/root/soft/squid-3.0.STABLE24'
Build Successful.
make[2]: Entering directory `/root/soft/squid-3.0.STABLE24'
make[2]: Nothing to be done for `install-exec-am'.==================说明安装成功
make[2]: Nothing to be done for `install-data-am'.
make[2]: Leaving directory `/root/soft/squid-3.0.STABLE24'
make[1]: Leaving directory `/root/soft/squid-3.0.STABLE24'
[root@server squid-3.0.STABLE24]#        
[root@server squid-3.0.STABLE24]# ll /usr/local/squid--enable-gnuregex/
总计 24
drwxr-xr-x 2 root root 4096 03-04 23:35 bin
drwxr-xr-x 2 root root 4096 03-04 23:35 etc
drwxr-xr-x 2 root root 4096 03-04 23:35 libexec
drwxr-xr-x 2 root root 4096 03-04 23:35 sbin
drwxr-xr-x 5 root root 4096 03-04 23:35 share
drwxr-xr-x 3 root root 4096 03-04 23:35 var
bin/ :放置主要的squid执行scripts的目录,重要的是RunCache那个档案;
etc/ :几乎所有的squid设定档都在这里;
libexec/ :一些函式库;
sbin/ :重要的就是那个squid的执行档!
share/ :一些错误讯息代码表示档案,以及一些小图示放置的目录;
var/ :预设是放置log file的,不过我不喜欢放在这里,这点等一下我们会修改的!
第一次运行squid时,要先设置/usr/local/squid/var权限,还有在/usr/local/squid/etc/squid.conf添加主机,visible_hostnam viong 并制定http_port 端口号.这里预设个80
 
[root@server squid-3.0.STABLE24]# chown -R nobody:nobody /usr/local/squid/var
 
[root@server squid-3.0.STABLE24]# /usr/local/squid/sbin/squid –zD
首次运行squid需要初始化cache,由于squid要验证其DNS可用才能启动,目前都没配置,所以我先禁用初始化DNS测试,先让squid启动
Squid[root@viong soft]# netstat -ntpl |grep 80
tcp        0      0 0.0.0.0:80                  0.0.0.0:*                   LISTEN    
[root@server squid-3.0.STABLE24]# /usr/local/squid/bin/squidclient -p 80 -h localhost mgr:info
取得squid运行状态信息
HTTP/1.0 200 OK
Server: squid/3.0.STABLE24
Mime-Version: 1.0
Date: Fri, 05 Mar 2010 14:43:26 GMT
Content-Type: text/plain
Expires: Fri, 05 Mar 2010 14:43:26 GMT
Last-Modified: Fri, 05 Mar 2010 14:43:26 GMT
X-Cache: MISS from viong
Via: 1.0 viong (squid/3.0.STABLE24)
Proxy-Connection: close
 
Squid Object Cache: Version 3.0.STABLE24
Start Time:     Fri, 05 Mar 2010 14:42:48 GMT
Current Time:   Fri, 05 Mar 2010 14:43:26 GMT
Connection information for squid:
        Number of clients accessing cache:      1
        Number of HTTP requests received:       0
        Number of ICP messages received:        0
        Number of ICP messages sent:    0
        Number of queued ICP replies:   0
        Number of HTCP messages received:       0
        Number of HTCP messages sent:   0
        Request failure ratio:   0.00
        Average HTTP requests per minute since start:   0.0
        Average ICP messages per minute since start:    0.0
        Select loop called: 8659 times, 4.376 ms avg
Cache information for squid:
        Hits as % of all requests:      5min: 0.0%, 60min: 0.0%
        Hits as % of bytes sent:        5min: -0.0%, 60min: -0.0%
        Memory hits as % of hit requests:       5min: 0.0%, 60min: 0.0%
        Disk hits as % of hit requests: 5min: 0.0%, 60min: 0.0%
        Storage Swap size:      0 KB
        Storage Swap capacity:   0.0% used, 100.0% free
        Storage Mem size:       104 KB
        Storage Mem capacity:    1.3% used, 98.7% free
        Mean Object Size:       0.00 KB
        Requests given to unlinkd:      0
Median Service Times (seconds) 5 min    60 min:
        HTTP Requests (All):   0.00000 0.00000
        Cache Misses:          0.00000 0.00000
        Cache Hits:            0.00000 0.00000
        Near Hits:             0.00000 0.00000
        Not-Modified Replies: 0.00000 0.00000
        DNS Lookups:           0.00000 0.00000
        ICP Queries:           0.00000 0.00000
Resource usage for squid:
        UP Time:        37.890 seconds
        CPU Time:       0.070 seconds
        CPU Usage:      0.18%
        CPU Usage, 5 minute avg:        0.00%
        CPU Usage, 60 minute avg:       0.00%
        Process Data Segment Size via sbrk(): 2784 KB
        Maximum Resident Size: 0 KB
        Page faults with physical i/o: 0
Memory usage for squid via mallinfo():
        Total space in arena:    3052 KB
        Ordinary blocks:         2938 KB      1 blks
        Small blocks:               0 KB      0 blks
        Holding blocks:          1520 KB      8 blks
        Free Small blocks:          0 KB
        Free Ordinary blocks:     113 KB
        Total in use:            4458 KB 98%
        Total free:               113 KB 2%
        Total size:              4572 KB
Memory accounted for:
        Total accounted:         1886 KB 41%
        memPool accounted:       1886 KB 41%
        memPool unaccounted:     2685 KB 59%
        memPoolAlloc calls:      1504
        memPoolFree calls:        481
File descriptor usage for squid:
        Maximum number of file descriptors:  65536  =============文件描述符
        Largest file desc currently in use:     16
        Number of file desc currently in use:   10
        Files queued for open:                   0
        Available number of file descriptors: 1014
        Reserved number of file descriptors:   100
        Store Disk files open:                   0
Internal Data Structures:
            26 StoreEntries
            26 StoreEntries with MemObjects
            25 Hot Object Cache Items
             0 on-disk objects
注释:
Squidclient参数:
取得squid运行状态信息: squidclient -p 80 mgr:info
取得squid内存使用情况: squidclient -p 80 mgr:mem
取得squid已经缓存的列表: squidclient -p 80 mgr:objects
取得squid的磁盘使用情况: squidclient -p 80 mgr:diskd
 
为了不每次输ulimit -HSn 65536 ,squid指定个启动脚本,复制一下脚本保存为squid.sh
#!/bin/sh
#
ulimit -HSn 65536
# starts and stops Squid
case "$1" in
start)
          /usr/local/squid/sbin/squid -s
          echo -n ' Squid'
          ;;
stop)
          /usr/local/squid/sbin/squid -k shutdown

          ;;
esac
 
以后启动或者停止squid,直接运行一下命令
[root@viong soft]# sh squid.sh stop     
[root@viong soft]# sh squid.sh start     
http://viong.blog.51cto.com/844766/280978


相关热词搜索:完全攻略 步骤 squid

上一篇:squid介绍及其简单配置
下一篇:如何在CentOS或RHEL上搭建Squid透明Web代理系统?