前言
反向代理,是指以代理服务器来接受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"