Squitd代理服务器安装

主机部署

@1  192.168.11.11主机代理服务器squid
@2 192.168.11.12互联网web服务器httpd
@3  192.168.11.13linux客户端elinks
win 10 客户机 

squid的代理机制

应用层的代理服务软件, Squid 主要提供缓存加速、 应用层过滤控制的功能。

 

首先客户端client向squid代理服务器发送一个请求,代理服务器是没有其他服务的,比如,客户端请求163这个服务,此时代理服务器并没有163这个web服务,通过接受客户端80端口的请求,如果代理服务器上有其他机器请求的163web服务,那么代理服务器会有一个缓存叫cashe,缓存在内存里面,cashe可以缓存一些静态元素,如果html,text文本,图片等等,如果有的话,直接返回给客户机,如果没有,代理服务器代理客户机去请求163,163返回结果给代理服务器,再由代理服务器返回给客户端。

代理分为传统代理与透明代理

 

@1需要编译环境

[root@node11 ~]# yum -y install gcc gcc-c++
已加载插件:fastestmirror, langpacks
local                                                        | 3.6 kB  00:00:00     
Loading mirror speeds from cached hostfile
软件包 gcc-4.8.5-11.el7.x86_64 已安装并且是最新版本
软件包 gcc-c++-4.8.5-11.el7.x86_64 已安装并且是最新版本
无须任何处理

@·1这里我是直接在官网下载好的包,日志分析包sarg-2.3.7.tar.gz 和 squid-3.5.23.tar.gz

[root@node11 ~]# ls
anaconda-ks.cfg  init.sh  sarg-2.3.7.tar.gz  squid-3.5.23.tar.gz
[root@node11 ~]# tar xf squid-3.5.23.tar.gz
[root@node11 ~]# cd squid-3.5.23/
[root@node11 squid-3.5.23]# ./configure --prefix=/usr/local/squid --enable-linux-netfilter --enable-async-io=240 --enable-default-err-language=Simplify_Chinese --disable-poll --enable-epoll --enable-gnuregex
[root@node11 squid-3.5.23]# make && make install
[root@node11 squid-3.5.23]# echo $?
0
[root@node11 squid-3.5.23]# ln -s /usr/local/squid/sbin/squid /usr/local/sbin/
[root@node11 squid-3.5.23]# useradd -r -s /sbin/nologin squid
[root@node11 squid-3.5.23]# chown -R squid:squid /usr/local/squid/var

 

@2配置安装httpd

[root@node12 ~]# yum -y install  httpd
已加载插件:fastestmirror, langpacks
local                                                        | 3.6 kB  00:00:00     
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 httpd.x86_64.0.2.4.6-45.el7.centos 将被 安装
--> 正在处理依赖关系 httpd-tools = 2.4.6-45.el7.centos,它被软件包 httpd-2.4.6-45.el7.centos.x86_64 需要
--> 正在处理依赖关系 /etc/mime.types,它被软件包 httpd-2.4.6-45.el7.centos.x86_64 需要
--> 正在处理依赖关系 libaprutil-1.so.0()(64bit),它被软件包 httpd-2.4.6-45.el7.centos.x86_64 需要
--> 正在处理依赖关系 libapr-1.so.0()(64bit),它被软件包 httpd-2.4.6-45.el7.centos.x86_64 需要
--> 正在检查事务
---> 软件包 apr.x86_64.0.1.4.8-3.el7 将被 安装
---> 软件包 apr-util.x86_64.0.1.5.2-6.el7 将被 安装
---> 软件包 httpd-tools.x86_64.0.2.4.6-45.el7.centos 将被 安装
---> 软件包 mailcap.noarch.0.2.1.41-2.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

====================================================================================
 Package             架构           版本                        源             大小
====================================================================================
正在安装:
 httpd               x86_64         2.4.6-45.el7.centos         local         2.7 M
为依赖而安装:
 apr                 x86_64         1.4.8-3.el7                 local         103 k
 apr-util            x86_64         1.5.2-6.el7                 local          92 k
 httpd-tools         x86_64         2.4.6-45.el7.centos         local          84 k
 mailcap             noarch         2.1.41-2.el7                local          31 k

事务概要
====================================================================================
安装  1 软件包 (+4 依赖软件包)

