一、Web代理的工作机制
为C/S双方都提供了便利,将服务器数据缓存给客户机,这样客户机访问服务器更快,服务器也能节省资源
二、代理方式
1. 传统代理
需要在客户端中浏览器配置代理服务器
2. 透明代理
客户机不需要指定代理服务器,而直接通过默认路由、防火墙策略将Web访问重定向给代理服务器处理
3. 反向代理
负载均衡的过程中Squid起到缓存作用
三、部署传统代理
1. 拓扑图
一台Squid一台Apache服务器
主机 | 操作系统 | IP 地址 | 主要软件 |
---|---|---|---|
Squid服务器 | CentOS 7 | 192.168.0.10 | Squid |
Apache服务器 | CentOS 7 | 192.168.0.20 | Apache |
2. 编译安装
systemctl stop firewalld
systemctl disable firewalld
setenforce 0
yum -y install gcc gcc-c++ make perl
wget -P /opt http://www.squid-cache.org/Versions/v3/3.5/squid-3.5.28.tar.gz
cd /opt
tar -zxvf squid-3.5.28.tar.gz
cd squid-3.5.28
./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
make -j && make install
ln -s /usr/local/squid/sbin/* /usr/local/sbin/
useradd -M -s /sbin/nologin squid
chown -R squid:squid /usr/local/squid/var/
#创建软链接至路径环境变量,方便系统识别 squid 的系统命令
#创建程序用户 squid,保证系统安全性
#为 /usr/local/squid/var 目录递归指定属主属组
3. 修改配置文件
vim /etc/squid.conf
#56行,插入
http_access allow all
#放在 http_access deny all 之前,允许任意客户机使用代理服务
http_access deny all
#61行,插入
cache_effective_user squid
#添加指定程序用户,用来设置初始化、运行时缓存的账号,否则启动不成功
cache_effective_group squid
#添加指定账号基本组
#63行,插入
cache_mem 64 MB
#指定缓存功能所使用的内存空间大小,便于保持访问较频繁的WEB对象,容量最好为4的倍数,单位为MB,建议设为物理内存的1/4
reply_body_max_size 10 MB
#允许用户下载的最大文件大小,以字节为单位,当下载超过指定大小的Web对象时,浏览器的报错页面中会出现“请求或访问太大”的提示默认设置0表示不进行限制
maximum_object_size 4096 KB
#允许保存到缓存空间的最大对象大小,以KB为单位,超过大小限制的文件将不被缓存,而是直接转发给用户
4. 服务控制
squid -k parse
#检查配置文件语法是否正确
squid –z
#-z 选项用来初始化缓存目录
squid
#启动 squid 服务
vim /etc/init.d/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)