squid代理服务器
squid简介
squid 原理
squid工作类型
代理服务器基本类型:
传统代理:适用于Internet,需明确指定服务端,
针对内网用户提供缓存,简单需要用户在客户端进行配置
透明代理:适用于共享上网网关,不需指定服务端
通过防火墙规则,使用户无需配置客户端
反向代理:位于网站前端,代理web接受客户端的请求
支持RR算法
支持同类型服务器的相互数据转换
squid简介
软件包:squid-3.xxxx
系统服务:squid
主程序:/usr/sbin/squid
主配置文件:/etc/squid/squid.conf
默认监听端口:TCP 3128
默认访问日志:/var/log/squid/access.log
5
2 4 5 6 3
1s
hash tables
digest tables
常见参数:
http_port 3128
access_log /var/log/squid/access.log squid
cache_mem 64 MB
cache_dir ufs /var/spool/squid 100 16 256
默认MB 一级目录的个数是16个,二级目录的个数是256个
visible_hostname proxy.benet.com
dns_testnames www.google.com www.163.com
maximum_object_size 4096 KB
reply_body_max_size 10 MB
ACL( Access Control List ,访问控制列表):
根据源地址、目标URL、文件类型等定义列表
acl 列表名称 列表类型 列表内容 ...
针对已定义的acl列表进行限制
http_access allow或deny 列表名称 ...
[root@localhost ~]
……
acl a1 src 0.0.0.0/0.0.0.0
http_access deny a1
[root@localhost ~]
支持列表类型
src 源地址
dst 目标地址
port 目标端口
dstdomain 目标域
time 访问时间
maxconn 最大并发连接
url_regex 目标URL地址
www.baidu.com
Urlpath_regex 整个目标URL路径
http://www.baidu.com/1.jpeg
squid实验的构建
传统代理方式
实验环境
三台虚拟机(自定义模式)
内网用户端:192.168.66.11
squid:内网:192.168.66.12
公网:10.10.10.12
公网端: 10.10.10.13
# squid去ping下内网和公网看看能不能通
外网(公网)机器配置
yum -y install httpd
service httpd start
chkconfig httpd on
echo "www.hongfu.com" >> /var/www/html/index.html
tcpdump -nA port 80
用内网访问公网 curl 10.10.10.13 然后再公网使用抓包命令(先抓包,后访问)
squid 网关机器
vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
sysctl -p
yum -y install squid
vim /etc/squid/squid.conf
http_port 3128
visible_hostname www.hongfu.com
cache_dir ufs /var/spool/squid 100 16 256
service squid start
service squid reload
内网机器
echo "GATEWAY=192.168.190.112" >> /etc/sysconfig/network-scripts/ifcfg-eth0
service network restart
route -n
测试
浏览器——编辑——首选项——高级——网络——设置——手动代理——网关内网IP——3128 下面打勾
vim /etc/profile
HTTP_PROXY=HTTP://192.168.66.12:3128
export HTTP_PROXY
source /etc/profile
echo $HTTP_PROXY
yum -y install elinks
elinks 10.10.10.13
透明代理方式
实验环境
三台虚拟机(自定义模式)
内网:192.168.190.111
squid网关:内:192.168.190.112
外:10.10.10.12
外网:10.10.10.13
内网机器
echo "GATEWAY=192.168.190.112" >> /etc/sysconfig/network-scripts/ifcfg-eth0
service network restart
route -n
网关服务器
yum -y install squid
vim /etc/squid/squid.conf
http_port 192.168.190.112:3128 transparent
visible_hostname www.hongfu.com
cache_dir ufs /var/spool/squid 100 16 256
service squid start
vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
sysctl -p
service iptables start
chkconfig iptables on
iptables -F
iptables -t nat -A PREROUTING -i eth0(内网网卡) -s 192.168.190.0/255.255.255.0 -p tcp --dport 80 -j REDIRECT --to-ports 3128
iptables -t nat -L
service iptables save
chkconfig iptables on
外网服务器
yum -y install httpd
service httpd start
echo "www" >> /var/www/html/index.html
curl localhost
测试或下载限速测试
curl 10.10.10.13
cd /var/www/html/
dd if=/dev/zero of=1.txt bs=1M count=15
du -sh 1,txt
wget 10.10.10.13/1.txt
vim /etc/squid/squid.conf
reply_body_max_size 10 MB
/etc/init.d/squid reload
wget 10.10.10.13/1.txt
反向代理
实验环境
四台虚拟机:(轮训)
内网服务器:1:192.168.190.111
2:192.168.190.112
squid网关服务器:
192.168.190.113
10.10.10.13
公网客户端:10.10.10.14
两台内网服务器
echo "GATEWAY=192.168.190.113" >> /etc/sysconfig/network-scripts/ifcfg-eth0
service network restart
route -n
yum -y install httpd
service httpd start
chkconfig httpd on
cd /var/www/html/
echo "www1.html-11" >> index1.html
echo "www2.html-11" >> index2.html
curl localhost
cd /var/www/html/
echo "www1.html-12" >> index1.html
echo "www2.html-12" >> index2.html
网关服务器
vim /etc/sysctl.conf
net.ipv4.ip_forward = 1
sysctl -p
yum -y install squid
vim /etc/squid/squid.conf
http_access allow all
http_port 10.10.10.13:80 vhost
visible_hostname www.hongfu.com
cache_dir ufs /var/spool/squid 100 16 256
cache_peer 192.168.190.111 parent 80 0 originserver round-robin
cache_peer 192.168.190.112 parent 80 0 originserver round-robin
/etc/init.d/squid start
/etc/init.d/squid reload
公网客户端
curl 10.10.10.13/index1.html
curl 10.10.10.13/index2.html
curl 10.10.10.13/index1.html
curl 10.10.10.13/index2.html
ACL访问控制
环境准备
在3配置好的反向代理模式下,并修改一下内容
将公网客户端机器的ip改为30.30.30.14
重启网卡
网关服务器
把eth1 改成 30.30.30.13
vim /etc/squid/squid.conf
http_port 30.30.30.13:80 vhost
/etc/init.d/squid reload
service squid reload
修改配置文件
vim /etc/squid/squid.conf
acl localnet src 10.0.0.0/8
acl localnet src 172.16.0.0/12
acl localnet src 192.168.0.0/16
acl localnet src 30.30.30.0/24
或 30.0.0.0/8
/etc/init.d/squid reload
如果把添加的放到上面
acl wan src 30.0.0.0/24
拒绝
http_access deny all
最后一行改成 http_access deny localhost
能访问
{ acl 列表名称 列表类型 列表内容
src 源地址
dst 目标地址
port 目标端口
dstdomain 目标域
time 访问时间
maxconn 最大并发连接
url_regex 目标URL地址 www.baidu.com
Urlpath_regex 整个目标URL路径
http://www.baidu.com/1.jpeg
http_access allow manager localhost
http_access deny manager
http_access allow localnet
http_access allow localhost
}
ACL规则
acl test src 30.30.30/24
30.30.30.14、index.html
http_access deny all
30.30.30.14、index.html