总下载量:3.0 M
安装大小:10 M
Downloading packages:
------------------------------------------------------------------------------------
总计                                                    30 MB/s | 3.0 MB  00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : apr-1.4.8-3.el7.x86_64                                          1/5 
  正在安装    : apr-util-1.5.2-6.el7.x86_64                                     2/5 
  正在安装    : httpd-tools-2.4.6-45.el7.centos.x86_64                          3/5 
  正在安装    : mailcap-2.1.41-2.el7.noarch                                     4/5 
  正在安装    : httpd-2.4.6-45.el7.centos.x86_64                                5/5 
  验证中      : httpd-tools-2.4.6-45.el7.centos.x86_64                          1/5 
  验证中      : mailcap-2.1.41-2.el7.noarch                                     2/5 
  验证中      : apr-1.4.8-3.el7.x86_64                                          3/5 
  验证中      : httpd-2.4.6-45.el7.centos.x86_64                                4/5 
  验证中      : apr-util-1.5.2-6.el7.x86_64                                     5/5 

已安装:
  httpd.x86_64 0:2.4.6-45.el7.centos                                                

作为依赖被安装:
  apr.x86_64 0:1.4.8-3.el7                       apr-util.x86_64 0:1.5.2-6.el7      
  httpd-tools.x86_64 0:2.4.6-45.el7.centos       mailcap.noarch 0:2.1.41-2.el7      

完毕!

@1修改配置文件

[root@node11 squid-3.5.23]# vim /usr/local/squid/etc/squid.conf
cache_dir ufs /usr/local/squid/var/cache/squid 100 16 256      #开启,cache存放的路径,ufs 类型 使用的缓冲值最大允午100MB空间,16个 一级目录,256个二级目录
cache_effective_user squid 
cache_effective_group squid 
visible_hostname node11

@1开启squid服务

#检查配置文件语法 
[root@node11 squid-3.5.23]#squid -k parse # 解析配置文件 初始化 
内容过多,就不显示了
[root@node11 squid-3.5.23]#squid -z # 根据配置文件重建缓存 
内容过多,就不显示了
[root@node11 squid-3.5.23]#squid         #    启动
[root@node11 squid-3.5.23]# ss -ntl        #开启端口3128
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128            *:22                         *:*                  
LISTEN     0      100    127.0.0.1:25                         *:*                  
LISTEN     0      128           :::22                        :::*                  
LISTEN     0      128           :::3128                      :::*                  
LISTEN     0      100          ::1:25                        :::*                  
[root@node11 squid-3.5.23]# 

 

@2开启HTTP服务

[root@node12 ~]# 
[root@node12 ~]# echo "`hostname` `hostname -I` "  > /var/www/html/index.html
[root@node12 ~]# systemctl enable --now httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@node12 ~]# ss -tnl
State      Recv-Q Send-Q Local Address:Port               Peer Address:Port              
LISTEN     0      128            *:22                         *:*                  
LISTEN     0      100    127.0.0.1:25                         *:*                  
LISTEN     0      128           :::80                        :::*                  
LISTEN     0      128           :::22                        :::*                  
LISTEN     0      100          ::1:25                        :::*                  
[root@node12 ~]# curl localhost
node12 192.168.11.12  

验证代理服务器

@3下载命令行终端浏览器

[root@node13 ~]# yum -y install  elinks
已加载插件:fastestmirror, langpacks
local                                                        | 3.6 kB  00:00:00     
Loading mirror speeds from cached hostfile
正在解决依赖关系
--> 正在检查事务
---> 软件包 elinks.x86_64.0.0.12-0.36.pre6.el7 将被 安装
--> 正在处理依赖关系 libnss_compat_ossl.so.0()(64bit),它被软件包 elinks-0.12-0.36.pre6.el7.x86_64 需要
--> 正在处理依赖关系 libmozjs185.so.1.0()(64bit),它被软件包 elinks-0.12-0.36.pre6.el7.x86_64 需要
--> 正在检查事务
---> 软件包 js.x86_64.1.1.8.5-19.el7 将被 安装
---> 软件包 nss_compat_ossl.x86_64.0.0.9.6-8.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

====================================================================================
 Package                架构          版本                       源            大小
====================================================================================
正在安装:
 elinks                 x86_64        0.12-0.36.pre6.el7         local        882 k
为依赖而安装:
 js                     x86_64        1:1.8.5-19.el7             local        2.3 M
 nss_compat_ossl        x86_64        0.9.6-8.el7                local         37 k

事务概要
====================================================================================
安装  1 软件包 (+2 依赖软件包)

总下载量:3.2 M
安装大小:9.6 M
Downloading packages:
------------------------------------------------------------------------------------
总计                                                    19 MB/s | 3.2 MB  00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : nss_compat_ossl-0.9.6-8.el7.x86_64                              1/3 
  正在安装    : 1:js-1.8.5-19.el7.x86_64                                        2/3 
  正在安装    : elinks-0.12-0.36.pre6.el7.x86_64                                3/3 
  验证中      : 1:js-1.8.5-19.el7.x86_64                                        1/3 
  验证中      : elinks-0.12-0.36.pre6.el7.x86_64                                2/3 
  验证中      : nss_compat_ossl-0.9.6-8.el7.x86_64                              3/3 

