squid缓存服务器——传统模式


两台服务器

传统模式中,客户端知道自己是一个代理

透明模式中,不需要对客户端进行设置

一:web代理的工作机制

缓存网页对象,减少重复请求
在这里插入图片描述

二:代理的基本类型

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

三:使用代理的好处

  • 提高web’访问速度
  • 隐藏客户机的真实IP地址

四:实操演示传统代理

web端IP:192.168.247.160
squid端IP:192.168.247.206
win10客户端IP:192.168.247.200

4.1 修改主机名,便于识别

[root@lamp ~]# hostnamectl set-hostname squid
[root@lamp ~]# su
[root@squid ~]# 
[root@nginx ~]# hostnamectl set-hostname web
[root@nginx ~]# su
[root@web ~]# 

备注:squid为了缓存页面对象,需要设置缓存空间,后面会设置

4.2 编译安装squid

  • 首先解压squid,安装编译工具
[root@squid ~]# mkdir /abc
mkdir: cannot create directory ‘/abc’: File exists
[root@squid ~]# mount.cifs //192.168.254.10/linuxs /abc
Password for root@//192.168.254.10/linuxs:  
[root@squid ~]# cd /abc
[root@squid abc]# tar zxvf squid-3.4.6.tar.gz -C /opt
[root@squid abc]# cd /opt
[root@squid opt]# ls
mysql-5.7.17  rh  squid-3.4.6
[root@squid opt]# cd squid-3.4.6/
[root@squid squid-3.4.6]# ls
acinclude     configure     CREDITS  INSTALL      QUICKSTART         src
aclocal.m4    configure.ac  doc      lib          README             test-suite
bootstrap.sh  contrib       errors   libltdl      RELEASENOTES.html  tools
cfgaux        CONTRIBUTORS  helpers  Makefile.am  scripts
ChangeLog     COPYING       icons    Makefile.in  snmplib
compat        COPYRIGHT     include  po4a.conf    SPONSORS
[root@squid squid-3.4.6]# yum install gcc gcc-c++ make pcre* perl* -y

  • configure 配置,make安装
    acl 防控制列表,可在acl中设置通过mac地址进行管理,防止IP七篇

netfilter 内核过滤

tproxy 支持透明模式

io 对于io的优化,异步I/O,提升存储性能

err-language 报错信息设置为中文

underscore 允许url中支持下划线

poll 提升功能性,使用poll()模式

gnuregex 支持正则表达式gnu

[root@squid squid-3.4.6]# ./configure --prefix=/usr/local/squid \
--sysconfdir=/etc \
--enable-arp-acl \
--enable-linux-netfilter \
--enable-linux-tproxy \
--enable-async-io=100 \
--enable-err-language="Simplify_Chinese" \
--enable-underscore \
--enable-poll \
--enable-gnuregex
[root@squid squid-3.4.6]# make && make install
[root@squid squid-3.4.6]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/ 	//优化命令路径
[root@squid squid-3.4.6]# useradd -M -s /sbin/nologin squid					//创建squid程序用户
[root@squid squid-3.4.6]# chown -R squid.squid /usr/local/squid/var/		//给程序用户squid的程序目录的权限
[root@squid squid-3.4.6]# vim /etc/squid.conf			//编辑配置文件
 55 # And finally deny all other access to this proxy
 56 http_access allow all
 #允许所有,关闭拒绝所有,若是拒绝所有,需要放到配置文件的最下方,否则会自deny生效起,后面的参数都不会生效
 57 #http_access deny all
 58 
 59 # Squid normally listens to port 3128
 60 http_port 3128
 61 cache_effective_user squid	
 #增加用户和组
 62 cache_effective_group squid
[root@squid squid-3.4.6]# squid -k parse
//验证语法
[root@squid squid-3.4.6]# squid -z
//初始化缓存,然后squid启用
[root@squid squid-3.4.6]# squid 		//启动
[root@squid squid-3.4.6]# netstat -natp | grep 3128
tcp6       0      0 :::3128                 :::*                    LISTEN      71471/(squid-1)  

4.3 创建systemctl 启动脚本

[root@squid squid-3.4.6]# 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..."
  ;;
  reload)
    $CMD -k reconfigure
  ;;
  check)
    $CMD -k parse
  ;;
  *)
    echo "用法: $0(start|stop|status|reload|check|restart)"
  ;;
esac

4.4 给脚本执行权限,加入到chkconfig中

[root@squid init.d]# chmod +x squid 
[root@squid init.d]# chkconfig --add squid 
[root@squid init.d]# chkconfig --level 35 squid on
[root@squid init.d]# netstat -natp | grep 3128
[root@squid init.d]# service squid start
正在启动 squid...
[root@squid init.d]# netstat -natp | grep 3128
tcp6       0      0 :::3128                 :::*                    LISTEN      102925/(squid-1)    
[root@squid init.d]# service squid stop
[root@squid init.d]# netstat -natp | grep 3128
[root@squid init.d]# setenforce 0

4.5 设置缓存参数

[root@squid init.d]# vim /etc/squid.conf
http_port 3128
cache_effective_user squid
cache_effective_group squid
//增加下面三行
cache_mem 64 MB			//缓存64M的内容
reply_body_max_size 10 MB		//禁止下载的超过10MB的文件
maximum_object_size 4096 KB		//超过4MB的文件不进行缓存

备注:增加的参数放在http_access deny all 前面才会生效
在这里插入图片描述

4.6 设置iptables规则

[root@squid init.d]# iptables -F
//清空规则
[root@squid init.d]# iptables -L
//查看规则
[root@squid init.d]# iptables -t nat -F
[root@squid init.d]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
[root@squid init.d]# service squid reload
[root@squid init.d]# netstat -natp | grep 3128
tcp6       0      0 :::3128                 :::*                    LISTEN      17863/(squid-1)     

4.7 接下来配置web端和客户端

[root@web ~]# systemctl stop firewalld
[root@web ~]# setenforce 0
[root@web ~]# yum install httpd -y
[root@web ~]# netstat -natp | grep 80
[root@web ~]# systemctl start httpd
[root@web ~]# netstat -natp | grep 80
tcp6       0      0 :::80                   :::*                    LISTEN      128136/httpd 

打开一台虚拟机win10,去访问web

在这里插入图片描述

4.8 查看web端httpd的访问日志

[root@web httpd]# cd /var/log/httpd/
[root@web httpd]# cat access_log 
192.168.247.200 - - [02/Feb/2020:10:58:32 +0800] "GET /noindex/css/fonts/Semibold/OpenSans-Semibold.ttf HTTP/1.1" 404 246 "http://192.168.247.160/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.10240"

访问ip为:192.168.247.200

此时并没有设置代理

4.9 开始配置代理

在谷歌浏览器中打开设置,点击高级

在这里插入图片描述

点击系统,打开您计算机的代理设置

在这里插入图片描述

打开手动设置代理
在这里插入图片描述
在这里插入图片描述

输入代理服务器ip和端口号,点击保存,然后关闭,关闭浏览器

重新打开浏览器,再次访问web
在这里插入图片描述

4.10 再次查看httpd日志

[root@web httpd]# cat access_log 
192.168.247.206 - - [02/Feb/2020:11:10:47 +0800] "GET /noindex/css/fonts/Light/OpenSans-Light.woff HTTP/1.1" 404 241 "http://192.168.247.160/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"

此时可以发现访问ip变成了192.168.247.206

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值