Linux安全网 - Linux操作系统_Linux 命令_Linux教程_Linux黑客

会员投稿 投稿指南 本期推荐: 强烈推荐:Ylmf OS 3.0 正式版发布附迅雷高 XP系统下硬盘安装Fedora 14图文教程
搜索:
您的位置: Linux安全网 > Linux入门 > » 正文

squid代理服务器

来源: sushanshan 分享至:

Squid cache(简称为Squid)是一个流行的自由软件(GNU通用公共许可证)的代理服务器和Web缓存服务器。Squid有广泛的用途,从作为网页服务器的前置cache服务器缓存相关请求来提高Web服务器的速度,到为一组人共享网络资源而缓存万维网,域名系统和其他网络搜索,到通过过滤流量帮助网络安全,到局域网通过代理上网。Squid主要设计用于在Unix一类系统运行。

Squid的发展历史相当悠久,功能也相当完善。除了HTTP外,对于FTP与HTTPS的支援也相当好,在3.0 测试版中也支援了IPv6。
你可以测试:curl -I www.163.com显示下面的X-Via .....(Cdn Cache Server v2.0 sxbj)显示其所用的代理服务器的名称。
实验1:(1)首先:Squid 基本配置
yum install squid ­y
#使用 tmpfs 动态文件系统加速 squid
mkdir ­-m 1777 /dev/shm/squid
(或者是chown squid.squid squid 然后chmod 750 squid/ 只要是squid对/dev/shm/squid具有7的权限即可。)
mount ­­bind /dev/shm/squid /var/spool/squid
service squid start
(2) df -h 可以用来显示: /dev/shm。
ll /var/spool/squid是指的默认的缓存路径。
/etc/init.d/squid start
此时正常的来说:ll /var/spool/squid下面会有一些文件,如果没有则需要修改文件:vi /etc/squid/squid.conf
cache_dir ufs /var/spool/squid 100 16 256的注释打开。
正常你会在ll /var/spool/squid下发现16个目录,然后在每个目录下有256个子目录。
同样的在/dev/shm/squid 也是那样子的哦!
实验2:步骤(1)
[root@server40 shm]# /etc/init.d/squid status
squid (pid 2449) is running...
2011/11/20 19:05:42| WARNING cache_mem is larger than total disk cache space!

(2)vi /etc/squid/squid.conf
#http_access allow localnet
http_access allow localhost
cache_mem 64 MB
/etc/init.d/squid restart(OR squid -k reconfigure不报错就是对的。)
(3)打开火狐:Edit-->Preferences-->Network-->Setting-->Manual proxy configuration:HTTP Proxy:192.168.0.140(假设以上设置全是在192.168.0.140上进行设置的。)
此时你输入网址:www.baidu.com
会出现:

ERROR
The requested URL could not be retrieved

The following error was encountered while trying to retrieve the URL: http://www.baidu.com/

Access Denied.

Access control configuration prevents your request from being allowed at this time. Please contact your service provider if you feel this is incorrect.

Your cache administrator is root.

