Squid代理缓存服务器

Squid代理缓存服务器


Squid代理服务器原理
1.Squid代理服务器原理
工作在tcp/ip的应用层
外网和内网是ping不通的
代理服务器把内网请求转发给外网
拿到数据后再把数据返回给内网,
而不是让内网和外网通信

2.Squid代理技术与Nat地址转换技术区别
nat是利用路由器或者开启了路由管道的linux服务器
两个网段是相通的
而代理服务器不开启路由管道

3.Squid七层防火墙与iptables三层防火墙区别
代理服务器做业务层代理,比如添加到浏览器,qq
做应用层防火墙,比如限制用户不能下载视频音频等
iptables直接网络层过滤

squid功能
提供http,ftp的代理服务
缓存代理内容,提高速度,节约流量
对客户端访问控制,限制允许访问squid服务器的客户机
对目标地址访问控制,限制客户端允许访问的网站
根据时间进行访问控制,限定客户端可以使用代理服务器的时间

4.Squid普通代理原理
客户端在浏览器或应用程序中指定代理的地址端口

5.Squid透明代理原理
企业网关主机
客户端不需要制定代理地址
通过iptables将客户机的web访问数据转交给代理服务器程序处理

6.Squid反向代理原理
外网用户访问代理服务器ip,访问内网的网站内容








Squid普通代理实验测试
1.Squid普通代理理论学习
也使用acl访问控制列表进行访问控制
内网客户端设置浏览器代理直接访问公网ip

2.Squid普通代理环境准备
安装
yum -y install squid*

配置
vi /etc/squid/squid.conf
http_port 192.168.10.1:3128
cache_mem 64 MB
maximum_object_size 4096KB
reply_body_max_size 10480000 allow all
access_log /var/log/squid/access.log    不配置的话默认是/var/spool/squid/store.log
visible_hostname 192.168.10.1
#缓存目录结构,100M缓存数据大小,16个1级目录,每个中256个2级目录
cache_dir ufs /var/spool/squid 100 16 256    
acl innet src 192.168.10.0/24
acl all src 0.0.0.0/0.0.0.0
http_access allow innet
http_access deny all

启动/停止
service squid start/restart/stop
重新加载配置文件
squid -k reconfig

3.Squid普通代理实验测试
windows
    企业私网
    192.168.10.2
    255.255.255.0
    内部员工一般需要设置网关
    但这里不需要,因为数据只要到达代理就可以,不需要再通过网关出去
    浏览器设置代理

squid服务器
    vm1    ifconfig eth0 192.168.10.1
    vm2    ifconfig eth1 200.168.10.1
    rpm -qa |grep squid*
    yum -y install squid*
    vi /etc/squid/squid.conf     文件太长,我们直接自己新建
    http_port 192.168.10.1:3128
    visible_hostname 192.168.10.1
    acl innet src 192.168.10.0/24    
    acl all src 0.0.0.0/0.0.0.0
    http_access allow innet        允许innet列表
    http_access deny all        拒绝all列表
    
    service squid restart    重启后,/var/spool/squid 里面自动创建缓存目录
    tree 命令可以总结目录下多少子目录和文件
    tree 00
    pstree |grep squid    查进程是否起来
    netstat -tunpl | grep :3128    查端口是否起来

apache
    公网
    vm2    ifconfig eth0 200.168.10.2
    rpm -qa | grep httpd
    service httpd restart
    chkconfig httpd on    3,5级别是on的
    cd /var/www/html/
    echo "squid web server 200.168.10.2" > index.html

windows访问200.168.10.2
成功


ls /var/spool/squid
可以看到store.log,
tail -f store.log    实时跟踪log,刷新页面时,控制台可以看到日志更新
日志中可以看到客户机的http访问记录
squid.out 是错误信息






Squid透明和反向代理
1.Squid透明代理理论学习
内网客户端不设置浏览器代理直接访问公网ip

ACL访问控制列表
拒绝所有访问请求
acl all src 0.0.0.0/0.0.0.0
http_access deny all

类型
src        源ip
dst        目的ip
port        端口
srcdomain    源域名
dstdomain    目的域名
time        时间
maxconn    最大连接
url_regex    url正则过滤
urlpath_regex    urlpath正则过滤

acl LAN1 src 192.168.1.0/24
acl PC1 src 192.168.1.66/32
acl Blk_Domain dstdomain .qq.com kaixin001.com
acl Work_Hours time MTWHF 08:30-17:30
acl Max20_Conn maxconn 20
acl Blk_URL url_regex -i ^rtsp:// ^mms://
acl Blk_Words urlpath_regex -i sex adult
acl RealFile urlpath_regex -i \.rmvb$ \.rm$

http_access deny LAN1 Blk_URL    拒绝lan1网段的人访问blk_rul的网站
http_access deny LAN1 Blk_Words
http_access deny PC1 RealFile
http_access deny PC1 Max20_Conn
http_access allow LAN1 Work_Hours

2.Squid与iptables的重定向功能结合
透明代理条件
客户机web访问数据能经过防火墙
代理服务构建在网关主机
代理服务程序能够支持透明代理
配置iptables将客户机的web请求转发到squid代理端口

