From:http://bbs.linuxtone.org/thread-5276-1-1.html



首先感谢netseek的指导,我也是参考他的文档和口头指导才做成了这个文档。
Netseek在论坛里贴了一个自己站点的squid3.0配置,我发现那个帖子只有配置没有解释,没接触过squid的新手是无法理解那个帖子的;
找了下鸟哥的帖子,鸟哥只介绍squid怎么做正向代理了,而http加速反向代理没有提;
又搜到了一些超详细的帖子,每个细节参数做什么都解释的很清楚了,反而让人或者看不下去,或者注意的东西太多,找不到真正的重点了。
通过借鉴Netseek的帖子,查询squid.conf.default以及一些详细介绍squid配置文件的帖子,我完成了下文。
希望高手们能指正我的一些不足,也希望新手朋友们多对这个教程做做测试,如果测试过程中发现了问题,请回帖并站内短信联系我。
本文首发自 http://bbs.linuxtone.org/thread-5276-1-1.html 
第一,系统设置
1,首先修改文件描述符并设定临时端口范围,这些设置重启后生效:

  1. cat >> /etc/security/limits.conf <<DDD

  2. * soft nofile 8192

  3. * hard nofile 20480

  4. DDD

复制代码

  1. cat >> /etc/sysctl.conf <<DDD

  2. #set temp port range

  3. net.ipv4.ip_local_port_range = 32768 61000

  4. DDD


  5. init 6

复制代码


确认修改的方法:

  1. [root@caotest ~]# ulimit -n

  2. 8192

  3. [root@caotest ~]# sysctl -n net.ipv4.ip_local_port_range

  4. net.ipv4.ip_local_port_range = 32768 61000

复制代码


2,添加squid专用账户:

  1. groupadd squid

  2. useradd -g squid -s /bin/false -M squid

复制代码


3,挂载cache专用分区,在本文中,我使用/dev/sda3挂载在/data目录。
手动卸载并按照新的参数挂载现有的文件系统:

  1. umount /data

  2. mount -o noatime,async /dev/sda3 /data/

复制代码


修改/etc/fstab,修改/data分区启动时的挂载方式。如下文,硬件位置、挂载点、文件系统都不变,将默认的“defaults”选项改为“noatime,async”

  1. sed -i '/data/s/defaults/noatime,async/g' /etc/fstab

复制代码


添加日志目录、设置缓存和日志目录的权限

  1. mkdir /data/squidlog/

  2. chown -R squid.squid /data

复制代码


4,设置防火墙打开80端口,下面的方法适用与打开了22端口的iptables:

  1. sed -i '/--dport 22 -j ACCEPT/a\-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT' /etc/sysconfig/iptables 

  2. /etc/init.d/iptables restart

复制代码



本文首发自 http://bbs.linuxtone.org/thread-5276-1-1.html 
第二,编译安装
3,准备squid安装包,解压缩并进入目录。

  1. [root@caotest ~]# ll squid-3.0.STABLE21.tar.gz 

  2. -rw-r--r-- 1 root root 2452990 Jan 18 15:33 squid-3.0.STABLE21.tar.gz

  3. [root@caotest ~]# tar xzfv squid-3.0.STABLE21.tar.gz

  4. [root@caotest ~]# cd squid-3.0.STABLE21

复制代码


编译参数如下:

  1. ./configure --prefix=/usr/local/squid3 --enable-async-io=100 --with-pthreads --enable-storeio="aufs,diskd,ufs" --enable-removal-policies="heap,lru" --enable-icmp --enable-delay-pools --enable-useragent-log --enable-referer-log --enable-kill-parent-hack --enable-arp-acl --enable-default-err-language=Simplify_Chinese --enable-err-languages="Simplify_Chinese English" --disable-poll --disable-wccp --disable-wccpv2 --disable-ident-lookups --disable-internal-dns --enable-basic-auth-helpers="NCSA" --enable-stacktrace --with-large-files --disable-mempools --with-filedescriptors=65535 --enable-ssl --enable-x-accelerator-var


  2. make 

  3. make install

  4. make install-pinger

复制代码


第三,编辑配置文件
编辑squid.conf,其中对详细参数不解的,可看同目录的squid.conf.default。
首先清空旧配置文件然后vi打开文件后,把下面的内容复制到空文件中去

  1. > /usr/local/squid3/etc/squid.conf

  2. vi /usr/local/squid3/etc/squid.conf

