Squid代理服务器应用

缓存代理概述

Web代理的工作机制
·缓存网页对象、减少重复请求

Squid代理服务器应用

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

使用代理的好处
·提高Web访问速度
·隐藏客户机的真实IP


搭建传统代理案例
Squid代理服务器应用

Squid代理服务器应用

Squid代理服务器应用



搭建透明代理案例
Squid代理服务器应用

Squid代理服务器应用

Squid代理服务器应用





搭建传统代理服务器

准备两台主机:
squid代理服务器 centos7.4 IP 20.0.0.31
apache网站服务器 centos7.4 IP 20.0.0.32



//登录20.0.0.31主机
【安装squid服务】
//上传squid-3.4.6.tar.gz 到/opt目录下
[[email protected] ~]#cd /opt
[[email protected] opt]#tar zxvf squid-3.4.6.tar.gz -C /opt
[[email protected] opt]# cd /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

解释:
–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 //url中支持下划线
–enable-poll //启用poll函数
–enable-gnuregex //支持正则表达式


[[email protected] squid-3.4.6]# make && make install

[[email protected] squid-3.4.6]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/
[[email protected] squid-3.4.6]# useradd -M -s /sbin/nologin squid
[[email protected] squid-3.4.6]# chown -R squid.squid /usr/local/squid/var/

[[email protected] squid-3.4.6]# vim /etc/squid.conf
:set nu //显示行数
56 http_access allow all(添加)
57 #http_access deny all(注释掉)
61 cache_effective_user squid(添加)
62 cache_effective_group squid(添加)

Squid代理服务器应用

[[email protected] squid-3.4.6]# squid -k parse //检查配合文件语法
[[email protected] squid-3.4.6]# cd ~
[[email protected] ~]# squid -z //初始化缓存目录
[[email protected] ~]# squid //启动服务
[[email protected] ~]# netstat -antp | grep 3128

[[email protected] ~]# cd /etc/init.d/
[[email protected] 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 -antp | grep squid &> /dev/null
if [ $? -eq 0 ]
then
echo “squid is running”
else
echo “正在启动 squid…”
$CMD
echo “已启动 squid”
fi
;;
stop)
$CMD -k kill &> /dev/null
rm -rf $PID &> /dev/null
;;
status)
[ -f $PID ] &> /dev/null
if [ $? -eq 0 ]
then
netstat -antp | 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

[[email protected] init.d]# chmod +x squid
[[email protected] init.d]# chkconfig --add squid
[[email protected] init.d]# chkconfig --level 35 squid on (如果不想开机自启可以不打这条)



//还是在20.0.0.31主机上
【传统代理服务器】
配置文件:
http_access allow all
http_access deny all
http_port 3128
cache_mem 64 MB //指定缓存功能所使用的内存空间大小,便于保持访问较频繁的WEB对象,容量最好为4的倍数,单位MB
reply_body_max_size 10 MB //允许用户下载的最大文件大小,以字节为单位。(默认设置0表示不限制)
maximum_object_size 4096 KB //允许保存到缓存空间的最大对象大小,以KB为单位。超过大小限制的文件将不被缓存,直接转发给客户端

[[email protected] init.d]# vim /etc/squid.conf
:set nu //显示行数
添加:
63 cache_mem 64 MB
64 reply_body_max_size 10 MB
65 maximum_object_size 4096 KB

[[email protected] init.d]#iptables -F
[[email protected] init.d]#setenforce 0
[[email protected] init.d]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
[[email protected] init.d]# service squid reload




//在20.0.0.32 客户端
[[email protected] ~]# yum -y install httpd
[[email protected] ~]# systemctl start httpd
[[email protected] ~]# netstat -antp | grep 80

[[email protected] ~]# cd /var/log/httpd/
[[email protected] httpd]# ls
access_log error_log



用客户机或者用真机的浏览器访问20.0.0.32,apache网站服务器,然后看apache的日志,我这里用真机访问,真机的IP是20.0.0.1。
[[email protected] httpd]# cat
access_log
看IP,是20.0.0.1访问的20.0.0.32 apache网站。
Squid代理服务器应用

然后在浏览器清除历史浏览数据,设置代理服务器IP地址为20.0.0.31 端口号为3128,然后再次访问20.0.0.32。
Squid代理服务器应用
Squid代理服务器应用
Squid代理服务器应用

Squid代理服务器应用

Squid代理服务器应用

Squid代理服务器应用
//回到20.0.0.32 Web服务器上看日志
[[email protected] httpd]# cat
access_log
Squid代理服务器应用
看IP,变成了20.0.0.31代理服务器访问了apache网站,传统代理服务搭建完成。

【Squid透明代理服务器】
配置双网卡
//在20.0.0.31上
添加一个新的网卡,选仅主机模式。
步骤:
ip add 查看新加的网卡
cd /etc/sysconfig/network-scripts/
cp -p ifcfg-ens33 ifcfg-ens37 (ens37不固定,我这里显示的是ens37)
vim ifcfg-ens37
修改IP地址为192.168.10.1
子网掩码:255.255.255.0
UUID网卡信息删除,把ens33的网卡名改为ens37。



//在20.0.0.31上
[[email protected] network-scripts]# vim /etc/sysctl.conf
添加:
net.ipv4.ip_forward=1

//在20.0.0.32上
[[email protected] ~]# route add -net 192.168.10.0/24 gw 20.0.0.31

//在20.0.0.31上
[[email protected] network-scripts]# vim /etc/squid.conf
:set nu //显示行数
60 http_port 192.168.10.1: 3128 transparent

[[email protected] network-scripts]# service squid restart

[[email protected] network-scripts]# iptables -t nat -I PREROUTING -i ens33 -s 192.168.10.0/24 -p tcp --dport 80 -j REDIRECT --to 3128
[[email protected] network-scripts]# iptables -t nat -I PREROUTING -i ens33 -s 192.168.10.0/24 -p tcp --dport 443 -j REDIRECT --to 3128
[[email protected] network-scripts]# iptables -I INPUT -p tcp --dport 3218 -j ACCEPT
[[email protected] network-scripts]# netstat -antp | grep 3128



开启一台虚拟机作为客户机,WIN10系统。
IP:192.168.10.30 (192.168.10.0网段,主机号自定义)
子网掩码:255.255.255.0
网关:192.168.10.1


打开浏览器访问Web服务器 apache网站:
Squid代理服务器应用

//登录到20.0.0.32 Web服务器上查看日志信息:
[[email protected] httpd]# vim /var/log/httpd/access_log
shift+G
Squid代理服务器应用
看最新的日志信息,不是20.0.0.31代理服务器的IP,而是客户机192.168.10.30的IP,透明代理完成。