已安装:
  elinks.x86_64 0:0.12-0.36.pre6.el7                                                

作为依赖被安装:
  js.x86_64 1:1.8.5-19.el7           nss_compat_ossl.x86_64 0:0.9.6-8.el7          

完毕!

@win10中设置代理 

个性化设置>>主页>>网络和internet>>代理>>开启使用代理服务器

访问后返回12首页,

@2上追踪下访问日志

@1上代理日志信息

以上是传统代理以及验证

 

Linux 客户机的命令行界面中使用代理服务器,必须通过环境变量来指定代理服务器的地址、端口等信息。

@3Linux客户端,使用代理服务器,并生效

[root@node13 ~]# vim /etc/profile.d/proxy.sh
export HTTP_PROXY=http://192.168.11.11:3128 
export HTTPS_PROXY=http://192.168.11.11:3128 
export FTP_PROXY=http://192.168.11.11:3128 
export NO_PROXY=192.168.1.,192.168.2.
[root@node13 ~]# . /etc/profile.d/proxy.sh
[root@node13 ~]# elinks 192.168.11.12      #显示信息在下图

这时敲空格得到的时浏览界面

以上就是传统代理,Windows浏览器访问和Linux客户端的访问过程

透明代理

 

透明代理提供的服务功能与传统代理是一致的,但是其 透明 的实现依赖于默认路由和防火墙的重定向策略,因此更适用于为局域网主机服务,而不 适合为 Internet 中的客户机提供服务

透明代理需要两个网段。一个网段也行,这里就做两个网段处理

@1添加网卡,内网和外网

@win10   改为22的网段

@3   改网卡为22网段

@3改网卡

[root@node13 ~]# vim  /etc/sysconfig/network-scripts/ifcfg-eth0 
DEVICE=eth0
NAME=eth0
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.22.13
NETMASK=255.255.255.0
GATEWAY=192.168.22.11
[root@node13 ~]# systemctl  restart  network

@1添加网卡


[root@node11 squid-3.5.23]#  cd /etc/sysconfig/network-scripts/
[root@node11 network-scripts]# cat ifcfg-eth1
DEVICE=eth1
NAME=eth1
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=static
IPADDR=192.168.22.11
NETMASK=255.255.255.0
[root@node11 network-scripts]# systemctl restart network
[root@node11 ~]# vim /usr/local/squid/etc/squid.conf
 59 http_port 3128  transparent   #在监听端口后加上transparent就可以实现透明话了
[root@node11 ~]# squid -kche        #检测配置
[root@node11 ~]# squid -krec       #重载
[root@node11 ~]# echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
[root@node11 ~]# sysctl -p              #开启转发
net.ipv4.ip_forward = 1

Squid的透明代理中需要开启防火墙策略, 这样才可以将局域网主机访问Internet 的数据包转交给 Squid进行处理

@1做iptables 防火墙celue

[root@node11 ~]# iptables -vnL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@node11 ~]# iptables -vnL  -t nat
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
 pkts bytes target     prot opt in     out     source               destination         
[root@node11 ~]# iptables -t nat -A PREROUTING -i eth1 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 3128
#如果是其他服务需要改下对应的端口号,若为多个服务,需在加一个策略

这时访问Windows刷新

 

@3这是Linux客户端要把刚刚做的设置清一下 ,显示信息入下图

[root@node13 ~]# unset HTTP_PROXY HTTPS_PROXY
[root@node13 ~]# elinks 192.168.11.12

以上为透明代理

 

ACL 访问控制

合理设置 ACL并进行限制, 可以针对访问的 各种条件进行过滤。

 

定义访问控制列表
 
acl 列表名称 列表类型 列表内容

 

通过黑名单限制目标网站
 
[root@node11 ~]# vim /usr/local/squid/etc/ipblock.list
192.168.22.13
[root@node11 ~]# cat /usr/local/squid/etc/dmblock.list
.qq.com
[root@node11 ~]#  vim /usr/local/squid/etc/squid.conf
acl IPBLOCK src  "/usr/local/squid/etc/ipblock.list"
acl DMBLOCK dstdomain   "/usr/local/squid/etc/dmblock.list"
http_access     deny     IPBLOCK DMBLOCK
[root@node11 ~]# squid -kche
[root@node11 ~]# squid -krec
[root@node11 ~]# 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值