复制代码


  1. #basic

  2. #c这里设置一些基础设置信息,唯一必须改的就是cache_mgr(管理员邮箱)

  3. cache_effective_user squid

  4. cache_effective_group squid

  5. pid_filename /usr/local/squid3/var/logs/squid.pid

  6. visible_hostname caotest.linuxtone

  7. cache_mgr caotest@linuxtone.org

  8. error_directory /usr/local/squid3/share/errors/Simplify_Chinese

  9. icon_directory /usr/local/squid3/share/icons

  10. mime_table /usr/local/squid3/etc/mime.conf

  11. hosts_file /etc/hosts


  12. #c这些设置是cache在硬盘和内存中的轮询替换策略

  13. cache_replacement_policy lru

  14. memory_replacement_policy lru

  15. #chttp监听的端口以及监听方式

  16. http_port 80 accel vhost vport

  17. #csquid可用的内存,请根据服务器实际情况酌情增加

  18. cache_mem 256 MB

  19. icp_port 0


  20. #.cache_dir

  21. #c这里是来指定squid目录和缓存文件的大小

  22. cache_dir aufs /data/cache1 32768 64 64

  23. cache_dir aufs /data/cache2 32768 64 64

  24. max_open_disk_fds 0

  25. maximum_object_size 20 MB

  26. maximum_object_size_in_memory 8 MB


  27. #.cache_peer

  28. #c下文中的cache_peer定义了一个叫www的邻居,这个邻居的主机名/ip地址是“192.168.118.3”,监听80端口,是该squid服务器的“parent”。请根据实际情况进行修改

  29. cache_peer 192.168.118.3 parent 80 0 no-query originserver no-digest name=www

  30. #c下文是说针对“a.com”“caotest.com”“caotest.com.cn”的访问都抛给上文中定义的“www”邻居,请根据实际情况进行修改。

  31. cache_peer_domain www a.com .caotest.com .caotest.com.cn


  32. #acl

  33. #c请修改LanSrc和webip为您的web服务器的IP或网段;

  34. acl Safe_ports port 80

  35. acl SSL_ports port 443

  36. acl LanSrc src 192.168.118.0/24

  37. acl webip dst 192.168.118.3

  38. acl webdomain dstdomain .a.com

  39. acl manager proto cache_object

  40. acl localhost src 127.0.0.1/255.255.255.255

  41. acl CONNECT method CONNECT

  42. http_access allow manager localhost

  43. http_access deny manager

  44. http_access deny !Safe_ports

  45. http_access deny CONNECT !SSL_ports

  46. http_access allow LanSrc

  47. http_access allow webdomain

  48. http_access allow webip

  49. http_access deny all


  50. #.cache deny

  51. #c指定不缓存的内容。其中test_deny是我个人测试着玩的,QUERY 和 DIRECT是一个典型的DZ论坛的缓存规则。

  52. acl test_deny url_regex -i testfile

  53. acl test_deny url_regex -i \.mp3$

  54. acl test_deny url_regex -i \.php$

  55. acl test_deny url_regex -i ^ftp

  56. cache deny test_deny 

  57. hierarchy_stoplist cgi-bin ? \.php

  58. acl QUERY urlpath_regex cgi-bin\? \.php \.css

  59. acl DIRECT url_regex -i ^http:\/\/bbs\.a\.com\/$

  60. acl DIRECT url_regex -i ^http:\/\/bbs\.caotest\.com\/.*$

  61. acl DIRECT url_regex -i ^http:\/\/bbs\.caotest\.com\/index\.html$

  62. cache deny QUERY

  63. cache deny DIRECT


  64. #refresh_pattern

  65. refresh_pattern ^ftp: 60 20% 10080

  66. refresh_pattern ^gopher: 60 0% 1440

  67. refresh_pattern . 0 20% 1440

  68. refresh_pattern -i \.css$ 360 50% 2880 reload-into-ims

  69. refresh_pattern -i \.js$ 1440 50% 2880 reload-into-ims

  70. refresh_pattern -i \.html$ 720 50% 1440 reload-into-ims

  71. refresh_pattern -i \.jpg$ 1440 90% 2880 ignore-reload

  72. refresh_pattern -i \.gif$ 1440 90% 2880 ignore-reload

  73. refresh_pattern -i \.swf$ 1440 90% 2880 ignore-reload

  74. refresh_pattern -i \.jpg$ 1440 50% 2880 ignore-reload

  75. refresh_pattern -i \.png$ 1440 50% 2880 ignore-reload 

  76. refresh_pattern -i \.bmp$ 1440 50% 2880 ignore-reload

  77. refresh_pattern -i \.doc$ 1440 50% 2880 ignore-reload

  78. refresh_pattern -i \.ppt$ 1440 50% 2880 ignore-reload

  79. refresh_pattern -i \.xls$ 1440 50% 2880 ignore-reload

  80. refresh_pattern -i \.pdf$ 1440 50% 2880 ignore-reload

  81. refresh_pattern -i \.rar$ 1440 50% 2880 ignore-reload

  82. refresh_pattern -i \.zip$ 1440 50% 2880 ignore-reload

  83. refresh_pattern -i \.txt$ 1440 50% 2880 ignore-reload


  84. #keepalived

  85. client_persistent_connections off

  86. server_persistent_connections on


  87. #log

  88. emulate_httpd_log on

  89. logformat web1 %{X-Forwarded-For}>h %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh

  90. cache_log /data/squidlog/cache.log

  91. cache_access_log /data/squidlog/access.log web1

  92. cache_store_log /dev/null

  93. strip_query_terms off

  94. logfile_rotate 10


  95. #other

  96. forwarded_for on

  97. log_icp_queries off

  98. via off

  99. httpd_suppress_version_string off

  100. ie_refresh off

  101. tcp_recv_bufsize 32 KB

  102. ipcache_size 1024

  103. ipcache_low 90

  104. ipcache_high 95

  105. request_header_max_size 128 KB


  106. #c本文首发自 http://bbs.linuxtone.org/thread-5276-1-1.html 

  107. quick_abort_min 20 KB

  108. quick_abort_max 20 KB

  109. quick_abort_pct 95


  110. connect_timeout 1 minute

  111. negative_ttl 0 minutes


  112. read_timeout 30 seconds

  113. pconn_timeout 120 seconds

  114. half_closed_clients off

  115. client_lifetime 10 minutes

  116. shutdown_lifetime 5 seconds


  117. hierarchy_stoplist cgi-bin ?

  118. access_log /usr/local/squid3/var/logs/access.log squid

