Squid ----反向代理模式
一:定義
Squid作爲代理服務軟件,其主要功能是使用代理上網可以節約緊缺的IP地址資源,而且可以阻斷外部主機對內部主機的訪問,有一定的安全性,但是如果想讓互聯上的主機訪問內部網的主機資源(例如:web站點),又想使內部主機免受外部網主機攻擊,一般的代理服務是不能實現的,這就需要反向代理來實現。通過squid反向代理可以加速網站的訪問速度,可以將不同的URL請求分發到後臺不同的Web服務器上,同時互聯網用戶只能看到反向代理服務器的地址,加強了網站的訪問安全。
二:工作原理
反向代理服務器位於本地WEB 服務器和Internet 之間,當用戶瀏覽器發出一個HTTP 請求時,通過域名解析將請求定向到反向代理服務器(如果要實現多個WEB 服務器的反向代理,需要將多個WEB 服務器的域名都指向反向代理服務器)。由反向代理服務器處理器請求,反向代理一般只緩存可緩衝的數據(比如html 網頁和圖片等),而一些CGI 腳本程序或者ASP 之類的程序不緩存,它根據從WEB 服務器返回的HTTP 頭標記來緩衝靜態頁面。
三:實驗環境
目的:
- squid服務器配置反向代理,客戶機添加squid代理,然後訪問squid服務器域名時跳轉到web服務器頁面。
- 以輪詢的方式依次顯示web1、web2
四:實驗過程
1、掛載squid軟件包並解壓
[root@localhost ~]# hostnamectl set-hostname squid
[root@localhost ~]# su
[root@squid ~]# mkdir /abc
[root@squid ~]# mount.cifs //192.168.100.1/share /abc
[root@squid ~]# cd /abc
[root@squid abc]# tar zxvf squid-3.4.6.tar.gz -C /opt
2、安裝編譯工具,再編譯,再安裝
[root@squid abc]# cd squid-3.4.6
[root@squid squid-3.4.6]# yum install gcc gcc-c++ -y
[root@squid squid-3.4.6]# ./configure
--prefix=/usr/local/squid \
--sysconfdir=/etc \
--enable-arp-acl \
--enable-linux-netfilter \
--enable-linux-tproxy \
--enable-async-io=100 \
--enable-err-language="Simplify_Chinese" \
--enable-underscore \
--enable-poll \
--enable-gnuregex
[root@squid squid-3.4.6]# make && make install
3、創建軟鏈接,優化服務控制方式
[root@squid squid-3.4.6]# ln -s /usr/local/squid/sbin/* /usr/local/sbin
4、建立程序性用戶squid ,並賦予權限
[root@squid squid-3.4.6]# useradd -M -s /sbin/nologin squid
[root@squid squid-3.4.6]# chown -R squid.squid /usr/local/squid/var/
5、修改主配置文件
[root@squid squid-3.4.6]# vim /etc/squid.conf
6、設置防火牆規則
[root@squid squid-3.4.6]# iptables -F ‘清空所有規則’
[root@squid squid-3.4.6]# iptables -t nat -F ‘清除所有NAT規則’
[root@squid squid-3.4.6]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT ‘允許目標端口3128轉發’
7、檢擦語法並開啓服務
[root@squid squid-3.4.6]# squid -k parse ‘檢查語法,沒問題Ctrl+C鍵取消’
[root@squid squid-3.4.6]# squid -z ‘初始化緩存目錄’
[root@squid squid-3.4.6]# squid ‘啓動服務’
[root@squid squid-3.4.6]# netstat -ntap | grep squid ‘檢測是否啓動成功’
8、編輯腳本,優化服務控制方式
[root@squid squid-3.4.6]# cd /etc/init.d
[root@squid init.d]# vim squid
#!/bin/bash
#chkconfig: 2345 90 25
PID="/usr/local/squid/var/run/squid.pid"
CONF="/etc/squid.conf"
CMD="/usr/local/squid/sbin/squid"
case "$1" in
start)
netstat -natp | grep squid &> /dev/null
if [ $? -eq 0 ]
then
echo "squid is running"
else
echo "正在啓動 squid...."
echo "成功啓動"
$CMD
fi
;;
stop)
$CMD -k kill &> /dev/null
rm -rf $PID &> /dev/null
;;
status)
[ -f $PID ] &> /dev/null
if [ $? -eq 0 ]
then
netstat -natp | grep squid
else
echo "squid is not running"
fi
;;
restart)
$0 stop &> /dev/null
echo "正在關閉 squid..."
echo "關閉成功"
$0 start &> /dev/null
echo "正在啓動 squid..."
echo "啓動成功"
;;
reload)
$CMD -k reconfigure
;;
check)
$CMD -k parse
;;
*)
echo "用法:$0{start|stop|status|reload|check|restart}"
;;
esac
[root@squid init.d]# chmod +x squid ‘添加權限’
[root@squid init.d]# chkconfig --add squid ‘便於service識別’
[root@squid init.d]# service squid stop ‘關閉squid服務’
[root@squid init.d]# service squid start ‘開啓squid’
9、修改網卡信息
[root@squid squid-3.4.6]# cd /etc/sysconfig/network-scripts/
[root@squid network-scripts]# cp -p ifcfg-ens33 ifcfg-ens36
[root@squid network-scripts]# vim ifcfg-ens36
[root@suqid network-scripts]# service network restart
Restarting network (via systemctl): [ 確定 ]
[root@web01 ~]# systemctl stop firewalld.service
[root@web01 ~]# setenforce 0
[root@web01 ~]# yum install httpd -y
[root@web01 ~]# cd /var/www/html
[root@web01 html]# vim index.html
<h1>this is test1 web</h1>
[root@web01 html]# systemctl start httpd.service
[root@web01 html]# netstat -ntap | grep 80
tcp6 0 0 :::80 :::* LISTEN 3843/httpd
[root@web01 html]# route add -net 192.168.10.0/24 gw 192.168.48.128
在win10客戶機測試
[root@web02 ~]# systemctl stop firewalld.service
[root@web02 ~]# setenforce 0
[root@web02 ~]# yum install httpd -y
[root@web02 ~]# cd /var/www/html/
[root@web02 html]# ls
[root@web02 html]# vim index.html
<h1>this is test2 web</h1>
[root@web02 html]# systemctl start httpd.service
[root@web02 html]# netstat -ntap | grep 80
tcp 0 36 192.168.48.132:22 192.168.48.1:51780 ESTABLISHED 2434/sshd: root@pts
tcp6 0 0 :::80 :::* LISTEN 4050/httpd
在win10客戶機測試
10、客戶端設置域名解析和squid代理並測試
(以 administrator用戶登錄 )
以谷歌瀏覽器爲例:
通過客戶機訪問www.hao.com ,以輪詢的方式顯示web界面。