`
mcj8089
  • 浏览: 193637 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

使用Squid搭建局域网视频缓存代理

阅读更多
1. 安装squid2.7版本,一定要2.7版本,新版本不支持
 
$ ./configure --prefix=/usr/local/squid --sysconfdir=/usr/local/squid/etc --bindir=/usr/local/squid/bin --sbindir=/usr/local/squid/sbin --mandir=/usr/local/squid/share/man --enable-gnuregex --enable-carp --enable-async-io=80 --enable-removal-policies=heap,lru --enable-icmp --enable-delay-pools --enable-useragent-log --enable-referer-log --enable-kill-parent-hack --enable-snmp --enable-arp-acl --enable-htcp --enable-cache-digests --enable-default-err-language=Simplify_Chinese --enable-err-languages="Simplify_Chinese" --enable-poll --enable-linux-netfilter --disable-ident-lookups --enable-underscores --enable-auth="basic" --enable-basic-auth-helpers="NCSA" --enable-external-acl-helpers="ip_user" --enable-x-accelerator-vary
$ make
 
$ make install
 
2. 修改配置文件
 
$ cd /usr/local/squid/
 
在alc部分添加下面代码
 

#rewrite store url youku tudou souhu iqiyi
acl store_url url_regex -i ^http:\/\/[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\/.*(\.flv|\.m4v\?|\.f4v\?|\.mp4\?|\.ts\?|\.letv\?)
acl store_url_no_domain urlpath_regex -i ^\/(.*\/.*\.flv$)

storeurl_access allow store_url
storeurl_access allow store_url_no_domain
storeurl_access deny all

storeurl_rewrite_program /usr/local/squirm/bin/squirm
storeurl_rewrite_children 5

3. 运行squid
 
第一次运行时,先运行squid -z创建缓存文件夹。缓存文件夹在/usr/local/squid/var下创建,因此创建缓存前还需运行chmod 777 /usr/local/squid/var给该文件夹权限。 

再运行squid -d 1开启squid即可。 

如果要关闭squid,运行squid -k shutdown则是安全关闭。 
 
$ /usr/local/squid/sbin/squid
 
启动时报错了,如下
FATAL: Cannot open '/usr/local/squid/var/logs/access.log' for writing.
 The parent directory must be writeable by the
 user 'nobody', which is the cache_effective_user
 set in squid.conf.
Squid Cache (Version 2.7.STABLE9): Terminated abnormally.
CPU Usage: 0.005 seconds = 0.001 user + 0.004 sys
Maximum Resident Size: 15008 KB
Page faults with physical i/o: 0
 
修改权限解决:
 
$ chown -R nobody /usr/local/squid/var/logs/access.log
echo>var/logs/store.log
$ chown -R nobody /usr/local/squid/var/logs/store.log
$ ./sbin/squid -z
$ ./sbin/squid -d 1
 
4. 修改/etc/resolve.conf添加DNS
 
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 192.168.237.2
nameserver 192.168.199.1
nameserver 114.114.114.114
nameserver 8.8.8.8
search localdomain
 
5.  安装squirm
wget http://squirm.foote.com.au/squirm-1.0betaB.tar.gz
mv squirm-1.0betaB.tar.gz /usr/local/src
cd /usr/local/src/
tar xvf squirm-1.0betaB.tar.gz
cd squirm-1.0betaB
cd regex
./configure
make clean
make
cp -p regex.o regex.h ../
cd ../
make
make install
6.  配置squirm
 
 
$ cd /usr/loca/squirm/etc/
$ cp squirm.local.dist squirm.local
$ squirm.patterns.dist squirm.patterns
 
修改squirm.local文件,将客户端所在的IP地址段,以标准C类IP地址段为单位,以192.168.1的形式,每行一个记录,写入到该文件中。这些地址,不是squid进程所在的主机IP,而是使用squid的客户端的IP地址,即用户IP。
[root@webcache ~]# more /usr/local/squirm/etc/squirm.local
127.0.0
10.1.5
10.1.6
编辑squirm.patterns文件,重写来自优酷以及其他在线视频网站的“媒体文件URL”。
# tudou video cdn has change to youku ,but keep it!
regex http://[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}[^&]*/f4v/.*id=tudou.itemid\=([0-9]*).* http://www.tudou.com.SQUIDTUDOUA/\1
regex http://[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}[^&]*/flv/.*id=tudou.itemid\=([0-9]*).* http://www.tudou.com.SQUIDTUDOUB/\1
#site:sohu tv status:OK date:20141024
regex http://[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}/sohu/[0-9]/[0-9]*/[0-9]*/(.*)\.mp4\?key=.* http://tv.sohu.com.SQUIDSOHUTV/\1.mp4
#site:iqiyi status:OK date:201403120956  
regex http://[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}/videos/v/.*/(.*\.f4v)?  http://www.iqiyi.com.SQUIDIQIYI/\1
#site:youku part-tudou status:OK date:201403120956
regex http://[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}\.[[:digit:]]{1,3}/youku/.*/(.*-.*-.*-.*-[^?]*)(.*) http://www.youku.com.SQUIDYOUKU/\1
#site:letv status:OK date:201403122047
regex http://[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/[0-9]*/[0-9]*/[0-9]*/letv-uts/[0-9]*/(.*_mp4)/(.*)\.ts\? http://www.letv.com.SQUIDLETVA/\1/\2.mp4
regex http://[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/[0-9]*/[0-9]*/[0-9]*/letv-uts/(.*)\.letv\?.*\&video_type=(...)\&.*\&rstart=(.*)\&rend=(.*)  http://www.letv.com.SQUIDLETVB/\1-\3-\4.\2
#site:v.qq.com status:OK date:201403122102
regex http://[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/[0-9]*/.*\.qq\.com/.*/[0-9]*/[0-9]*/(.*)\.mp4\? http://v.qq.com.SQUIDQQ/\1.mp4
#site:v.ifeng.com status:OK date:201403130838
regex http://[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/data[0-9]*/documentary/[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]/(.*)\.mp4 http://v.ifeng.com.SQ
UIDIFENG/\1.mp4
#site:56 status:OK date:201403130846
regex http://[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}/.*\.56\.com/flvdownload/[0-9]*/[0-9]*/(.*)\.flv\?.* http://www.56.com.SQUID56/\1.flv
#此文件内容,需要不断的发现、归纳、丰富!
通过squidclient -h 127.0.0.1 mgr:store_rewriter命令查询squirm的运行状态。如下所示,
[root@webcache ~]# squidclient -h 127.0.0.1 mgr:store_rewriter
HTTP/1.0 200 OK
Server: squid/2.7.STABLE9
Date: Mon, 17 Mar 2014 08:32:23 GMT
Content-Type: text/plain
Expires: Mon, 17 Mar 2014 08:32:23 GMT
X-Cache: MISS from webcache
Via: 1.0 webcache:3128 (squid/2.7.STABLE9)
Connection: close

Redirector Statistics:
program: /usr/local/squirm/bin/squirm
number running: 5 of 5
requests sent: 154
replies received: 154
queue length: 0
avg service time: 0.37 msec

#      FD     PID  # Requests     # Pending       Flags      Time  Offset Request
1       9    8044         120             0                 0.000       0 (none)
2      10    8045           0             0                 0.000       0 (none)
3      11    8046           0             0                 0.000       0 (none)
4      12    8047           0             0                 0.000       0 (none)
5      14    8048           0             0                 0.000       0 (none)

Flags key:

B = BUSY
C = CLOSING
S = SHUTDOWN
[root@webcache ~]#
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics