環(huán)境如下:
北侖網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)公司,北侖網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為北侖近千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)營(yíng)銷網(wǎng)站建設(shè)要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的北侖做網(wǎng)站的公司定做!
操作系統(tǒng):CentOS 6.4
eth0:192.168.100.1 (LAN口)
eth2:200.168.10.1 (WAN口)
一.iptables的配置
1.啟動(dòng)ip_forward(IP轉(zhuǎn)發(fā))
修改/etc/sysctl.conf
將net.ipv4.ip_forward = 1
并運(yùn)行sysctl -p,使之生效
[root@nagios~]# vim /etc/sysctl.conf
[root@nagios ~]# sysctl -p net.ipv4.ip_forward = 1 net.ipv4.conf.default.rp_filter = 1 net.ipv4.conf.default.accept_source_route = 0 kernel.sysrq = 0 kernel.core_uses_pid = 1 net.ipv4.tcp_syncookies = 1 kernel.msgmnb = 65536 kernel.msgmax = 65536 kernel.shmmax = 68719476736 kernel.shmall = 4294967296
2.清空iptables所有表的規(guī)則并查看nat表
[root@nagios ~]# iptables -F [root@nagios ~]# iptables -t nat -F [root@nagios ~]# iptables -t mangle -F [root@nagios ~]# iptables -t nat -L Chain PREROUTING (policy ACCEPT) target prot opt source destination Chain POSTROUTING (policy ACCEPT) target prot opt source destination Chain OUTPUT (policy ACCEPT) target prot opt source destination
3.設(shè)置端口重定向
[root@nagios ~]# iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 3128
#在NAT表的PREROUTING鏈加目標(biāo)動(dòng)作REDIRECT,將入站的數(shù)據(jù)包進(jìn)行重定向,將80端口重定向到3128端口
4.設(shè)置iptables路由DNS數(shù)據(jù)
[root@nagios~]#iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -p udp --dport 53 -d 202.96.128.86 -j SNAT --to-source 200.168.10.1 [root@nagios~]#iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -p tcp --dport 53 -d 202.96.128.86 -j SNAT --to-source 200.168.10.1
說(shuō)明:可以看出上面DNS是通過(guò)服務(wù)器的NAT功能來(lái)實(shí)現(xiàn),202.96.128.86是外部DNS服務(wù)器地址,而200.168.10.1是我們WAN口的IP。如果外部DNS服務(wù)器經(jīng)常變更的話,可以直接將DNS忽略掉:
iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -p tcp --dport 53 -j SNAT --to-source 200.168.10.1
iptables -t nat -A POSTROUTING -s 192.168.10.0/24 -p udp --dport 53 -j SNAT --to-source 200.168.10.1
5.保存iptables設(shè)置及重啟iptables服務(wù)
[root@Nagios ~]# service iptables save [root@Nagios ~]# service iptables restart iptables: Saving firewall rules to /etc/sysconfig/iptables:[ OK ] [root@Nagios ~]# iptables -t nat -L Chain PREROUTING (policy ACCEPT) target prot opt source destination REDIRECT tcp -- 192.168.10.0/24 anywhere tcp dpt:http redir ports 3128 Chain POSTROUTING (policy ACCEPT) target prot opt source destination SNAT udp -- 192.168.10.0/24 202.96.128.86 udp dpt:domain to:200.168.10.1 SNAT tcp -- 192.168.10.0/24 202.96.128.86 tcp dpt:domain to:200.168.10.1 Chain OUTPUT (policy ACCEPT) target prot opt source destination
二.Squid的安裝與配置
我們使用yum來(lái)安裝Squid:
[root@nagios ~]# yum -y install squid
備份squid的配置文件,防止因?yàn)榕渲缅e(cuò)誤
[root@nagios ~]# cp /etc/squid/squid.conf /etc/squid/squid.conf_bak
配置squid.conf
找到
http_port 3128修改成http_port 192.168.100.10:3128 transparent
并添加
visible_hostname squid acl innet src 192.168.10.0/24 http_access allow innet http_access deny all
下面就運(yùn)行squid -z進(jìn)行初始化
[root@nagios ~]# squid -z 2013/05/29 13:41:43| Creating Swap Directories
啟動(dòng)squid代理服務(wù)器
[root@nagios ~]# service squid start
重新啟動(dòng)iptables服務(wù)
[root@nagios ~]# service iptables restart
并將squid及iptables設(shè)置成開機(jī)啟動(dòng)
[root@nagios ~]# chkconfig --level 35 squid on [root@nagios ~]# chkconfig --level 35 iptables on
客戶端網(wǎng)絡(luò)設(shè)置: DNS設(shè)置為轉(zhuǎn)發(fā)的DNS IP
其他設(shè)置:
隱藏錯(cuò)誤頁(yè)面Squid版本
httpd_suppress_version_string on
不記錄訪問(wèn)日志
cache_store_log none cache_access_log /dev/null cache_log /dev/null
隱藏Header頭部信息(3.0以后版本)
request_header_access Via deny all request_header_access Server deny all request_header_access X-Cache deny all request_header_access X-Cache-Lookup deny all
如果是2.8之前的版本
header_access Via deny all header_access Server deny all header_access X-Cache deny all header_access X-Cache-Lookup deny all