代理服务器

代理服务器proxy

应用软件包:squid-2.6.STABLE6-3.el5

主程序:/usr/sbin/squid.conf

配置目录:/etc/squid

主配置文件:/etc/squid/squid.conf

端口:tcp 3128

日志文件:/var/log/squid/access.log

其中代理服务器有两块网卡一个是内网IP,一个是外网IP

 

安装squid

使用yum 安装squid

yum -y install squid

[root@localhost ~]# cd /etc/squid/  切换到配置目录

[root@localhost squid]# vim squid.conf  编辑主配置文件

配置文件中大部分是注释行,以下部分是未注释行

acl all src 0.0.0.0/0.0.0.0

acl manager proto cache_object

acl localhost src 127.0.0.1/255.255.255.255

acl to_localhost dst 127.0.0.0/8

acl SSL_ports port 443

acl Safe_ports port 80 # http

acl Safe_ports port 21 # ftp

acl Safe_ports port 443 # https

acl Safe_ports port 70 # gopher

acl Safe_ports port 210 # wais

acl Safe_ports port 1025-65535 # unregistered ports

acl Safe_ports port 280 # http-mgmt

acl Safe_ports port 488 # gss-http

acl Safe_ports port 591 # filemaker

acl Safe_ports port 777 # multiling http

acl CONNECT method CONNECT

http_access allow manager localhost

http_access deny manager

http_access deny !Safe_ports

http_access deny CONNECT !SSL_ports

http_access allow localhost

http_access deny all

icp_access allow all

http_port 3128

hierarchy_stoplist cgi-bin ?

access_log /var/log/squid/access.log squid

acl QUERY urlpath_regex cgi-bin \?

cache deny QUERY

refresh_pattern ^ftp: 1440 20% 10080

refresh_pattern ^gopher: 1440 0% 1440

refresh_pattern . 0 20% 4320

acl apache rep_header Server ^Apache

broken_vary_encoding allow apache

coredump_dir /var/spool/squid

我们需要便捷的配置项包括

http_port内网IP:3128  默认监听的网卡和端口

cache_mem 64MB  占据内存空间大小,通常是物理内存的1/4-1/3

reply_body_max_size 10240000  allow all    (字节)设置或限制文件下载的最大大小

access_log /var/log/squid/access.log squid  访问日志文件路径

minimum_object_size 0 KB  缓存对象最小值

maximum_object_size 4096 KB  缓存对象最大值

cache_swap_low 90  缓存目录大小超过限定的95%,则清空老数据,清除值90%不再清理

cache_swap_high 95  cache_swap_low同时使用

visible_hostname proxy.zsl.com  代理服务器要求的主机名(写自己的主机名)

cache_dir ufs /var/spool/squid 100 16 256  缓存目录(ufs是缓存格式 100 缓存目录分配磁盘空间(MB) 16 缓存空间的一级子目录个数 256 缓存空间的二级子目录个数)

更改默认访问控制http_assess deny all 为 allow all

[root@localhost squid]# squid -k parse  检查配置文件是否有错误

[root@localhost squid]# squid -z  初始化(创建)squid缓存目录

[root@localhost squid]# service squid start

因为我们是两块网卡,所以proxy服务器开启路由

[root@localhost squid] vim /etc/sysctl.conf

    设置net.ipv4.ip_forward = 1

[root@localhost squid]# service squid reload  重新加载squid服务

 

关于proxy的访问控制(acl

常用的acl列表类型

src源地址  dst目标地址  arp匹配源MAC地址  port端口类型  time基于时间的控制

srcdomain源域 detdomain目标域  maxconn同一客户端的最大连接求 

url_regex限制协议 urlpath_regex 

acl的使用

acl NAME 类型 地址

NAME是自己设定的

比如

acl GOD src 192.168.2.0/24

http_assess deny GOD 拒绝GODGOD就是源地址是192.168.2.0/24的用户

 

透明代理(做透明代理时客户机需要将网关设置为代理服务器)

所谓的透明代理对于用户来讲不需要设置代理,就是说他们意识不到代理服务器的存在,只需要将网关指向代理服务器即可。

我们需要以下操作

修改主配置文件squid.conf,让其支持透明代理,并重新加载该配置

[root@localhost squid]# vim squid.conf

仅需修改一行

http_port 内网IP3128 transparent

添加iptables规则

iptables -t nat -A PREROUTING -i eth1(内网网卡) -s 192.168.1.0/24 -p tcp --dport 80 -j REDIRECT --to-ports 3128

举个例子

192.168.1.20代理服务器的内部网卡(eth1)相连,他访问www.baidu.com 时,到防火墙的路由前,源地址变为代理服务器地址,端口由80变为本机的3128,添加此行是为了让客户端不在浏览器设置中指定代理的情况iptables帮客户机联系到代理服务器

(生产环境下需要在内网中部署一台DNS服务器,DNS服务器可以连接到互联网,客户DNS指向内网DNS服务器

设置SNAT

iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j SNAT --to-source IP代理服务器外网卡)

[root@localhost squid]# service squid reload  重新加载squid服务

 

反相代理

作用:为内部服务器提供缓存,提高外网访问网站的速度

修改主配置文件squid.conf,让其支持反相代理

[root@localhost squid]# vim squid.conf

编辑

http_port 外网IP:80 vhost

添加以下几行

cache_peer 内网IP1 parent 80 0 originserver weight=1 max-conn=10

cache_peer 内网IP2 parent 80 0 originserver weight=2 max-conn=20

cache_peer 内网IP3 parent 80 0 originserver weight=3 max-conn=30

cache_peer WEB服务器ip 服务器类型 http端口 icp端口 可选项

可选项包括 originserver这个服务器是个真实的server weight权重 max-conn同时相应多少请求

*基于反向代理实现虚拟主机映射

*cache_peer 内网IP4 parent 80 0 originserver name=zsl

*cache_peer 内网IP5 parent 80 0 originserver name=zzu

*cache_peer_domain zsl www.zsl.com

*cache_peer_domain zzu www.zzu.com

[root@localhost squid]# service squid reload  重新加载squid服务

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章