代理服务器Squid,Nginx

来源:互联网 发布:淘宝专业版退回基础版 编辑:程序博客网 时间:2024/05/21 09:20
代理服务器:Squid,Nginx

正向代理(透明代理) squid  nginx
     作用:让内网用户上网,缓存(内存,硬盘),加快访问速度,节约通信带宽
             访问控制ACL实现对用户上网行为进行控制(时间、网站、内容...)
          防止内部主机受到攻击
反向代理  squid,nginx
     作用:给网站加速
================================================================
[root@nat-server ~]# iptables -F
[root@nat-server ~]# iptables -t nat -F
[root@nat-server ~]# service iptables save


==squid:
软件包:squid
端口: 3128/tcp默认
配置文件:/etc/squid/squid.conf
日志文件: /var/log/squid


正向代理
client(192.168.2.168)--->eth0(192.168.2.10)squid Server(1.1.1.1)eth1 --> Web(1.1.1.100)
一、配置squid
# yum -y install squid
# vim /etc/squid/squid.conf
http_port 3128          //squid监听的端口
cache_mem 16000 MB     //设置squid内存缓冲大小
cache_dir ufs /var/spool/squid 50000 16 256     //设置squid硬盘缓冲大小
cache_effective_user squid
cache_effective_group squid
dns_nameservers 202.106.0.20 8.8.8.8
cache_mgr tianyun@126.com

===========================================================
# service squid start
init_cache_dir /var/spool/squid... /etc/init.d/squid: line 62:  5504 已放弃              
$SQUID -z -F -D >> /var/log/squid/squid.out 2>&1
启动 squid:/etc/init.d/squid: line 42:  5505 已放弃              
$SQUID $SQUID_OPTS >> /var/log/squid/squid.out 2>&1
[失败]

visible_hostname squid
=========================================================

# service squid start
# chkconfig squid on
[root@nat-server ~]# netstat -tnlp |grep :3128
tcp        0      0 0.0.0.0:3128         0.0.0.0:*   LISTEN      5526/(squid) 

从客户端测试代理服务器:
浏览器:需要手工设置代理
测试结果:代理服务默认不为任何主机代理


解决方案:
ACL,限制用户访问(时间、目标网站、内容...)
# vim /etc/squid/squid.conf
/INSERT
方案一:为所有主机代理
acl all src 0.0.0.0/0.0.0.0
http_access allow all

方案二:为部分主机代理
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
acl it_net src 192.168.2.0/24
http_access allow it_net
# service squid reload


案例1:允许192.168.2.0/24网段的主机使用squid
acl it_net src 192.168.2.0/24
http_access allow it_net

案例2: 允许192.168.2.0/24网段的主机使用squid,(周一到周五 9:00-16:00)
acl it_net src 192.168.2.0/24
acl worktime time MTWHF 9:00-16:00
http_access allow it_net worktime

案例3 拒绝主机
acl wangcheng src 192.168.2.168
http_access deny wangcheng

案例4 禁止用户访问URL包含qq.com网站,-i忽略大小写
acl disable_web url_regex -i qq.com
http_access deny disable_web

案例5 禁止用户下载*.mp3 *.exe *.iso
acl disable_down urlpath_regex -i \.mp3$ \.exe$ \.iso$                 \\ . 点需要转义下,不转义的话代表任意单个字符。
http_access deny disable_down

=============================================================
acl all src 0.0.0.0/0.0.0.0
acl it_net src 192.168.2.0/24
acl hr_net src 192.168.3.0/24
acl worktime time MTWHF 9:00-16:00
acl disable_web url_regex -i qq.com
acl disable_down urlpath_regex -i \.mp3$ \.exe$ \.iso$
acl wangcheng src 192.168.2.168

http_access deny wangcheng
http_access deny disable_down
http_access deny disable_web
http_access allow it_net
http_access allow hr_net worktime
http_access deny all
=============================================================

透明代理(以正向代理为基础)
代理服务器:IP 192.168.2.10
          http_port: 3128
1. iptables数据包重定向
[root@squid-server ~]# iptables -t nat -F
[root@squid-server ~]# iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to 3128
[root@squid-server ~]# service iptables save

2. squid支持透明代理
[root@squid-server ~]# vim /etc/squid/squid.conf
http_port 3128 transparent
[root@squid-server ~]# service squid reload

注意:客户端必须将数据包发给代理服务器


反向代理
============================
[root@squid-server ~]# service iptables stop
client(192.168.2.115)--->          前端反向代理服务器(192.168.2.10) --> Web(192.168.2.108)
本身已经可以访问Internet          前端反向代理服务器(192.168.2.168)     前端反向代理服务器(192.168.2.169)。。。。。。。。。。。。。。。    
一、配置squid
# service iptables stop
# yum -y install squid
# vim /etc/squid/squid.conf
cache_mem 16000 MB     //设置squid内存缓冲大小
cache_dir ufs /var/spool/squid 50000 16 256     //设置squid硬盘缓冲大小,
cache_effective_user squid
cache_effective_group squid
dns_nameservers 202.106.0.20 8.8.8.8
cache_mgr tianyun@126.com

http_port 80 vhost     //反向代理
cache_peer 192.168.2.108 parent 80 0          //源站
acl all src 0.0.0.0/0.0.0.0
http_access allow all

cache_peer Web服务器地址 服务器类型 http端口 icp端口 选项
cache_peer 192.168.2.108 parent 80 0

=========================================================
给一个源站做反向代理
cache_peer 192.168.2.108 parent 80 0         

给多个源站做反向代理
cache_peer 192.168.2.100 parent 80 0 originserver weight=1 name=tianyun
cache_peer 192.168.2.120 parent 80 0 originserver weight=1 name=uplooking
cache_peer 192.168.2.130 parent 80 0 originserver weight=1 name=126
cache_peer_domain tianyun www.tianyun.com
cache_peer_domain uplooking www.uplooking.com
cache_peer_domain 126 www.126.com
============================================================
如果启动失败,检查80端口是否被占用


从客户端测试:必须使用域名访问
/etc/hosts
192.168.2.10     www.tianyun.com www.uplooking.com www.126.com
反向代理服务器


0 0