百度网站排名查询,网站建设教程浩森宇特,网页设计教程,青岛网站推广优化公司简介#xff1a; Squid 是 Linux/Unix 平台下最为流行的高性能免费应用层代理服务器#xff0c;它具有权限管理灵活、性能高和效率快的特点。 代理服务器可以提供文件缓存、复制和地址过滤等服务#xff0c;充分利用有限的出口带宽#xff0c;加快内部主机的访问速度。 同… 简介 Squid 是 Linux/Unix 平台下最为流行的高性能免费应用层代理服务器它具有权限管理灵活、性能高和效率快的特点。 代理服务器可以提供文件缓存、复制和地址过滤等服务充分利用有限的出口带宽加快内部主机的访问速度。 同时可以作为一个防火墙隔离内外网并且能够提供监控网络和记录传输信息的功能加强局域网的安全性。 代理服务器工作原理 1、客户端 A 向代理服务器发出访问 Internet 的请求 。 2、代理服务器接受请求后首先与访问控制列表中的访问规则相匹配如果满足规则则在缓存中查找是否有需要的资源信息。 3、如果缓存中存在客户端 A 的请求信息那么将此信息返回给客户端 A ; 如果没有代理服务器将代替客户端去向 Internet 请求指定的信息。 4、Internet 上的主机将请求的信息发送到代理服务器代理服务器会将信息存入缓存中。 5、代理服务器将 Internet 上主机的返回信息传给客户端 A 。 6、当客户端 B 也请求相同的信息时。 7、代理服务器也会接受请求后与访问控制列表中的规则相匹配。、 8、如果满足规则代理服务器将会把缓存中的信息直接传给客户端 B 。 Squid 对硬件的要求 1、内存短缺会严重影响性能。 2、硬盘空间也是一个重要因素更多的磁盘空间意味着更多的缓存目录和更高的命中率。 3、Squid 需要使用硬盘作为缓存 Cache 所以对硬盘的存取速度要求较高最好配置万转高速 SCSI 硬盘和磁盘阵列。 一、安装 Squid shell rpm -qa squid
shell
shell yum -y install squid ## 这里选择使用 yum 安装 也可以选择编译安装下载地址http://www.squid-cache.org/Versions/v3/3.4/squid-3.4.10.tar.gz 二、配置 Squid shell cd /etc/squid/
shell cp squid.conf squid.conf.bak ## 首先备份一下 squid.conf 文件 shell grep -P -v #|^$ squid.confacl manager proto cache_object acl localhost src 127.0.0.1/32 ::1 acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1 acl SSL_ports port 443 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 localnet http_access allow localhost http_access deny all http_port 3128 hierarchy_stoplist cgi-bin ? coredump_dir /var/spool/squid refresh_pattern ^ftp: 1440 20% 10080 refresh_pattern ^gopher: 1440 0% 1440 refresh_pattern -i (/cgi-bin/|?) 0 0% 0 refresh_pattern . 0 20% 4320 ## 这是默认 Squid 的有效配置 shell squid.conf # 清空默认配置文件
shell vim squid.conf # 重新写入配置 当然你也可以直接修改原来的配置文件 http_port 192.168.1.88:3128
指定在哪个 IP 跟 端口监听客户机请求默认为所有 IP 的 3128 端口即 http_port 3128
cache_mem 64 MB
内存缓存大小指定可以使用多少物理内存作为高速缓存。如果此服务器就是专门的缓存服务器可以指定为物理内存的一半。否则不应大于三分之一
cache_dir ufs /opt/squid_cache 4096 16 256
磁盘缓存区大小ufs 为缓存的存储类型一般为 ufs /span/opt/span stylecolor: #000000;squid_cache 指定缓存存放目录/spanspan stylecolor: #800080;4096/span 缓存空间最大为 span stylecolor: #800080;4096/spanspan stylecolor: #000000; MB/spanspan stylecolor: #800080;16/span 硬盘缓存目录下可以创建 span stylecolor: #800080;16/span 个一级子目录默认为 span stylecolor: #800080;16/spanspan stylecolor: #000000; 个/spanspan stylecolor: #800080;256/span 每个一级子目录下可以创建 span stylecolor: #800080;256/span 个二级目录默认为 span stylecolor: #800080;256/spanspan stylecolor: #000000; 个之所以创建这么多子目录是因为
如果子目录太少则存储在一个子目录下的文件数目会很多很多会导致系统寻找某一个文件的时间增加从而影响整体性能
cache_effective_user squid
使用缓存的有效用户如果系统没有此用户需手动添加
cache_effective_group squid
使用缓存的有效组如果系统没有此组需手动添加
dns_nameservers 8.8.8.8
为了使 Squid 能解析域名需定义真实 DNS 地址
cache_access_log /var/log/squid/access.log
访问日志文件路径记录了用户访问 Internet 的详细信息可以查看每用户的上网记录
cache_log /var/log/squid/cache.log
缓存日志文件路径记录了缓存相关的日志信息
cache_store_log /var/log/squid/store.log
网页缓存日志文件路径记录了网页在缓存中调用情况
visible_hostname 192.168.1.88
定义运行 Squid 的主机名称当访问发生错误时该选项会显示在错误提示网页中
cache_mgr wangxiaoqiang888163.com
设置 Squid 代理服务器管理员 E-mail 地址访问发生错误时会提示在错误页面中
acl all src 0.0.0.0/0.0.0.0
访问控制列表默认拒绝所有访问客户机的请求
acl 格式acl 列表名 列表类型 [-i] 列表值
列表名 区分各个访问控制列表不能相同尽量使用有意义的名称如badurl、badip、clientip、work time 等
列表类型 src 源 IP 地址 客户机 IP 地址 dst 目标 IP 地址 服务器 IP 地址 srcdomain 源名称 客户机所属的域 dstdomain 目标名称 服务器所属的域 /spanspan stylecolor: #0000ff;time/spanspan stylecolor: #000000; 一天中的时间和一周内的一天 url_regex URL 规则表达式匹配urlpath_regex:URL/span-span stylecolor: #000000;path 略去协议和主机名的 URL 规则表达式匹配proxy_auth 通过外部程序进行用户认证maxcocnn 单一 IP 最大连接数/spanspan stylecolor: #0000ff;time/spanspan stylecolor: #000000; 时间段[星期][时间段][星期]M Monday 星期一 T Tuesday , 星期二 W Wednesday , 星期三 H Thursday , 星期四 F Friday 星期五 A Saturday , 星期六 S Sunday , 星期日 [时间段]可以表示为 /spanspan stylecolor: #800080;10/span:span stylecolor: #800080;00/span-span stylecolor: #800080;21/span:span stylecolor: #800080;00/spanspan stylecolor: #000000;-i 选项加 -i 表示列表值的不区分大小写
列表值 对应列表类型值也不同可以是 IP 、时间、域名等
http_access allow all
允许或拒绝某个访问控制列表的 HTTP 请求格式为http_access [allow|deny] 列表名 三、初始化 Squid shell mkdir /opt/squid_cache
shell chown -R squid.squid /opt/squid_cache/ ## 创建缓存目录并更改属主、属组如果没有 squid 用户跟组要先创建 shell squid -k check
2015/01/05 15:11:04| ERROR: 0.0.0.0/0.0.0.0 needs to be replaced by the term all.
2015/01/05 15:11:04| SECURITY NOTICE: Overriding config setting. Using all instead.
2015/01/05 15:11:04| WARNING: (B) ::/0 is a subnetwork of (A) ::/0
2015/01/05 15:11:04| WARNING: because of this ::/0 is ignored to keep splay tree searching predictable
2015/01/05 15:11:04| WARNING: You should probably remove ::/0 from the ACL named all
squid: ERROR: No running copy ## squid -k check 检查 squid.conf 是否配置有误最后一行是因为没有启动 squid 服务## 前面五行是因为 3.0 版本后不让使用 all 这个列表名默认就有一个 all 为所有## 允许所有主机能够使用代理就变成了 http_access allow all 不需要 acl 申明了 即删除 acl all src 0.0.0.0/0.0.0.0 shell squid -k check
squid: ERROR: No running copy ## 删除之后正常了下面初始化 shell squid -z
2015/01/05 15:24:01| Creating Swap Directories
2015/01/05 15:24:01| /opt/squid_cache exists
2015/01/05 15:24:01| Making directories in /opt/squid_cache/00
2015/01/05 15:24:01| Making directories in /opt/squid_cache/01
2015/01/05 15:24:01| Making directories in /opt/squid_cache/02
2015/01/05 15:24:01| Making directories in /opt/squid_cache/03
2015/01/05 15:24:01| Making directories in /opt/squid_cache/04
2015/01/05 15:24:01| Making directories in /opt/squid_cache/05
2015/01/05 15:24:01| Making directories in /opt/squid_cache/06
2015/01/05 15:24:01| Making directories in /opt/squid_cache/07
2015/01/05 15:24:01| Making directories in /opt/squid_cache/08
2015/01/05 15:24:02| Making directories in /opt/squid_cache/09
2015/01/05 15:24:02| Making directories in /opt/squid_cache/0A
2015/01/05 15:24:02| Making directories in /opt/squid_cache/0B
2015/01/05 15:24:02| Making directories in /opt/squid_cache/0C
2015/01/05 15:24:02| Making directories in /opt/squid_cache/0D
2015/01/05 15:24:02| Making directories in /opt/squid_cache/0E
2015/01/05 15:24:02| Making directories in /opt/squid_cache/0F ## 看到创建了好多目录接着看一下子目录下的二级目录 shell ls /opt/squid_cache/00/
00 08 10 18 20 28 30 38 40 48 50 58 60 68 70 78 80 88 90 98 A0 A8 B0 B8 C0 C8 D0 D8 E0 E8 F0 F8
01 09 11 19 21 29 31 39 41 49 51 59 61 69 71 79 81 89 91 99 A1 A9 B1 B9 C1 C9 D1 D9 E1 E9 F1 F9
02 0A 12 1A 22 2A 32 3A 42 4A 52 5A 62 6A 72 7A 82 8A 92 9A A2 AA B2 BA C2 CA D2 DA E2 EA F2 FA
03 0B 13 1B 23 2B 33 3B 43 4B 53 5B 63 6B 73 7B 83 8B 93 9B A3 AB B3 BB C3 CB D3 DB E3 EB F3 FB
04 0C 14 1C 24 2C 34 3C 44 4C 54 5C 64 6C 74 7C 84 8C 94 9C A4 AC B4 BC C4 CC D4 DC E4 EC F4 FC
05 0D 15 1D 25 2D 35 3D 45 4D 55 5D 65 6D 75 7D 85 8D 95 9D A5 AD B5 BD C5 CD D5 DD E5 ED F5 FD
06 0E 16 1E 26 2E 36 3E 46 4E 56 5E 66 6E 76 7E 86 8E 96 9E A6 AE B6 BE C6 CE D6 DE E6 EE F6 FE
07 0F 17 1F 27 2F 37 3F 47 4F 57 5F 67 6F 77 7F 87 8F 97 9F A7 AF B7 BF C7 CF D7 DF E7 EF F7 FF 四、启动 Squid shell service squid start
正在启动 squid.................... [失败] ## 启动失败马上想到看日志 shell cat /var/log/squid/cache.log2015/01/05 15:40:31| /opt/squid_cache/swap.state: (13) Permission denied FATAL: commonUfsDirOpenSwapLog: Failed to open swap log.
## 发现这两条可疑记录权限问题但是 /opt/squid_cache 目录的属主跟属组已经是 squid 了那么想到可能是 SELinux 权限 shell getenforce
Enforcing
shell setenforce 0 ## 发现 SELinux 是开启的那么关闭试试 shell service squid start
正在启动 squid. [确定]
shell netstat -anpt | grep 3128
tcp 0 0 192.168.1.88:3128 0.0.0.0:* LISTEN 2564/(squid) ## 果然成功启动了记得修改 /etc/selinux/config 这个配置文件并重启服务器
五、测试 Squid shell curl -x 192.168.1.88:3128 www.sina.com ## 访问 www.sina.com 然后去看看缓存目录下有没有缓存文件 shell find /opt/squid_cache/ -type f
/opt/squid_cache/00/00/00000004
/opt/squid_cache/swap.state ## 其中这个 00000004 里面就是 www.sina.com 的首页内容 shell iptables -I INPUT 4 -i eth0 -p tcp --dport 3128 -j ACCEPT ## 插入一条防火墙规则用客户端浏览器测试代理服务
## 搜狗浏览器 -- 工具 -- 选项 -- 高级 -- 代理设置 -- 编辑 -- 连接 -- 局域网设置 -- 填写代理服务器 IP 端口 -- 确定
## 多次访问网站体验速度
## 再次查看缓存目录 shell find /opt/squid_cache/ -type f
/opt/squid_cache/00/00/0000001A
/opt/squid_cache/00/00/0000008E
/opt/squid_cache/00/00/000000D6
/opt/squid_cache/00/00/0000005B
/opt/squid_cache/00/00/000000B5
/opt/squid_cache/00/00/000000F6
/opt/squid_cache/00/00/000000C7
/opt/squid_cache/00/00/0000005E
/opt/squid_cache/00/00/00000078
/opt/squid_cache/00/00/00000027
/opt/squid_cache/00/00/000000AA
/opt/squid_cache/00/00/00000065
/opt/squid_cache/00/00/000000EC
/opt/squid_cache/00/00/00000045
/opt/squid_cache/00/00/000000C4
/opt/squid_cache/00/00/00000024
/opt/squid_cache/00/00/00000080
/opt/squid_cache/00/00/0000004E
/opt/squid_cache/00/00/000000A4
/opt/squid_cache/00/00/000000DE
/opt/squid_cache/00/00/000000E2
/opt/squid_cache/00/00/00000049
/opt/squid_cache/00/00/00000070
/opt/squid_cache/00/00/000000DD
/opt/squid_cache/00/00/000000BE
..... ## 发现多了好多内容这就是缓存下来的信息当下次访问相同的资源时则代理服务器会直接把缓存中的信息返回给客户端
六、访问控制列表
1、禁止 192.168.1.110 使用代理上网 acl badclientip src 192.168.1.110
http_access deny badclientip ## 顺序很重要把想拒绝的放前面然后重新加载配置文件当 1.110 访问网页时则提示拒绝访问
2、禁止一个网段上网 acl badclientnet src 192.168.1.0/24
http_access deny badclientnet ## /24 也可以写成 /255.255.255.0 的形式
3、禁止访问 IP 为 61.135.169.121 的网站 acl badwebserver dst 61.135.169.121
http_access deny badwebserver ## 这里其实直接写成 baidu.com www.baidu.com 也是可以的
4、禁止访问域名为 www.163.com 的网站 acl badwebserver dstdomain www.163.com
http_access deny badwebserver ## 用户可以访问 163.com mail.163.com
5、禁止用户访问域名中包含 163.com 的网站 acl badwebserver url_regex 163.com
http_access deny badwebserver ## 这样就可以全面禁止所有包含 163.com 的网站
6、禁止用户访问域名包含 sex 的网站 acl badwebserver url_regex sex
http_access deny badwebserver ## 用户无法访问 www.sex.com www.abc.com/sex/index.html 等站点
7、限制 IP 为 192.168.1.110 的并发最大连接数为 10 acl clientip src 192.168.1.110
acl conn10 maxconn 10
http_access deny clientip conn10 ## 通过两条 acl 来限制最大并发连接数注意 有时候做完就直接不能访问网站了是因为连接上限了可以打开一个网页的连接数已经超过了 10
8、禁止 1.0 网段的 IP 在 9:00-18:00 上网 acl clientnet src 192.168.1.0/24
acl worktime time MTWHF 9:00-18:00
http_access deny clientnet worktime ## 只能说很强大
9、禁止下载 *.mp3 *.mp4 *.exe *.zip *.rar 类型的文件 acl badfile urlpath_regex -i .mp3 .mp4 .exe .zip .rar
http_access deny badfile ## urlpath_regex 略去协议和主机名的 URL 规则表达式匹配方式-i 忽略列表值的大小写此规则可以禁止下载这些后缀的文件