搭建squid代理伺服器
準備環境:
客戶機:192.168.118.4
squid代理伺服器:192.168.118.3(内網IP)1.1.1.1(外網IP)
web伺服器:1.1.1.2
實作目标:客戶機通過squid代理伺服器通路web伺服器
一、普通代理
1.首先将各個主機的防火牆關閉,然後實作squid伺服器分别與另外兩台機器互通
2.給web伺服器搭建HTTP服務
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# service httpd start
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# echo "it is work" > index.html
測試能否成功通路
[root@localhost html]# elinks --dump 1.1.1.2
3.squid在伺服器上安裝squid服務
[root@localhost ~]# yum -y install squid
[root@localhost ~]# service squid start
[root@localhost ~]# cd /etc/squid/
[root@localhost squid]# mv squid.conf squid.conf.bak
[root@localhost squid]# grep -vE "^$|^#" squid.conf.bak > suqid.conf
[root@localhost squid]# vim squid.conf
修改允許所有主機通路
http_access allow all
添加緩存目錄和進階緩存目錄
cache_dir ufs /var/spool/squid 100 16 256
cache_mem 64 MB
[root@localhost squid]# service squid restart
測試通路web伺服器
[root@localhost squid]# elinks --dump 1.1.1.2
4.設定客戶機的浏覽器
以火狐浏覽器為例
首選項—進階—網絡—設定—手動配置代理—HTTP代理:192.168.118.3 端口:3128
然後用浏覽器通路web伺服器1.1.1.2
二、透明代理
1.必須是網絡中的網關主機。
2.必須和防火牆服務運作在同一台伺服器上。
3.修改squid伺服器
[root@localhost squid]# vim /etc/squid/squid.conf
修改
http_port 3128 transparent
停止 squid:. [确定]
啟動 squid:. [确定]
4.修改防火牆規則
[root@localhost squid]# service iptables start
[root@localhost squid]# iptables -t nat -A PREROUTING -s 192.168.118.0/24 -i eth0 -p tcp --dport 80 -j REDIRECT --to-ports 3128
檢視防火牆規則
[root@localhost squid]# iptables -t nat -L
儲存防火牆規則
[root@localhost squid]# service iptables save
5.修改客戶機
取消浏覽器的代理
設定網關為squid伺服器的Ip位址
[root@localhost ~]# route add default gw 192.168.118.3
[root@localhost ~]# route -n
[root@localhost ~]# elinks --dump 1.1.1.2
三、通路控制清單
1.在透明傳輸的基礎上實作
2.修改squid伺服器主機的配置檔案
3.[root@localhost ~]# vim /etc/squid/squid.conf
添加以下
acl pc22 src 192.168.118.22/32 //聲明一個源位址為192.168.118.22的位址
acl no_time time MTWHF 09:00-18:00 //聲明一個時間段為周一到周五每天的9點到18點
acl no_nodamin dstdomain baidu.com qq.com //聲明兩個域
acl no_url urlpath_regex -i \.mp3$ \.mp4$ //聲明兩個url位址,分别是音樂和電影位址
acl mynet src 192.168.118.0/24 //定義一個網段
http_access deny pc22 //拒絕192.168.118.22通路外網
http_access deny mynet no_time no_nodamin no_url//拒絕192.168.118.0網段在規定時間内通路指定網站和範圍内的網址
http_access allow mynet //允許所有該網段所有主機通路
http_access deny all //拒絕所有主機通路
[root@localhost ~]# service squid restart
4.内網的用戶端驗證
修改IP位址
[root@localhost ~]# ifconfig eth0 192.168.118.22
發現通路出錯
可以得出squid的acl配置生效
四、反向代理
将内網主機作為web伺服器供外網主機通路
1.在内網用戶端IP位址為192.168.1.4的主機上配置以下
安裝HTTP服務
[root@localhost ~]# vim /etc/httpd/conf/httpd.conf
添加以下基于域名虛拟主機
NameVirtualHost *:80
<VirtualHost *:80>
ServerName www.tarena.com
DocumentRoot /var/www/html
</VirtualHost>
ServerName bbs.tarena.com
DocumentRoot /bbs
建立虛拟主機首頁
[root@localhost ~]# echo "<center><h1>inside-web</h1></center>" > /var/www/html/index.html
[root@localhost ~]# mkdir /bbs
[root@localhost ~]# echo "bbs.tarena.com" > /bbs/index.html
重新開機服務
[root@localhost ~]# service httpd restart
添加域名解析
[root@localhost bbs]# vim /etc/hosts
添加
192.168.118.4 www.tarena.com www
192.168.118.4 bbs.tarena.com bbs
驗證能否正确通路虛拟主機
[root@localhost bbs]# elinks --dump www.tarena.com
inside-web
[root@localhost bbs]# elinks --dump bbs.tarena.com
bbs.tarena.com
2.修改squid主機配置檔案
[root@localhost ~]# vim /etc/squid/squid.conf
http_port 80 vhost
cache_peer 192.168.118.4 parent 80 0 originserver
3.修改外網IP位址為1.1.1.2的主機
[root@localhost ~]# vim /etc/hosts
1.1.1.1 www.tarena.com www
1.1.1.1 bbs.tarena.com bbs
驗證
[root@localhost ~]# elinks --dump www.tarena.com
[root@localhost ~]# elinks --dump bbs.tarena.com
能否成功通路内網主機的web
本文轉自 無心低語 51CTO部落格,原文連結:http://blog.51cto.com/fengzhankui/1557117,如需轉載請自行聯系原作者