前言
反向代理,是指以代理服務器來接受internet上的連接請求,然後將請求轉發給內部網絡上的服務器,並將從服務器上得到的結果返回給internet上請求連接的客戶端,此時代理服務器對外就表現爲一個服務器。本文使用squid做web的代理服務器
實驗環境
(1)網絡拓撲
(2)IP地址規劃
主機名 | IP地址 |
---|---|
squid | ens33:192.168.7.128,ens36:192.168.10.1 |
web1 | 192.168.7.129 |
web2 | 192.168.7.134 |
客戶端(win10) | 192.168.10.10 |
實驗過程
1、squid安裝
(1)安裝環境包
[root@squid ~]# yum install gcc gcc-c++ -y
(2)編譯安裝squid安裝包
[root@squid squid]# tar zxvf squid-3.4.6.tar.gz -C /opt
[root@squid squid]# cd /opt/squid-3.4.6/
[root@squid squid-3.4.6]# ./configure \
--prefix=/usr/local/squid //指定安裝路徑
--sysconfdir=/etc //指定配置文件目錄
--enable-arp-acl //啓用acl訪問控制列表功能
--enable-linux-netfilter //內核過濾
--enable-linux-tproxy //支持透明模式
--enable-async-io=100 //io優化
--enable-err-language="Simplify_Chinese" //設定支持語言
--enable-underscore //支持下劃線
--enable-poll //開啓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)創建suid用戶,更改/usr/local/squid/var/的屬主、屬組
[root@squid ~]# useradd -M -s /sbin/nologin squid
[root@squid ~]# chown -R squid.squid /usr/local/squid/var/
(5)更改配置文件
[root@squid ~]# vim /etc/squid.conf
http_access allow all
#http_access deny all
#添加指定用戶
cache_effective_user squid
#添加指定組
cache_effective_group squid
#容災備份目錄(此項不需修改)
coredump_dir /usr/local/squid/var/cache/squid
(6)檢查配置文件語法
[root@squid ~]# squid -k parse
(7)初始化緩存目錄
[root@squid ~]# squid -z
(8)啓動服務
[root@squid ~]# squid
(9)創建squid管理腳本
[root@squid ~]# 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..."
$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..."
$0 start &> /dev/null
echo "正在啓動 squid..."
;;
check)
$CMD -K parse
;;
*)
echo "用法: $0{start|stop|status|reload|check|resatrt}"
esac
[root@squid init.d]# chmod +x squid
[root@squid init.d]# chkconfig --add squid
[root@squid init.d]# chkconfig --level 35 squid on
2、web服務器配置
[root@web1 ~]# yum install httpd -y
[root@web1 ~]# cd /var/www/html/
[root@web1 html]# vim index.html
this is web1
[root@web1 html]# systemctl stop firewalld.service
[root@web1 html]# setenforce 0
[root@web1 html]# systemctl start httpd
#配置默認路由
[root@web1 html]# route add -net 192.168.10.0/24 gw 192.168.7.128
3、配置反向代理
(1)開啓路由轉發功能
[root@localhost ~]# echo "net.ipv4.ip_forward=1" >> /etc/sysctl.conf
[root@localhost ~]# sysctl -p
net.ipv4.ip_forward = 1
(2)設置防火牆規則
[root@localhost ~]# iptables -F
[root@localhost ~]# iptables -t nat -F
[root@localhost ~]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
(3)更改squid配置文件,設置代理功能
[root@squid ~]# vim /etc/squid.conf
http_port 192.168.7.128:80 accel vhost vport
cache_peer 192.168.7.129 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web1
cache_peer 192.168.7.128 parent 80 0 no-query originserver round-robin max_conn=30 weight=1 name=web2
cache_peer_domain web1 web2 www.yun.com
[root@localhost ~]# service squid restart
正在關閉 squid...
正在啓動 squid...
[root@localhost ~]# netstat -natp | grep 80
tcp 0 0 192.168.7.128:80 0.0.0.0:* LISTEN 40233/(squid-1)
4、客戶端配置
(1)設置IP
(2)添加代理
5、訪問測試
(1)在客戶端使用瀏覽器打開www.yun.com
(2)使用web服務器查看訪問日誌
[root@web1 html]# tail -f /var/log/httpd/access_log
192.168.7.128 - - [18/Mar/2020:23:25:31 +0800] "GET / HTTP/1.1" 200 22 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"
192.168.7.128 - - [18/Mar/2020:23:25:34 +0800] "GET / HTTP/1.1" 200 22 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36"