squid代理服务器

本文详细介绍了如何编译安装Squid代理服务器,并设置了传统代理、透明代理和反向代理。在传统代理中,调整了缓存空间、最大下载容量和对象大小限制。透明代理配置涉及双网卡、路由转发和iptables规则。反向代理则用于负载均衡,通过配置文件实现了访问特定域名时的流量分配。整个过程包括了防火墙规则设置、服务管理脚本创建以及客户端配置,确保了代理服务器的正常运行和访问效率。
摘要由CSDN通过智能技术生成

缓存代理概述

基本类型

传统类型:适用于lnternet,需明确指定服务器
透明代理:客户机不需要指定代理服务器的地址和端口,是通过默认路由、防火墙策略将web访问重新定向给代理服务器处理

使用代理的好处:提高web访问速度、隐藏客户机真实IP地址

在这里插入图片描述
dns解析的是vip地址还是直接访问后端服务器只不过会自动调度给squid代理服务器,如果没有资源squid代理会向后端发送请求资源在传给客户机,下次再次连接,就直接在squid代理上拿资源,静态资源存在缓存空间,动态资源还是存在后端的tomcat,如果超出缓存空间限制还是找后端nginx拿静态资源,这用到了动静分离技术。需要监控日志就用到了ELK,mysql高可用集群,web集群。

编译安装squid代理服务器

端口号 3128 20.0.0.43 apche服务器,20.0.0.44 代理服务器
拖入压缩包到opt下然后解压

[root@localhost squid-3.4.6]# tar zxvf squid-3.4.6.tar.gz 

./configure解压
[root@localhost squid-3.4.6]# cd squid-3.4.6/
[root@localhost squid-3.4.6]# ./configure --prefix=/usr/local/squid \ ##安装路径
> --sysconfdir=/etc \  ##配置文件所在目录
> --enable-arp-acl \  ##访问控制列表
> --enable-linux-netfilter \  ##内核过滤
> --enable-linux-tproxy \  ##支持透明模式
> --enable-async-io=100 \  ##io优化
> --enable-err-language="Simplify_Chinese" \  ##支持报错语言形式
> --enable-underscore \  ##支持下划线
> --enable-poll \  ##poll功能提升
> --enable-gnuregex  ##支持正则表达式

make编译
make && make install

做一个软连接让squid的命令能够被系统识别需要添加到usr/local/bin

[root@localhost squid-3.4.6]# ln -s /usr/local/squid/sbin/ /usr/local/bin

建立一个程序性用户管理squid

[root@localhost squid-3.4.6]# useradd -M -s /sbin/nologin squid
赋予目录权限,var下有个run会跑squid里面的服务
[root@localhost squid-3.4.6]# chown -R squid.squid /usr/local/squid/var/

修改配置文件

[root@localhost squid-3.4.6]# vi /etc/squid.conf
56 http_access allow all  ##改为allow all允许所有访问
59 http_port 3128 下添加内容
cache_effective_user squid  ##缓存管理用户和组
cache_effective_group squid
wq保存退出
检查语法是否有错等同于niginx -t
[root@localhost sbin]# squid -k parse
没有问题就初始化缓存
[root@localhost sbin]# squid -z
[root@localhost sbin]# 2020/10/30 11:55:53 kid1| Set Current Directory to /usr/local/squid/var/cache/squid
2020/10/30 11:55:53 kid1| Creating missing swap directories
2020/10/30 11:55:53 kid1| No cache_dir stores are configured

接下来就可以启动服务了

[root@localhost sbin]# squid
检查一下端口
[root@localhost sbin]# netstat -anpt|grep 3128
tcp6       0      0 :::3128                 :::*                    LISTEN      36876/(squid-1) 

为了便于管理把他放入service中

[root@localhost sbin]# cd /etc/init.d/
[root@localhost init.d]# vim squid  ##创建管理脚本
#!/bin/bash
#chkconfig:2345 90 25
PID="/usr/local/squid/var/run/squid.pid"   ##PID文件保存squid运行进程号
CONF="/etc/squid.conf"  ##主配置文件
CMD="/usr/local/squid/sbin/squid"   ##启动命令

case "$1" in
        start)
           netstat -anpt|grep squid &> /dev/null   ##查看过滤3128端口
           if [ $? -eq 0 ];then   ##返回值是0就是已经启动了非0就运行启动squid
                echo "squid is runing"
                else
                echo "正在启动squid..."
                $CMD
           fi
        ;;
        stop)
           $CMD -k kill &> /dev/null  ##kill掉进程
           rm -rf $PID &> /dev/null  ##删除PID文件
        ;;
        status)
           [ -f $PID ] &> /dev/null  ##查看状态PID文件是否存在
                if [ $? -eq 0 ];then  ##存在返回值为0
                   netstat -anpt|grep squid
                else
                   echo "squid is not running"
                fi
        ;;
        restart)   ##先执行stop在执行start
           $0 stop &> /dev/null
           echo "正在关闭squid..."
                $0 start &> /dev/null
           echo "正在启动squid..."
        ;;
        reload)  ##加载配置文件
           $CMD -k reconfigure
        ;;
        check)  ##检查语法
           $CMD -k parse
        ;;
        *)  ##输入用法
        echo "用法 $0[start|stop|status|reload|check|restart]
"
        ;;
esac
wq保存退出
给执行脚本权限
[root@localhost init.d]# chmod +x squid 
添加service列表中添加名称
[root@localhost init.d]# chkconfig --add squid
添加3 和 5 模式能开机自启动

测试

