红联Linux门户
Linux帮助

Iptables+squid 多网段透明代理的实现

发布时间:2009-10-16 20:18:56来源:红联作者:gary168
首先,还是将我的测试环境介绍一下:
代理服务器:
OS:RedHat Enterprise LINUX AS 4.0
Proxy Server Version: squid-2.5.STABLE6-3.4E.5 (系统自带)

网卡设置
Eth0:192.168.0.251 (连接外网)
Eth1:192.168.1.254 (连接内网)
路由器:(主要是用来连接内网的各个子网间的通信,路由器我是用 LINUX 来做的软路由
)
OS: OS:RedHat Enterprise LINUX AS 4.0

网卡设置:
Eth0:192.168.1.1
Eth1:192.168.2.1
客户端:
Client-1:
OS: Windows 2000 Professional

网卡设置:
IP address: 192.168.1.111
Netmask: 255.255.255.0
Gateway:192.168.1.1
DNS:61.144.56.100
Client-2
OS:Windows XP Professional
IP address: 192.168.2.120
Netmask: 255.255.255.0
Gateway:192.168.2.1
DNS:61.144.56.100
[attach]21074[/attach]
本次测试的目的:
1.实现多个网段的透明代理
2.通过 Squid + Iptables 的透明代理实现在客户端可以用 OE 收发外网邮箱的邮件
二、具体的设置
代理服务器的配置
1.Squid 的配置
http_port 3128
hierarchy_stoplist cgi-bin ?
hierarchy_stoplist -i ^https:\\ ?
acl QUERY urlpath_regex -i cgi-bin \? \.asp \.php \.jsp \.cgi
acl denyssl urlpath_regex -i ^https:\\
no_cache deny QUERY
no_cache deny denyssl
cache_mem 24 MB
cache_swap_low 90
cache_swap_high 95
maximum_object_size 4096 KB
maximum_object_size_in_memory 8 KB
ipcache_size 1024
ipcache_low 90
ipcache_high 95
fqdncache_size 1024
cache_dir aufs /usr/local/squid/var/cache1 100 16 256
cache_dir aufs /usr/local/squid/var/Cache2 200 16 256
access_log /usr/local/squid/var/logs/access.log squid
cache_log /usr/local/squid/var/logs/cache.log
cache_store_log /usr/local/squid/var/logs/store.log
pid_filename /usr/local/squid/var/logs/squid.pid
ftp_user Squid@
ftp_passive on
refresh_pattern ^ftp: 1440 20% 10080

refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern . 0 20% 4320
connect_timeout 1 minute
peer_connect_timeout 30 seconds
request_timeout 2 minutes
persistent_request_timeout 1 minute
cache_mgr webmaster@localhost
cache_effective_user squid
cache_effective_group squid
visible_hostname redhat
/**************** 下面四行是实现透明代理的关键之处 *****************\
httpd_accel_host virtual
httpd_accel_port 80
httpd_accel_with_proxy on
httpd_accel_uses_host_header on

2.iptables 的配置
以下是我写的一个脚本:脚本名为:firewall(当然你也可以根据自己的喜好设置名称,用 VI 编辑器编译器好保存,然后将文件的属性设置为可执行,用 chmod 777 firewall (这只是一个示例,可根据实际情况更改)命令更改文件的属性为可执行文件)
#!/bin/bash
echo “enable ip forwarding”
#打开路由转发功能
echo “1” > /proc/sys/net/ipv4/ip_forward
#加载 IPTABLES 所需要的模块
echo “starting iptables rules”
/sbin/modprobe iptable_fileter
/sbin/modprobe ip_tables
/sbin/modprobe iptables_nat
/sbin/modprobe ip_conntrack_ftp
/sbin/modprobe ip_nat_ftp
#刷新 IPTABLES 的规则链
/sbin/iptables -F
/sbin/iptables -X
/sbin/iptables -Z
/sbin/iptables -F -t nat
/sbin/iptables -X -t nat
/sbin/iptables -Z -t nat
#所有从 eth1 进来的,目标端口为 80 的流量都转到代理服务器默认的 3128 端口上
/sbin/iptables -t nat -A PREROUTING -i eth1 -p tcp -dport 80 -j REDIRECT -to-ports 3128
#对出站访问 IP 地址进行伪装
/sbin/iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -o eth0 -j MASQUERADE
/sbin/iptables -t nat -A POSTROUTING -s 192.168.2.0/24 -o eth0 -j MASQUERADE
代理服务器的路由设置
#在 squid 服务器上添加一条到 192.168.2.0 网络上的路由,网关为 192.168.1.1
route add -net 192.168.2.0/24 gw 192.168.1.1

二、路由器上的设置
/********************* 打开路由转发功能******************\
# echo 1 > /proc/sys/net/ipv4/ip_forward
路由器原来的默认路由为 0.0.0.0 0.0.0.0 192.168.1.1,将这条路由删除,添加另外一条默认路由:
route add default gw 192.168.1.254
至些多网段透明代理的设置就算是完成了,现在到客户端进行测试,可以正常浏览网页,在客户设置好 OE 可以收发邮件,我是用 sina 和 21cn 的邮箱测试的,163 的邮箱不知道怎么回事,收发邮件的时候总是会弹出一个认证框,让重新输入密码,但输入密码还是不行,怀疑应该是 163 服务器方面的原因。
文章评论

共有 6 条评论

  1. vitas333 于 2009-12-07 13:34:04发表:

    squid 2.6的版本, 只需要这个, 然后iptables 再设置下 转发。 就可以实现透明代理。 至于 iptables 和squid 不同一台的, 还没想好咋整。。

    http_port 3128 transparent

  2. zw3312096 于 2009-12-05 11:27:17发表:

    好贴

  3. Summerserenity 于 2009-12-04 10:08:27发表:

    httpd_accel_host virtual
    httpd_accel_port 80
    httpd_accel_with_proxy on
    httpd_accel_uses_host_header on
    在实现透明代理上,我总是卡在以上四行关键,总是提示不能识别。想了许多办法也不行。请问,在debian中怎样叫它识别从而实现这样的透明代理呢?谢谢。

  4. YBnlbqj54 于 2009-11-18 20:20:49发表:

    谢谢分享!

  5. li659258510 于 2009-10-29 16:06:39发表:

    参考了!

  6. samlisamli 于 2009-10-29 13:28:49发表:

    好好好帖