Generated Sun, 20 Nov 2011 11:25:57 GMT by server40.example.com (squid/3.1.4)
着说明你拒绝成功!
实验三:
(1)在server40(192.168.0.140) 上:
vi /etc/squid/squid.conf
acl desktop38 src 192.168.0.38
http_access deny desktop38
意思是你拒绝desktop38这台主机以140为代理服务器。
(2)squid -k reconfigure(重新读入文件)
(3)此时在desktop38(192.168.0.38主机上)打开火狐:Edit-->Preferences-->Network-->Setting-->Manual proxy configuration:HTTP Proxy:192.168.0.140
此时拒绝访问,而你如果将上述两条注释之后,那么你就可以访问了。
实验四:
(1)在server40(192.168.0.140) 上:
vi /etc/squid/squid.conf
acl desktop38 src 192.168.0.38
acl cleantime time MTWHF 15:00-18:00(意思在周一到周五的这段时间在15:00-18:00的时候要拒绝访问。MTWHF分别代表周一周二周三。。。。)
http_access deny desktop38 cleantime
意思是你拒绝desktop38这台主机以140为代理服务器。
(2)squid -k reconfigure(重新读入文件)
(3)此时在desktop38(192.168.0.38主机上)打开火狐:Edit-->Preferences-->Network-->Setting-->Manual proxy configuration:HTTP Proxy:192.168.0.140
倘若现在的时间是在:15:00-18:00之间的话,那么你不可以访问,倘若不在那个时间段,你是可以访问(另外要注意一点两个机子的时间必须是一致的。即192.168.0.140(部署squid的服务器)和192.168.0.38(以192.168.0.140为代理服务器的主机)上的时间应该是一致的,改动在/etc/ntp.conf中进行修改的哦!)
实验五:
#站点屏蔽
echo 151.11.56.58 > /etc/squid/sexip
echo .sex.com > /etc/squid/sexdn
echo www.playbody.com > /etc/squid/sexurl
echo sex > /etc/squid/sextag
chown root:squid /etc/squid/sex{ip,dn,url,tag}
chmod 640 /etc/squid/sex{ip,dn,url,tag}
acl sexip dst "/etc/squid/sexip" (sexip 记录不法 ip 地址)
acl sexdn dstdomain "/etc/squid/sexdn" (sexdn记录不法域名)
acl sexurl url_regex "/etc/squid/sexurl" (sexurl记录不法网址)
acl sextag urlpath_regex "/etc/squid/sextag" (sextag记录不法关键字)
http_access deny sexdn
http_access deny sexip
http_access deny sexurl
http_access deny sextag(注意那个deny的要在allow之前哦,否则会被放进来的!)
测试:假设你现在客户端的IP:192.168.0.38时,设置以140为代理squid。那么你现在在192.168.0.38这台机子上:访问:151.11.56.58, www.souhu.sex.com www.playbody.com,或者有sex关键字的时候都是拒绝的,但如果你是访问这个www.baidu.com则是可以的哦!(因为你没有写关于防止www.baidu.com的任何信息)
实验6:#限制访问端口
acl SSL_ports port 443
acl Safe_ports port 80
acl Safe_ports port 21
acl CONNECT method CONNECT
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
意思就是拒绝非safe_ports.也就是允许21,80这两个端口进行访问,拒绝connect。
实验7:
squid 反向代理加速 web
#监听服务器 192.168.0.254 的 80 端口,透明代理,支持域名和 IP 的虚拟主机
http_port 192.168.0.254:80 transparent vhost vport
#代理到本机的 80 端口的服务,仅仅做为原始内容服务器(这样子不用防火墙参与,即可完成 web 反向代理加速)
cache_peer 127.0.0.1 parent 80 0 no­query originserver
测试:你在以该机子为服务器的客户端:输入类似:www.baidu.com,那么你会显示:server40,
实验8:
在192.168.0.140主机上:squid 反向代理对后端进行负载均衡
cache_peer 192.168.0.38 parent 80 0 no­query originserver name=a round­robin
cache_peer 192.168.0.40 parent 80 0 no­query originserver name=b round­robin
cache_peer_domain a www.example.com
cache_peer_domain b www.example.com
在这个例子中 www.example.com 就是 squid 服务器的地址 squid 使用的是 RR 的方法轮询,从后端的实际服务器中
挑选一台进行抓取,同时会对后端的健康状态进行检查,如果后端服务器 down 掉,那么 squid 会从剩余的 origin 服
务器中抓取数据
测试:你在192.168.0.38和192.168.0.40这两个主机上开启httpd /etc/init.d/httpd restart
然后你在一个客户端上输入: www.example.com 的时候你点击刷新:会发现desktop40.example.com和desktop38.example.com(写在/var/www/html/index.html里面)
当你关闭一台机子上的httpd的时候会只显示一台机子上。

 


Tags:
分享至:
最新图文资讯
1 2 3 4 5 6
验证码:点击我更换图片 理智评论文明上网,拒绝恶意谩骂 用户名:
关于我们 - 联系我们 - 广告服务 - 友情链接 - 网站地图 - 版权声明 - 发展历史