实现步骤
1 修改squid.conf
http_port 192.168.10.1:3128 transparent
2 重加载配置
squid -k reconfig
3 添加iptables规则,把内部http请求重定向到3128端口
iptables -t nat -I PREROUTING -i eth0 -s 192.168.10.0/24 -p tcp --dport 80 -j REDIRECT --to 3128
4内部用户不需要在浏览器设置代理,只要设置网关
还可以把squid服务器同时做成缓存dns服务器,内部用户指向这台服务器内部接口



3.Squid透明代理环境准备
windows
    企业私网
    192.168.10.2
    255.255.255.0
    网关:
    192.168.10.1

squid服务器
    vm1    ifconfig eth0 192.168.10.1
    vm2    ifconfig eth1 200.168.10.1
    rpm -qa |grep squid*
    yum -y install squid*
    vi /etc/squid/squid.conf     
    http_port 192.168.10.1:3128 transparent
    visible_hostname 192.168.10.1
    acl innet src 192.168.10.0/24    
    acl all src 0.0.0.0/0.0.0.0
    http_access allow innet        允许innet列表
    http_access deny all        拒绝all列表
    
    squid -k reconfig
    iptables -t nat -I PREROUTING -i eth0 -s 192.168.10.0/24 -p tcp --dport 80 -j REDIRECT --to 3128
    iptables -t nat -L -n    查看iptables信息

apache
    公网
    vm2    ifconfig eth0 200.168.10.2
    rpm -qa | grep httpd
    service httpd restart
    chkconfig httpd on    3,5级别是on的
    cd /var/www/html/
    echo "squid web server 200.168.10.2" > index.html

windows访问200.168.10.2
成功






5.Squid反向代理理论学习
公网客户端通过代理服务器访问内网服务器集群
公网访问的是代理服务器的ip,然后得到内网网站内容
修改squid.conf
http_port 192.168.10.1:80 vhost
cache_peer 192.168.10.2 parent 80 0 originserver weitht=5 max-conn=30
cache_peer 192.168.10.3 parent 80 0 originserver weitht=5 max-conn=30
cache_peer 192.168.10.4 parent 80 0 originserver weitht=5 max-conn=30
cache_peer 192.168.10.5 parent 80 0 originserver weitht=1 max-conn=8
代理服务器开了虚的80端口,并没有apache服务


6.Squid反向代理实验测试
windows
    外网
    200.168.10.2
    255.255.255.0

squid服务器
    vm1    ifconfig eth0 200.168.10.1
    vm2    ifconfig eth1 192.168.10.1
    rpm -qa |grep squid*
    yum -y install squid*
    vi /etc/squid/squid.conf     
    http_port 200.168.10.1:80 vhost
    visible_hostname 200.168.10.1
    acl all src 0.0.0.0/0.0.0.0
    http_access allow all        允许all列表
    cache_peer 192.168.10.2 parent 80 0 originserver weitht=5 max-conn=30

    squid -k reconfig

apache
    内网
    也不需要网关
    vm1    ifconfig eth0 192.168.10.2
    rpm -qa | grep httpd
    service httpd restart
    chkconfig httpd on    3,5级别是on的
    cd /var/www/html/
    echo "squid web server 200.168.10.2" > index.html

windows访问200.168.10.2
成功




7.Squid反向代理与apache虚拟主机结合
利用缓存静态内容做到网站加速
把静态网页传到apache的站点
rsync -a -e ssh myweb/ 192.168.10.2:/var/www/html/
访问200.18.10.1/index.html
显示了css,图片等
利用firebug-网络标签,追踪查看再次访问时是否来自缓存
可以看到Hit from 200.168.10.1:80,命中squid缓存池(/var/spool/squid/)

apache虚拟主机做反向代理

windows
    应该要能解析域名
    这里使用hosts文件简单模拟
    200.168.10.1 www.lamp.com
    200.168.10.1 www.web.com

squid服务器
    vi /etc/squid/squid.conf     
    http_port 200.168.10.1:80 vhost
    visible_hostname 200.168.10.1
    acl all src 0.0.0.0/0.0.0.0
    http_access allow all
    cache_peer 192.168.10.2 parent 80 0 originserver name=a
    cache_peer 192.168.10.2 parent 80 0 originserver name=b
    cache_peer_domain a www.lamp.com
    cache_peer_domain b www.web.com
    cache_peer_access a allow all    
    cache_peer_access b allow all
    
    squid -k config
apache
    vi /etc/httpd/conf/httpd.conf
    #需要配置权限,虚拟主机才可以访问
    <Directory />
        Options FollowSymLinks
        AllowOverride None
        order deny,allow
        allow from all
    </Directory>
    NameVirtualHost 192.168.10.2:80
    <VirtualHost 192.168.10.2:80>
        DocumentRoot /lamp
        ServerName www.lamp.com
    </VirtualHost>
    <VirtualHost 192.168.10.2:80>
        DocumentRoot /web
        ServerName www.web.com
    </VirtualHost>

    mkdir /web
    echo "www.web.com" > index.html
    mkdir /lamp
    echo "www.lamp.com" > index.html

访问
www.web.com
www.lamp.com





你可能感兴趣的:(linux,squid,reverseproxy)