Centos 5.6下squid 2.6  安装 (透明代理)

第一:说明,软件说明,和安装的目的

架设基于linux下的代理服务器,使用透明代理的好处在于,内网用户端完全透明,使用中只要将内网用户的网关地址设成squid的在址址就可实现代理上网。

本文参照:http://wenku.baidu.com/view/b70a60f64693daef5ef73da0.html

第二:本例操作环境

所使用的系统环境为 Centos 5.6 64位操作系统

[root@jedy ~]# uname -a
Linux jedy.com 2.6.18-238.el5 #1 SMP Thu Jan 13 15:51:15 EST 2011 x86_64 x86_64 x86_64 GNU/Linux
[root@jedy ~]# more /etc/redhat-release
CentOS release 5.6 (Final)
[root@jedy ~]#

第三:所需要的软件包如下

squid软件包在系统光盘里有

第四:安装、配置

[root@jedy ~]# rpm –ivh squid-2.6.STABLE21-6.el5.i386.rpm

编辑配置文件,以下是修改后的配置文件,为方便显示特去除了#和空行

[root@jedy squid]# more squid.conf | grep -v "#" |grep -v ^$
acl all src 0.0.0.0/0.0.0.0
acl manager proto cache_object
acl localhost src 127.0.0.1/255.255.255.255
acl to_localhost dst 127.0.0.0/8
acl SSL_ports port 443
acl CONNECT method CONNECT
acl testip dst 192.168.10.0/24                       \\对访问的目的地址进行定义
acl jedy dstdomain .jedy.com                       \\对访问的目的域进行定义
http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow testip                                \\允许之前定义的testip通过

http_access allow jedy                                   \\允许之前定义的jedy通过

http_access deny all
#always_direct allow testip jedy                    \\直接出去取数据,不通过squid
http_reply_access allow all
icp_access allow all
http_port 3128 transparent                        \\ 定义squid端口 和使用透明代理模式

cache_mem 1000 MB                                   \\额外提供给squid使用的内存,squid的内存总占用为 X * 10+15+“cache_mem”其是 X为squid的cache占用的容量(以GB为单位)

 

cache_swap_low 80
cache_swap_high 85                                    \\ cache目录的限值,超过总容量的85%时会自动清理旧数据至总容量的80%

hierarchy_stoplist cgi-bin ?
cache_dir ufs /var/spool/squid 1024 16 256                \\ 定义squid的cache存放路径 、cache目录容量(单位M)、一级缓存目录数量、二级缓存目录数量
access_log /var/log/squid/access.log squid                 \\ log文件存放路径 和日志格式

cache_log /var/log/squid/cache.log

cache_store_log /var/log/squid/store.log

pid_filename /var/log/squid/squid.pid


acl QUERY urlpath_regex cgi-bin \?
cache deny QUERY
refresh_pattern ^ftp:           1440    20%     10080
refresh_pattern ^gopher:        1440    0%      1440
refresh_pattern .               0       20%     4320
acl apache rep_header Server ^Apache
broken_vary_encoding allow apache
coredump_dir /var/spool/squid

[root@jedy ~]#

[root@jedy ~]#services squid start

[root@jedy ~]#netstart –tlnp | grep 3128

新建一个脚本,增加iptables规则

[root@jedy ~]#vi /etc/rc.d/iptables.sh


#!/bin/bash
 
iptables -t nat -F
iptables -F
 
###############################################
## squid configure
###############################################
echo 1 > /proc/sys/net/ipv4/ip_forward
echo "starting iptables rules ....."
iptables -t nat -A PREROUTING -i eth1 -p tcp --dport 80 -j REDIRECT --to-port 3128      
iptables -t nat -A POSTROUTING -s 0.0.0.0/0 -o eth0 -j SNAT --to 202.22.252.114

\\如果第一句不写,只写第二句,那就没有转发功能。也就会出现只透明不代理。

#iptables -t nat -A POSTROUTING -s 192.168.19.0/24 -o eth0 -j SNAT --to-source 202.22.252.114
 
 
###############################################

[root@jedy ~]# sh /etc/rc.d/iptables.sh

第五:测试

在客户端将网关设置成squid的ip地址 访问外网。

[root@jedy ~]#netstat -aunt | grep 3128

tcp        0      0 0.0.0.0:3128                0.0.0.0:*                   LISTEN     
tcp        0      0 202.22.252.114:3128          192.168.19.63:39187         TIME_WAIT  
tcp        0      0 202.22.252.114:3128          192.168.19.63:39186         TIME_WAIT  
tcp        0      0 202.22.252.114:3128          192.168.19.63:39185         ESTABLISHED
tcp        0      0 202.22.252.114:3128          192.168.19.63:39184         ESTABLISHED
tcp        0      0 202.22.252.114:3128          192.168.19.63:39188         ESTABLISHED
tcp        0      0 202.22.252.114:3128          192.168.19.63:39168         TIME_WAIT  
tcp        0      0 202.22.252.114:3128          192.168.19.63:39183         ESTABLISHED
tcp        0      0 202.22.252.114:3128          192.168.19.63:39182         ESTABLISHED
tcp        0      0 202.22.252.114:3128          192.168.19.63:39181         ESTABLISHED
tcp        0      0 202.22.252.114:3128          192.168.19.63:39180         TIME_WAIT

说明所有的访问都是通过代理都出去了