[root@localhost init.d]# chkconfig --level 35 squid on
[root@localhost init.d]# service squid stop
[root@localhost init.d]# netstat -anpt |grep 3128
[root@localhost init.d]# service squid start
正在启动squid...
[root@localhost init.d]# netstat -anpt |grep 3128
tcp6       0      0 :::3128                 :::*                    LISTEN      37237/(squid-1)  

传统代理

设置选项:缓存空间、允许用户下载最大容量、缓存中存储对象上限值(超出不会存,直接发往用户端)
进入配置文件

[root@localhost squid-3.4.6]# vi /etc/squid.conf
接着3128端口下继续插入
cache_mem 64 MB    ##缓存空间
reply_body_max_size 10 MB  ##允许用户下载最大容量
maximum_object_size 4096 KB  ##缓存中存储对象上限值
wq保存退出

清空防火墙和关闭核心防护添加新的规则允许代理

[root@localhost init.d]# iptables -F  ##清空防火墙规则
关闭核心防护
[root@localhost init.d]# setenforce 0
setenforce: SELinux is disabled
自己设定防火墙规则:规则---链---表
定义到指定的链中,没有清空一定要加
INPUT:定义到转发表、-p 协议、--dport 目标端口、-j ACCEPT 允许访问
[root@localhost init.d]# iptables -I INPUT -p tcp --dport 3218 -j ACCEPT
重载配置文件
[root@localhost init.d]# service squid reload

之后就是客户端中的配置
首先底层防护关闭,下载apache

[root@localhost opt]# yum -y install httpd

然后启动服务

[root@localhost opt]# systemctl start httpd

去主机中打开网络设置添加代理地址和端口
20.0.0.44 3128
然后在apche那台主机上cd进入access.log文件查看20.0.0.43登录访问的是不是44,是就成功了

[root@localhost httpd]# cd /var/log/httpd/
[root@localhost httpd]# ls
access_log  error_log

查看access日志文件

20.0.0.44 - - [30/Oct/2020:13:57:10 +0800] "GET /noindex/css/fonts/Light/OpenSans-Light.ttf HTTP/1.1" 404 240 "http://20.0.0.43/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"

提示是代理服务器44进行的成功。

透明代理

需要设置双网卡
需要做转发要改/etc/sysctl.conf
加net.ipv4.ip_forward=1开启路由功能
添加一块VM1网卡192.168.100网段
进入网卡配置目录然后复制ens33为ens36然后进入修改配置

[root@localhost init.d]# cd /etc/sysconfig/network-scripts/
[root@localhost network-scripts]# cp -p ifcfg-ens33 ifcfg-ens36
[root@localhost network-scripts]# vi ifcfg-ens36
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens36
DEVICE=ens36
ONBOOT=yes
NETMASK=255.255.255.0
IPADDR=192.168.100.5
DNS1=8.8.8.8
DNS2=114.114.114.114
wq保存刷新网卡
[root@localhost network-scripts]# systemctl restart network

查看网卡

[root@localhost network-scripts]# ifconfig 
ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.5  netmask 255.255.255.0  broadcast 192.168.100.255

开启路由转发

[root@localhost network-scripts]# vi /etc/sysctl.conf 
最后插入
net.ipv4.ip_forward=1
然后重新加载
[root@localhost network-scripts]# sysctl -p
net.ipv4.ip_forward = 1
有来有回需要在配置一条路由条目给他指一条路指向另一张网卡
[root@localhost network-scripts]# route add -net 192.168.100.
0/24 gw 20.0.0.44

配置squid配置文件
指明从那个入口进入访问
vi /etc/squid.conf
59 http_port 3128 监听端口修改成入口地址改为,http_port 192.168.100.5:3128 transparent
然后重载服务

[root@localhost network-scripts]# service squid stop
[root@localhost network-scripts]# service squid start
正在启动squid...

然后要加入iptable规则,允许访问

prerouting面添加规则如网卡36网卡源地址是192.168.100段tcp协议重定向到3128,访问80实际访问3128
[root@localhost network-scripts]# iptables -t nat -I PREROUTING -i ens36 -s 192.168.100.0/24 -p tcp --dport 80 -j REDIRECT --to 3128
如果访问的是https 433口
[root@localhost network-scripts]# iptables -t nat -I PREROUTING -i ens36 -s 192.168.100.0/24 -p tcp --dport 443 -j REDIRECT --to 3128
从3128input链进行访问
[root@localhost network-scripts]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT

设置完成后检查一下端口3128

[root@localhost network-scripts]# netstat -anpt|grep 3128
tcp        0      0 192.168.100.5:3128      0.0.0.0:*               LISTEN      43316/(squid-1)   

查看access

192.168.100.5 - - [30/Oct/2020:15:57:10 +0800] "GET /noindex/css/fonts/Light/OpenSans-Light.ttf HTTP/1.1" 404 240 "http://20.0.0.43/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"

是代理服务器端进行访问,成功。

反向代理

修改squid配置文件59行及其往下插入

[root@localhost network-scripts]# vi /etc/squid.conf
59 http_port 192.168.100.5:3128 transparent
http_port 20.0.0.44:80 accel vhost vport
下面插入的是重定向找80端口,地址是两台web的自己的地址
cache_peer 20.0.0.41 parent 80 0 no-query originserver round-robin max_conn-30 wcight-l name-web1
cache_peer 20.0.0.40 parent 80 0 no-query originserver round-robin max_conn-30 wcight-l name-web2
匹配域名访问访问yuheng就等于访问3128
cache_peer_domain web1 web2 www.yuheng.com

然后重启服务

[root@localhost network-scripts]# service squid stop
[root@localhost network-scripts]# service squid start
正在启动squid...

之后去测试两个都能访问。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值