forked from guoxuce/linux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsquid
272 lines (225 loc) · 12.9 KB
/
squid
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
squid, 它大多用作http服务的缓存服务器,缓存图片等静态文件可以加速客户端的请求返回速度。当然squid的功能可不仅仅局限于这么一小块。
Squid是比较知名的代理软件,它不仅可以跑在linux上还可以跑在windows以及Unix上,它的技术已经非常成熟。
目前使用Squid的用户也是十分广泛的。Squid与Linux下其它的代理软件如Apache、Socks、TIS FWTK和delegate相比,下载安装简单,配置简单灵活,
支持缓存和多种协议。
Squid之所以用的很多,是因为它的缓存功能,Squid缓存不仅可以节省宝贵的带宽资源,也可以大大降低服务器的I/O.
从经济角度考虑,它是很多网站架构中不可或缺的角色。
Squid不仅可以做正向代理,又可以做反向代理。
当作为正向代理时,Squid后面是客户端,客户端想上网不管什么网都得经过Squid.
当一个用户(客户端)想要请求一个主页时,它向Squid发出一个申请,要Squid替它请求,
然后Squid 连接用户要请求的网站并请求该主页,接着把该主页传给用户同时保留一个备份,
当别的用户请求同样的页面时,Squid把保存的备份立即传给用户,使用户觉得速度相当快。
使用正向代理时,客户端需要做一些设置,才能实现,也就是平时我们在IE选项中设置的那个代理。
而反向代理是,Squid后面为某个站点的服务器,客户端请求该站点时,会先把请求发送到Squid上,
然后Squid去处理用户的请求动作。
一个特别容易的区分:正向代理,Squid后面是客户端,客户端上网要通过Squid去上;
反向代理,Squid后面是服务器,服务器返回给用户数据需要走Squid.
也许你会问,什么时候需要配置正向代理,又什么时候配置反向代理呢?
正向代理用在企业的办公环境中,员工上网需要通过Squid代理来上网,这样可以节省网络带宽资源。
而反向代理用来搭建网站静态项(图片、html、流媒体、js、css等)的缓存服务器,它用于网站架构中。
端口3128
搭建Squid正向代理
CentOS系统自带Squid包,但是需要安装一下:
[root@localhost ~]# yum install -y squid
当然你也可以源码包编译安装,Squid官方网站为 http://www.squid-cache.org/ 当前最新版本为3.3, 下载3.1版本即可,
因为CentOS6上提供的版本也为3.1版本。如果你想编译安装Squid, 请参考提供的编译参数吧:
./configure --prefix=/usr/local/squid \
--disable-dependency-tracking \
--enable-dlmalloc \
--enable-gnuregex \
--disable-carp \
--enable-async-io=240 \
--with-pthreads \
--enable-storeio=ufs,aufs,diskd,null \
--disable-wccp \
--disable-wccpv2 \
--enable-kill-parent-hack \
--enable-cachemgr-hostname=localhost \
--enable-default-err-language=Simplify_Chinese \
--with-build-environment=POSIX_V6_ILP32_OFFBIG \
--with-maxfd=65535 \
--with-aio \
--disable-poll \
--enable-epoll \
--enable-linux-netfilter \
--enable-large-cache-files \
--disable-ident-lookups \
--enable-default-hostsfile=/etc/hosts \
--with-dl \
--with-large-files \
--enable-removal-policies=heap,lru \
--enable-delay-pools \
--enable-snmp \
--disable-internal-dns
这些参数不见得符合你的需求,只是提供一个参考,也许你在编译的过程中会遇到诸多错误,没有关系,请google。
使用CentOS中自带的squid已经满足需求,所以没有编译安装。
安装完后,可以查看squid版本:
[root@localhost ~]# squid -v
Squid Cache: Version 3.1.10
同时还可以看到squid的编译参数。下面需要配置一下squid, 来实现正向代理:
[root@localhost ~]# rm -f /etc/squid/squid.conf
[root@localhost ~]# vim /etc/squid/squid.conf
我们不使用默认配置文件,删除,重新写入如下配置:
http_port 3128
acl 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 localnet src 10.0.0.0/8 # RFC1918 possible internal network
acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl SSL_ports port 443
acl Safe_ports port 80 8080 # http
acl Safe_ports port 21 # ftp
acl Safe_ports port 443 # https
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 allow all
cache_dir aufs /data/cache 1024 16 256
cache_mem 128 MB
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 \.(jpg|png|gif|mp3|xml) 1440 50% 2880 ignore-reload
refresh_pattern . 0 20% 4320
修改:
cache_dir aufs /data/cache 1024 16 256
cache_mem 128 MB
refresh_pattern \.(jpg|png|gif|mp3|xml) 1440 50% 2880 ignore-reload
visible_hostname guoxuce.com
配置文件中有几处要简单描述一下,第一行的 “http_port 3128” 这个指的是,squid服务启动后将要监听的端口,也可以是80.
“cache_dir” 这个用来指定本地磁盘上的缓存目录,后边的1024为大小,单位是M,具体根据你的磁盘大小决定。
“cache_mem” 它用来规定缓存占用内存的大小,即把缓存的东西存到内存里,具体也需要根据你机器的内存定,
如果你的机器只是跑Squid服务,那么留给系统512M内存外,其他可以都分给squid, 但做实验的虚拟机一共才300M内存,所以只分了128M.
配置文件保存好后,可以先检测一下是否有语法错误:
[root@localhost ~]# squid -kcheck
如果提示信息为:
squid: ERROR: No running copy
这是说squid还未启动,没有关系,显示成这样说明配置文件没有问题了。在启动前还得再做一件事,就是初始化缓存目录:
[root@localhost ~]# mkdir /data/cache
[root@localhost ~]# chown -R squid:squid /data/cache/
[root@localhost ~]# squid -z
2013/06/12 16:25:14| Creating Swap Directories
2013/06/12 16:25:14| /data/cache exists
好了,初始化完成后,就可以启动squid了:
[root@localhost ~]# /etc/init.d/squid start
正在启动 squid:. [确定]
查看squid是否启动:
[root@localhost ~]# ps aux |grep squid
root 7201 0.0 0.7 14524 2444 ? Ss 16:25 0:00 squid -f /etc/squid/squid.conf
squid 7204 0.0 2.7 17468 9024 ? S 16:25 0:00 (squid) -f /etc/squid/squid.conf
squid 7205 0.0 0.2 3280 916 ? S 16:25 0:00 (unlinkd)
现在你可以在你的真机上测测看squid的正向代理了,具体IE选项不再阐述,
如果实在不懂请google,直接使用curl命令测试即可:
[root@localhost ~]# curl -xlocalhost:3128 http://www.baidu.com/
如果你看到了一大串,说明squid正向代理设置ok啦。另外我们也可以观察squid对图片的缓存:
[root@localhost ~]# curl -xlocalhost:3128 http://www.aminglinux.com/bbs/static/image/common/logo.png -I
HTTP/1.0 200 OK
Server: nginx/1.0.0
Date: Sat, 08 Jun 2013 04:30:17 GMT
Content-Type: image/png
Content-Length: 7785
Last-Modified: Wed, 13 Jan 2010 03:33:47 GMT
Accept-Ranges: bytes
X-Cache: HIT from dx_cache216.5d6d.com
X-Cache: MISS from localhost.localdomain
X-Cache-Lookup: MISS from localhost.localdomain:3128
Via: 1.0 dx_cache216.5d6d.com:80 (squid), 1.0 localhost.localdomain (squid/3.1.10)
Connection: keep-alive
[root@localhost ~]# curl -xlocalhost:3128 http://www.aminglinux.com/bbs/static/image/common/logo.png -I
HTTP/1.0 200 OK
Server: nginx/1.0.0
Content-Type: image/png
Content-Length: 7785
Last-Modified: Wed, 13 Jan 2010 03:33:47 GMT
Accept-Ranges: bytes
Date: Sat, 08 Jun 2013 04:30:17 GMT
X-Cache: HIT from dx_cache216.5d6d.com
Age: 360898
Warning: 113 localhost.localdomain (squid/3.1.10) This cache hit is still fresh and more than 1 day old
X-Cache: HIT from localhost.localdomain
X-Cache-Lookup: HIT from localhost.localdomain:3128
Via: 1.0 dx_cache216.5d6d.com:80 (squid), 1.0 localhost.localdomain (squid/3.1.10)
Connection: keep-alive
阿铭连续访问了两次阿铭论坛的logo图片,可以发现前后两次的不同,其中 “X-Cache-Lookup: HIT from localhost.localdomain:3128” 显示,
该请求已经HIT, 它直接从本地的3128端口获取了数据。
有时,我们会有这样的需求,就是想限制某些域名不能通过代理访问,或者说只想代理某几个域名,这如何做呢?在squid.conf中找到:
acl CONNECT method CONNECT
在其下面添加四行:
acl http proto HTTP
acl good_domain dstdomain .lishiming.net .aminglinux.com
http_access allow http good_domain
http_access deny http !good_domain
其中我的白名单域名为 ”.lishiming.net .aminglinux.com” ,这里的 . 表示万能匹配,前面可以是任何字符,你只需要填写你的白名单域名即可。
重启squid再来测测看:
[root@localhost ~]# /etc/init.d/squid restart
[root@localhost ~]# curl -xlocalhost:80 http://www.baidu.com/ -I
访问百度已经变为403了。如果要设置黑名单呢?道理是一样的:
acl http proto HTTP
acl bad_domain dstdomain .sina.com .souhu.com
http_access allow http !bad_domain
http_access deny http bad_domain
重启squid后,测试:
[root@localhost ~]# /etc/init.d/squid restart
[root@localhost ~]# curl -xlocalhost:80 http://www.sina.com/ -I
[root@localhost ~]# curl -xlocalhost:80 http://www.baidu.com/ -I
baidu.com可以访问,而sina.com不可以访问了。
搭建Squid反向代理
过程其实和前面的正向代理没有什么太大区别,唯一的区别是配置文件中一个地方需要改动一下。需要把:
http_port 3128
改为:
http_port 80 accel vhost vport
然后再增加你要代理的后端真实服务器信息:
cache_peer 123.125.119.147 parent 80 0 originserver name=a
cache_peer 61.135.169.125 parent 80 0 originserver name=b
cache_peer_domain a www.qq.com
cache_peer_domain b www.baidu.com
因为咱们之前没有配置网站信息,所以就拿qq.com和baidu.com来做个例子吧。
其中cache_peer为配置后端的服务器ip以及端口,name后边为要配置的域名,这里和后面的cache_peer_domain相对应。
实际的应用中,ip大多为内外ip,而域名也许会有多个,如果是squid要代理一台web上的所有域名,那么就写成这样:
cache_peer 192.168.10.111 80 0 originserver
后面连cache_peer_domain 也省了。
反向代理主要用于缓存静态项,因为诸多静态项目尤其是图片、流媒体等比较耗费带宽,在中国,联通网访问电信的资源本例就慢,
如果再去访问大流量的图片、流媒体那更会慢了,所以如果在联通网配置一个squid反向代理,让联通客户端直接访问这个联通squid,
而这些静态项已经被缓存在了squid上,这样就大大加快了访问速度。
也许你听说过CDN, 其实它的设计原理就是这样的思路。好了,我们再测一测反向代理吧。
因为修改了配置文件,所以需要重启一下squid:
[root@localhost ~]# /etc/init.d/squid restart
[root@localhost ~]# curl -xlocalhost:80 http://www.baidu.com/
[root@localhost ~]# curl -xlocalhost:80 http://www.qq.com/
[root@localhost ~]# curl -xlocalhost:80 http://www.sina.com/
你会发现,baidu.com和qq.com都能正常访问,然而sina.com访问503了,这是因为并没有加sina.com的相关设置。
还有一个知识点,需要介绍给你:
[root@localhost ~]# squid -h
Usage: squid [-cdhvzCFNRVYX] [-s | -l facility] [-f config-file] [-[au] port] [-k signal]
-a port Specify HTTP port number (default: 3128).
-d level Write debugging to stderr also.
-f file Use given config-file instead of
/etc/squid/squid.conf
-h Print help message.
-k reconfigure|rotate|shutdown|interrupt|kill|debug|check|parse
Parse configuration file, then send signal to
running copy (except -k parse) and exit.
-s | -l facility
Enable logging to syslog.
-u port Specify ICP port number (default: 3130), disable with 0.
-v Print version.
-z Create swap directories
-C Do not catch fatal signals.
-D OBSOLETE. Scheduled for removal.
-F Don't serve any requests until store is rebuilt.
-N No daemon mode.
-R Do not set REUSEADDR on port.
-S Double-check swap during rebuild.
-X Force full debugging.
-Y Only return UDP_HIT or UDP_MISS_NOFETCH during fast reload.
上面把squid命令所用到的选项全部打印出来了,但最常用的除了 squid -k check 外,还有一个那就是 squid -k reconfigure 它们俩都可以简写:
[root@localhost ~]# squid -kche
[root@localhost ~]# squid -krec
其中第二条命令表示重新加载配置文件,如果我们更改了配置文件后,不需要重启squid服务,直接使用该命令重新加载配置即可。