Centos7构建Squid代理服务器——传统代理,透明代理


实验材料:
两台Centos7;一台Win7
一台担任Squid代理服务器:192.168.1.110
一台担任Web网站服务器:192.168.1.10

一台Win担任客户机:192.168.1.20
需要安装Squid软件包:

https://pan.baidu.com/s/1Eex0QDhF_LrL8_qPhwcnXA
提取码:8xhv

Squid相关概述

Squid: Linux系统中一款最常用的开源的代理服务软件,提供缓存代理功能
工作原理:
在这里插入图片描述
当客户机通过代理去访问各种服务时,代理服务器会先检查自己的缓存,缓存中有结果,直接返回;如果缓存中没有用户需要的信息时,代理服务器转给Internet,获取用户需要的信息,保存到缓存中,并返回给用户
代理缓存加速的对象:文字、图像等静态的Web元素
优势:提高Web访问速度;隐藏客户机的IP地址,还可针对访问目标、时间等进行访问控制(过滤机制)

代理的类型:
(一)、传统代理: 需要在客户端去指定代理服务器 (多用于Internet 环境)
(二)、透明代理:不需要指定代理服务器的地址和端口 (局域网环境,无需额外设置可实现优质上网)

构建Squid 代理服务器

(一)、Squid 的安装及运行控制
1、编译安装Squid

[root@localhost ~]# mount /dev/cdrom /media/cdrom  (挂载光盘)
[root@localhost ~]# tar zxf squid-3.5.23.tar.gz    (解压squid软件包)
配置:
[root@localhost ~]# cd squid-3.5.23/
[root@localhost squid-3.5.23]# ./configure \
--prefix=/usr/local/squid \
--sysconfdir=/etc \
--enable-linux-netfilter \
--enable--linux-tproxy \
--enable-async-io=240 \
--enable-default-err-language=Simplify_Chinese \
--disable-poll \
--enable-epoll  \
--enable-gnuregex
  • –prefix=/usr/local/squid 这里为squid的安装目录
  • –sysconfdir=/etc 配置文件目录
  • –enable-linux-netfilter 使用内核过滤
  • –enable-async-io=240 缓存文件存取机制。异步i/o,提升存储性能
  • –enable-err-language=“Simplify_Chinese” 错误警告输出中文
  • –disable-poll与–enable-epoll 关闭默认使用poll模式,开启epoll模式提升性能
  • –enable-gnuregex 使用gnu正则表达式
[root@localhost squid-3.5.23]# make && make install   (编译安装)
[root@localhost squid-3.5.23]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/  (创建软链接,方便系统识别)
[root@localhost squid-3.5.23]# useradd -M -s /sbin/nologin squid       (添加squid用户)
[root@localhost squid-3.5.23]# chown -R squid:squid /usr/local/squid/var/   (设置属主和属组)
[root@localhost squid-3.5.23]# cd  (后退)

在这里插入图片描述
2、修改squid 配置文件 (/etc/squid.conf)

[root@localhost ~]# vim /etc/squid.conf
添加:
cache_effective_user squid     (指定squid 的程序用户,用来设置初始化、运行时缓存的账户)
cache_effective_group squid    (指定squid 的程序组,用来设置初始化、运行时缓存的组账户)

在这里插入图片描述
3、squid的运行控制
1)、检查配置文件语法是否正确: squid -k parse
2)、启动、停止Squid
第一次启动时,会自动初始化缓存目录 :

[root@localhost ~]# squid  -z   (初始化缓存目录)
[root@localhost ~]# squid       (启动squid 服务)
[root@localhost ~]# netstat -anpt | grep squid   (确认服务的监听状态)
(squid 服务默认监听TCP 3128 端口)

在这里插入图片描述
3)、为了方便对squid 服务的启动、停止、重新加载更加方便,需要编写squid 服务脚本,并使用chkconfig 和 systemctl 工具进行管理)

[root@localhost ~]# vim  /etc/init.d/squid
添加:
#!/bin/bash
# chkconfig: 35 90 25
# config: /etc/squid.conf
# pidfile: /usr/local/squid/var/run/squid.pid
# Description: Squid - Internet Object Cache.
PID="/usr/local/squid/var/run/squid.pid"
CONF="/etc/squid.conf"
CMD="/usr/local/squid/sbin/squid"
case "$1" in
        start)
                netstat -anpt|grep squid &> /dev/null
                if [ $? -eq 0 ];then
                        echo "squid is running"
                else
                        echo "starting squid"
                $CMD
                fi
        ;;
        stop)
                $CMD -k kill &> /dev/null
                rm -f $PID &> /dev/null
        ;;
        status)
                 [ -f $PID ] &> /dev/null
                        if [ $? -eq 0 ];then
                                netstat -anpt |grep squid
                        else
                                echo "squid is not running"
                        fi
        ;;
        restart)
                $0 stop &> /dev/null
                echo "stoping squid"
                $0 start &> /dev/null
                echo "starting squid"
        ;;
        reload)
                $CMD -k reconfigure
        ;;
        check)
                $CMD -k parse
        ;;
        *)
                echo "Usage: $0 {start|stop|restart|reload|check|status}"
esac
[root@localhost ~]# chmod +x /etc/init.d/squid       (给服务器脚本可执行权限)
[root@localhost ~]# chkconfig --add squid             (添加为系统服务)
接下来即可使用 systemctl   选项  squid  (对squid 服务进行控制)
[root@localhost ~]# systemctl start squid   (开启squid服务)

在这里插入图片描述

构建Squid传统代理

步骤:
(一)、squid 服务器的配置(192.168.1.110)
修改squid.conf 配置文件

[root@localhost ~]# vim  /etc/squid.conf
添加:
http_access allow all  (允许任意客户机使用代理服务, 添加在“http_access deny all” 之前)
reply_body_max_size 10 MB   (允许下载的最大文件大小是10 MB)
[root@localhost ~]# systemctl restart squid  (重启squid)

在这里插入图片描述
(二)、配置Web网站服务(192.168.1.10)

扫描二维码关注公众号,回复: 11847128 查看本文章
[root@Web-server ~]# mount /dev/cdrom /media/cdrom   (挂载光盘)
[root@Web-server ~]# yum -y install httpd	     (yum安装httpd)
[root@Web-server ~]# echo "<h1>www.ajbn.6666</h1>" > /var/www/html/index.html (给httpd创建默认网页)
[root@Web-server ~]# systemctl start httpd   (启动httpd服务)
[root@Web-server ~]# curl 192.168.1.10       (测试能否成功访问)
<h1>www.ajbn.6666</h1>

在这里插入图片描述
(三)、修改客户机的浏览器设置
打开浏览器——找到工具——Internet选项——连接——局域网设置——开启代理,并指向squid代理服务器
在这里插入图片描述
在这里插入图片描述(四)、使用Win客户机访问验证(192.168.1.20)
用客户机访问web 服务器 (查看Web 服务器的访问日志,只能发现来自代理服务器的访问记录,而发现不了客户机的真实IP)
win7访问查看:
在这里插入图片描述
squid服务器验证:

[root@localhost ~]# tail -f /usr/local/squid/var/logs/access.log

在这里插入图片描述
Web服务器验证:

[root@Web-server ~]# tail -f /var/log/httpd/access_log

在这里插入图片描述

构建Squid透明代理

实验环境:
1.squid服务器上添加两块网卡:
vmnet1:192.168.1.110(内网)
vmnet8:200.1.1.2(外网)
2.外网web服务器:
vmnet8:200.1.1.100 (不指网关)

3.一台win7作为客户端
vmnet1:192.168.1.20 将网关指向squid内网的网关

实验步骤:
可直接使用上面做传统代理的机器,稍作修改即可
在这里插入图片描述
1.Web网站服务器上操作:

[root@Web-server ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33  (编辑ens33配置文件)
[root@Web-server ~]# systemctl restart network   (重启网卡)

在这里插入图片描述
2.squid服务器上操作

[root@localhost ~]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# ls
[root@localhost network-scripts]# cp ifcfg-ens33 ifcfg-ens37  (将ens33配置文件复制到ens37配置文件中)
[root@localhost network-scripts]# vim ifcfg-ens37	      (编辑ens37配置文件)

在这里插入图片描述
在这里插入图片描述

[root@localhost network-scripts]# systemctl restart network  (重启网卡)
[root@localhost network-scripts]# ifconfig ens33	     (查看ens33接口地址)
[root@localhost network-scripts]# ifconfig ens37	     (查看ens37接口地址)
[root@localhost network-scripts]# ping -c 3 200.1.1.100	     (和外网Web服务器测试连通性)
[root@localhost network-scripts]# cd    (后退)

在这里插入图片描述

[root@localhost ~]# vim /etc/squid.conf
修改:
http_port 192.168.1.110:3128 transparent
[root@localhost ~]# systemctl restart squid

在这里插入图片描述
3.在squid上开启路由转发

[root@localhost ~]# vim /etc/sysctl.conf
在最下面添加:
net.ipv4.ip_forward = 1

在这里插入图片描述
4.开启防火墙,添加防火墙规则
ens33----内网网关 ens37----外网接口

[root@localhost ~]# systemctl start firewalld    (启动防火墙)
[root@localhost ~]# iptables -t nat -I PREROUTING -i ens33 -s 192.168.1.0/24 -p tcp --dport 80 -j REDIRECT --to 3128
(将来自ens33接口且是192.168.1.0网段地址访问的tcp协议的80端口的请求转换到3128端口上)
[root@localhost ~]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
(开启3128端口的访问权限)

在这里插入图片描述
测试:
1.Win客户机访问:200.1.1.100(如果你是用上面实验的Win7需要把代理关闭)
在这里插入图片描述
2.squid 服务器验证:

[root@localhost ~]# tail -f /usr/local/squid/var/logs/access.log

在这里插入图片描述
3.外网web服务器验证

[root@Web-server ~]# tail -f /var/log/httpd/access_log 

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_46902396/article/details/108716428