您的位置:首页 > 运维架构 > 反向代理

squid 透明代理,反向代理,acl访问控制

2014-11-27 12:36 309 查看
squid 的三种代理方式:1普通代理;2透明代理 3反向代理;

cleint(192.168.1.20)----> eth0(192.168.1.10)| squid |eth1(1.1.1.254) ---->server(1.1.1.1)

1.普通代理;squid是代替内网 访问外网; 缓存:加快内网的访问速度;
服务器端配置:ip 1.1.1.1
关闭防火墙和selinux
[root@server ~]# /etc/init.d/iptables stop
iptables: Setting chains to policy ACCEPT: nat filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
[root@server ~]# setenforce 0
yum install -y httpd
echo "1.1.1.1web" >/var/www/html/index.html
本机测试:
[root@server ~]# elinks --dump 1.1.1.1
1.1.1.1web//本机测试成功;

squid代理端的配置:
[root@localhost log]# vim /etc/squid/squid.conf
#http_access deny all //拒绝所有
http_access allow all//允许所有的人使用本机的代理

[root@localhost log]# /etc/init.d/squid restart
Stopping squid: [FAILED]
Starting squid: . [ OK ]

[root@localhost log]# lsof -i :3128
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
squid 3406 squid 14u IPv6 18682 0t0 TCP *:squid (LISTEN)

客户端的配置:
在浏览器中制定代理和端口即可;

2.透明代理:客户端不需要制定浏览器ip地址和端口;
前提条件:{1.代理服务器也是内网的防火墙,}
客户端的配置:
把代理服务器做为client的默认网关;
route add -net default gw 192.168.1.10

squid的配置:
vim /etc/squid/squid.conf
http_port 3128 transparent//修改代理方式为透明代理;
[root@localhost /]# /etc/init.d/squid restart
Stopping squid: ................ [ OK ]
Starting squid: . [ OK ]

//添加一条防火墙策略进行端口转发
[root@localhost /]# iptables -t nat -A PREROUTING -i eth0 -s 192.168.1.0/24 -p tcp --dport 80 -j REDIRECT --to-port 3128
[root@localhost /]# iptables -t nat -L
Chain PREROUTING (policy ACCEPT)
target prot opt source destination
REDIRECT tcp -- 192.168.1.0/24 anywhere tcp dpt:http redir ports 3128

Chain POSTROUTING (policy ACCEPT)
target prot opt source destination

Chain OUTPUT (policy ACCEPT)
target prot opt source destination

3.反向代理:代替外网访问内网 ,缓存加快外网的访问速度(可以实现多个web服务器来提供服务);

squid端的配置:
http_port 80 vhost
cache_peer 192.168.1.20 parent 80 0 originserver

就这么简单就实现了:反向代理;剩下的在服务段配置httpd即可;
客户端访问squid就OK了;

4.反向代理+虚拟主机(基于域名):
要求:互联网上的客户端www.haha.com的时候访问的服务器/www
bbs.haha.com的时候访问的服务器/bbs

服务器端用虚拟主机来实现:
vim /etc/httpd/conf.d/VirtualHost.conf
NameVirtualHost 192.168.1.20:80
<VirtualHost 192.168.1.20:80>
ServerAdmin root@www.haha.com
DocumentRoot /www
ServerName www.haha.com
ErrorLog logs/www-error_log
CustomLog logs/www-access_log common
</VirtualHost>
<VirtualHost 192.168.1.20:80>
ServerAdmin root@bbs.haha.com
DocumentRoot /bbs
ServerName bbs.haha.com
ErrorLog logs/bbs-error_log
CustomLog logs/bbs-access_log common
</VirtualHost>

在服务器端本机测试:
vim /etc/hosts //添加静态解析
192.168.1.20 www.haha.com
192.168.1.20 bbs.haha.com

[root@localhost conf.d]# elinks --dump www.haha.com
192.168.1.20:www
[root@localhost conf.d]# elinks --dump bbs.haha.com
192.168.1.20:bbs
//测试成功!

配置反向代理服务器:
vim /etc/squid/squid.conf

http_port 80 vhost
cache_peer 192.168.1.20 parent 80 0 originserver name=www
cache_peer 192.168.1.20 parent 80 0 originserver name=bbs
cache_peer_domain www www.haha.com
cache_peer_domain bbs bbs.haha.com

service squid restart

客户端的配置;
vim /etc/hosts
1.1.1.254 bbs.haha.com
1.1.1.254 www.haha.com

[root@server ~]# elinks --dump www.haha.com
192.168.1.20:www
[root@server ~]# elinks --dump bbs.haha.com
192.168.1.20:bbs

//测试成功!!

5.squid的ACL(访问控制)

acl acl名称 类型 匹配的目标对象;

类型总结:
src : 原地址 (是ip,网段,主机名)
192.168.1.10/32 192.168.1.0/24 www.haha.com

dst 目标地址 (同上);

port 端口 25

dstdomian 目标域 .baidu.com

time 时间 MTWHFAS(周一到周五) 8:30-20:30

maxcoon 最大的并发连接数;
acl maxline maxconn 50

url_regex 目标的URL地址 (可以用正则表达式);
acl deny_url url_regex ^http://
http_access deny deny_acl

URLpath_regex “完整的url” http://www.baidu.com/music/xxx.mp3
调用acl (acl可以先定义后调用,但可以自定义不调用)
http_access deny/allow acl名称列表;

综合实践:
acl allow_ip src 192.168.1.0/24
acl deny_ip src 192.168.1.250/32
acl work_time time MTWHF 8:30-17:30
acl deny_qq url_regex qq
acl deny_mp3 url_regex mp3$
acl deny_domain dstdomain .taobao.com .jingdong.com
acl deny_url url_regex taobao jingdong

http_access deny deny_domain deny_url
http_access deny work_time deny_qq deny_mp3
http_access deny deny_ip
http_access allow allow_ip
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息