squid

squid3.0单网卡透明代理服务器[原创]

看网上很多人都做了squid的透明代理,今天自己也试验了一个,使用的是squid3.0stable8版本+iptables,下面就吧我的过程总结如下,首先看下我的拓扑结构
squid单网卡,通过交换机可以直接连接到Internet,A和B两组client可以处在不同的网段或相同的网段,只要可以和squid所在网段互通就行
本文本着只把透明代理实现的最基本过程整理如下,对于squid详细配置,以及iptables相对应的安全配置一概不涉及,有需要的朋友请自行去相应网站查找
言归正传,我的环境如下
vmware6.0模拟两个系统1、centos4.4(squid:192.168.220.128),2、winxp(ip:192.168.220.140)
squid基本配置:

acl manager proto cache_object
acl localhost src 127.0.0.1/32
acl to_localhost dst 127.0.0.0/8
acl localnet src 10.0.0.0/8 # RFC1918 possible internal network
acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl SSL_ports port 443
acl Safe_ports port 80 # http
acl Safe_ports port 21 # ftp
acl Safe_ports port 443 # https
acl Safe_ports port 70 # gopher
acl Safe_ports port 210 # wais
acl Safe_ports port 1025-65535 # unregistered ports
acl Safe_ports port 280 # http-mgmt
acl Safe_ports port 488 # gss-http
acl Safe_ports port 591 # filemaker
acl Safe_ports port 777 # multiling http
acl CONNECT method CONNECT

http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports

http_access allow localnet

http_access deny all
always_direct allow localnet

http_port 3128 transparent       #透明代理的关键参数

hierarchy_stoplist cgi-bin ?

access_log /opt/squid/var/logs/access.log squid
cache_dir ufs /tmp 10 2 4 no-store

refresh_pattern ^ftp: 1440 20% 10080
refresh_pattern ^gopher: 1440 0% 1440
refresh_pattern (cgi-bin|?) 0 0% 0
refresh_pattern . 0 20% 4320

visible_hostname 1

cache_effective_user squid
cache_effective_group squid

然后正常启动squid,测试机xp系统ie中设置代理服务器,可以正常上网,说明,squid目前运行正常

接下来是iptables的配置

echo "1" >/proc/sys/net/ipv4/ip_forward
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-ports 3128

完成后,将xp中ie的代理去掉,更改xp的网关为192.168.220.128,然后配置好dns,直接在访问网页,就可以看到你想要的页面了,而squid中也有正常访问的日志了

这次最简单的squid透明代理就做完了

关于实施中的一点点问题,最先开始的时候,squid中我没有添加transparent这个参数,结果加完iptables后,访问返回的错误的页面如下

ERROR
The requested URL could not be retrieved

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

While trying to retrieve the URL: /

The following error was encountered:

Invalid URL
Some aspect of the requested URL is incorrect. Possible problems:

Missing or incorrect access protocol (should be `http://'' or similar)
Missing hostname
Illegal double-escape in the URL-Path
Illegal character in hostname; underscores are not allowed
Your cache administrator is webmaster.

后来加上这个参数后就正常了

Related Post

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *