:(linux 局域网代理服务 路由器配制)
* .什么是透明代理?
* 如果你问:我如何才能使得用户的浏览器不需要任何代理设置就能使用我的Squid cache代理服务器上网?,此时你就需要使用透明代理。透明代理让你的客户端不需设置任何代理,当包经过透时代理服务器时实际上被重定向到squid代理服务器的代理端口(如8080),即由本地代理服务器向外请求所需数据然后拷贝给客户端。
* 2.我需要什么样的环境才能实现透明代理?
* a.客户端的windows PC的网关必须设成Squid代理服务器,因为既然你的browser中没有任何代理设置,你要访问某个站点时,包必须经经过squid代理服务器才能被重定向,故这是最基本的条件。
* b.客户端必须正确设置DNS服务器。因为既然现在不用设置任何代理。则DNS必须由browser来解析,也就是要由客户端的PC中TCP/IP中设置的DNS服务器来正确解析出某个站点的IP地址来
* c.服务器端可以安装squid代理服务器,1.x or 2.x版本均可。
* 3.配置Squid代理,启动透明代理功能
* Squid-2
* 加下面的行到你的/etc/squid/squid.conf中
* http_port 8080
* httpd_accel_host virtual
* httpd_accel_port 80
* httpd_accel_with_proxy on
* httpd_accel_uses_host_header on
* Squid-1.1
* 加下面的行到/etc/squid.conf
* http_port 8080
* httpd_accel virtual 80
* httpd_accel_with_proxy on
* httpd_accel_uses_host_header on
* 4. 重启动squid. 用下面的命令:
* #/usr/sbin/squid -k reconfigure 重启(/etc/init.d/squid restart ; service squid restart)
前提:
主机安装两块网卡,外网卡(假设eth1)设置为192.168.8.8内网卡(假设eth0)设定IP为:192.168.0.1
DNS不用说也要手动设置的,nameserer 192.168.8.1nameserver 202.96.128.166(/etc/resolv.conf)
eth1:.
NETMASK=255.255.255.0
NETWORK=192.168.8.0
BROADCAST=192.168.8.255
eth0:
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.0.1
NETMASK=255.255.255.0
NETWORK=192.168.0.0
BROADCAST=192.168.0.255
network:(/etc/network)
NETWORKING=yes
HOSTNAME=an
FORWARD_IPV4=yes
GATEWAY=192.168.8.1
设置透明代理时,必须打开包转发功能,
在启动脚本中加入本例在/etc/rc.d/firewall中加入,echo 1 > /proc/sys/net/ipv4/ip_forward;或者打开ipforward编辑/etc/sysctl.conf,修改下列一行:
net.ipv4.ip_forward=1
squid iptable的具体安装我在此不做说明(直接下载源码或通过RPM包安装)。
iptable的配置,在/etc/rc.d/目录下用touch命令建立firewall文件,执行chmod u+x firewll(chmod 755 firewall)以更改文件属性,编辑/etc/rc.d/rc.local文件,在末尾加上/etc/rc.d/firewall以确保开机时能自动执行该脚本。
firewall内容:
echo "starting ip forward"
echo "1">/proc/sys/net/ipv4/ip_forward
echo "start iptables rules"
modprobe ip_forward
modprobe ip_nat_ftp
/sbin/iptables -F -t nat
iptables -t nat -A PREROUTING -i eth0 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 3128 #将所有80端口的包转发到3128端口
iptables -t nat -A POSTROUTING -o eth1 -j MASQUERADE #对eth0端口进行欺骗
或iptables -t nat -A POSTROUTING -o eth1 -j SNAT --to-source 192.168.8.8
squid的配置
http_port 3128
cache_mem 512 M
cache_swap_low 75
cache_swap_high 95
maximum_object_size 1024 KB
cache_dir ufs /usr/local/squid/cache 60000 16 256
cache_access_log /var/squid/logs/access.log
cache_log /dev/null
cache_store_log none
debug_options ALL,1
icp_access allow all
icp_query_time out 2000
cache_effective_user nobody
cache_effective_group nogroup
httpd_accel_host virtual
httpd_accel_port 80
httpd_accel_with_proxy on
httpd_accel_uses_host_header on
acl all src 0.0.0.0/0
acl localnet src 10.0.0.0/20
http_access allow localnet
http_access deny all
acl QUERY urlpath_regex -i cgi-bin \? \.exe$ \.zip$ \.mp3$ \.mp2$ \.rm$ \.avi$
no_cache deny QUERY
reference_age 3 days
quick_abort_min 16 KB
quick_abort_max 16 KB
quick_abort_pct 95
connect_timeout 60 seconds
read_timeout 3 minutes
request_timeout 30 seconds
client_lifetime 30 seconds
half_closed_clients off
pconn_timeout 60 seconds
ident_timeout 10 seconds
shutdown_lifetime 10 seconds
memory_pools off
memory_pools_limit 0
****************-
编辑/etc/rc.d/rc.local
vi rc.local
/etc/rc.d/firewall(启动这个脚本)
#dns forward -t nat -A PREROUTING -d 192.168.0.0/24 -p udp --dport 53 -j DNAT --to-destination 192.168.8.1:53
iptables -t nat -A PREROUTING -d 192.168.0.0/24 -p tcp --dport 53 -j DNAT --to-destination 192.168.8.1:53
iptables -A FORWARD -P udp --dport 53 -j ACCEPT
iptables -A FORWARD -P tcp --dport 53 -j ACCEPT
#port nat(在这作端口映射)
iptables -t nat -A PREROUTING -d 192.168.8.8 -p tcp -m tcp --dport 3389 -j DNAT --to-destination 192.168.0.246:3389
iptables -t nat -A PREROUTING -d 192.168.8.8 -p tcp -m tcp --dport 21 -j DNAT --to-destination 192.168.0.2:21
*********************************
iptables 保存:
/etc/rc.d/init.d/iptables save
service iptables save