复制代码



在更改了squid.conf的一些必须的设定(如定义邻居)之后,执行下面的命令删除之前添加的中文注释:

  1. sed -i '/^\#c/d' /usr/local/squid3/etc/squid.conf

复制代码


最后,设置squid用户对squid3目录的所有者。

  1. chown -R squid.squid /usr/local/squid3

复制代码




第四,日常命令;以下为常用的squid命令。
1,初始化cache目录

  1. /usr/local/squid3/sbin/squid -z

复制代码


2,检查配置文件配置的语法是否正确;
#注:只有像这样的“cache_cf.cc(346) squid.conf:14 unrecognized: 'http1_port'”才是语法错误,
#“WARNING: use of 'reload-into-ims'……”这类提示属于意见建议,可以忽略。

  1. /usr/local/squid3/sbin/squid -k parse

复制代码


3,运行squid
#-D参数用来跳过DNS检测;

  1. /usr/local/squid3/sbin/squid -D

复制代码


4,重读配置文件

  1. /usr/local/squid3/sbin/squid -k reconfigure

复制代码


5,查看squid的日志和进程,看squid运行是否正常:

  1. ps -ef |awk '/^squid/'

  2. cat /data/squidlog/cache.log 

  3. /usr/local/squid3/sbin/squid -k check ;echo $?

复制代码



6,关闭squid(发出关闭信号,等会话结束后彻底关闭)

  1. /usr/local/squid3/sbin/squid -k shutdown

复制代码


关闭squid(更高优先级,直接关闭squid)

  1. /usr/local/squid3/sbin/squid -k interrupt

复制代码


关闭squid(最高优先级,直接杀死squid进程)

  1. /usr/local/squid3/sbin/squid -k kill

复制代码


7,滚动日志文件

  1. /usr/local/squid3/sbin/squid -k rotate

复制代码



本文首发自 http://bbs.linuxtone.org/thread-5276-1-1.html 
第五,设置自启动项:
1,设置squid自启动。

  1. echo "/usr/local/squid3/sbin/squid -D " >>/etc/rc.local

复制代码


2, 设置每周二凌晨两点25分自动滚动日志;

  1. echo "25 2 * * 2 root /usr/local/squid3/sbin/squid -k rotate " >>/etc/crontab

复制代码


第六,测试squid
上文的squid.conf配置部分,已经提到我们定义了一个192.168.118.3的邻居,请保证这个邻居开启了web服务,并且我们可以正常访问到他的web服务。
监视access.log日志

  1. tail -f /data/squidlog/access.log

复制代码


然后在第三台服务器上使用a.com这个域名访问squid代理服务器(需要设置这台服务器的DNS 或者hosts文件),如果能正常返回192.168.118.3 web服务器的页面,则代表测试成功。
同时access.log中也会记录下访问的动作,如:

  1. - - - [10/Mar/2010:09:36:49 +0800] "GET http://a.com/basic/0110whatislinux.htm HTTP/1.1" 200 95944 "http://a.com/index.htm" "Opera/9.80 (Windows NT 5.1; U; zh-cn) Presto/2.2.15 Version/10.10" TCP_MISS:FIRST_UP_PARENT