使用Squid搭建内网视频缓存系统

 
videocache地址: http://cachevideos.com/documentation/introduction
 
软件需要: videocache、 squid-2.7、apache2、redis、python2.7
 
操作系统: Ubuntu
 
文件缓存路径: /va r/ www/html/videocache/
 
操作步骤:
 
==================
 
1.  安装Python:Ubuntu系统自带Python2.7版本,跳过本步骤
 
2. 安装Mysql(这步没有用到,可以直接跳过)
 
$ apt-get install mysql-server mysql-client libmysqlclient-dev
$ mysql -uroot -proot
$ create database videocache
 
3. 安装apache2,安装完成后apache2自动启动,可以访问http://ip/测试apache2是否安装成功
 
$ apt-get install apache2
 
4. 安装squid2.7
 
$ wget  http://www.squid-cache.org/Versions/v2/2.7/squid-2.7.STABLE9.tar.gz
$ tar -xvf squid-2.7.STABLE9.tar.gz
./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
 
5. 修改配置squid文件,用下面的内容替换
 
# --BEGIN-- videocache config for squid
#
# PASTE THIS FILE AT THE TOP OF YOUR SQUID CONFIGURATION FILE
#
# (C) Copyright Kulbir Saini < [email protected]>
# Product Website :  http://cachevideos.com/
#
# Videocache Version : 3.0
#
# This configuration is for Squid versions 2.x
url_rewrite_program /usr/bin/python /usr/share/videocache/videocache.py
url_rewrite_children 15
url_rewrite_concurrency 15

acl vc_deny_url url_regex -i \.blip\.tv\/(.*)filename \.hardsextube\.com\/videothumbs \.xtube\.com\/(.*)(Thumb|videowall)
acl vc_deny_url url_regex -i \.(jpg|jpeg|gif|png|tiff|mp3|exif|riff|bmp|webp)\?

acl vc_url url_regex -i \/youku\/[0-9A-Z]+\/[0-9A-Z\-]+\.(flv|mp4|avi|mkv|mp3|rm|rmvb|m4v|mov|wmv|3gp|mpg|mpeg)
acl vc_url url_regex -i \/(.*)key=[a-z0-9]+(.*)\.flv
acl vc_url url_regex -i \-xh\.clients\.cdn[0-9a-zA-Z]?[0-9a-zA-Z]?[0-9a-zA-Z]?\.com\/data\/(.*)\.flv
acl vc_url url_regex -i \.android\.clients\.google\.com\/market\/GetBinary\/
acl vc_url url_regex -i cs(.*)\.vk\.me\/(.*)/([a-zA-Z0-9.]+)\.(flv|mp4|avi|mkv|mp3|rm|rmvb|m4v|mov|wmv|3gp|mpg|mpeg)
acl vc_url url_regex -i video(.*)\.rutube\.ru\/(.*)/([a-zA-Z0-9.]+)\.(flv|mp4|avi|mkv|mp3|rm|rmvb|m4v|mov|wmv|3gp|mpg|mpeg)Seg[0-9]+-Frag[0-9]+

acl vc_dom_r dstdom_regex -i msn\..*\.(com|net)
acl vc_dom_r dstdom_regex -i msnbc\..*\.(com|net)
acl vc_dom_r dstdom_regex -i video\..*\.fbcdn\.net
acl vc_dom_r dstdom_regex -i myspacecdn\..*\.footprint\.net

acl vc_dom dstdomain .stream.aol.com .5min.com .msn.com .blip.tv .dmcdn.net .break.com .vimeo.com .vimeocdn.com video.thestaticvube.com
acl vc_dom dstdomain .dailymotion.com .c.wrzuta.pl .v.imwx.com .mccont.com .myspacecdn.com video-http.media-imdb.com fcache.veoh.com
acl vc_dom dstdomain .hardsextube.com .public.extremetube.phncdn.com .redtubefiles.com .video.pornhub.phncdn.com .videos.videobash.com
acl vc_dom dstdomain .public.keezmovies.com .public.keezmovies.phncdn.com .slutload-media.com .public.spankwire.com .xtube.com
acl vc_dom dstdomain .public.youporn.phncdn.com .xvideos.com .tube8.com .public.spankwire.phncdn.com .pornhub.com

refresh_pattern \.video\.msn\.com\/.*\/entityid\/[a-zA-Z0-9_\-]+\? 20 80% 40 ignore-no-cache override-expire override-lastmod ignore-private
refresh_pattern \.android\.clients\.google\.com\/market\/GetBinary\/ 20 80% 40 ignore-no-cache override-expire override-lastmod ignore-private
refresh_pattern stream\.aol\.com\/(.*)/[a-zA-Z0-9]+\/(.*)\.(flv|mp4) 20 80% 40 ignore-no-cache override-expire override-lastmod ignore-private
refresh_pattern videos\.5min\.com\/(.*)/[0-9_]+\.(mp4|flv) 20 80% 40 ignore-no-cache override-expire override-lastmod ignore-private
refresh_pattern \.blip\.tv\/(.*)\.(m4v|mp4|flv) 20 80% 40 ignore-no-cache override-expire override-lastmod ignore-private
refresh_pattern proxy[a-z0-9\-]?[a-z0-9]?[a-z0-9]?[a-z0-9]?\.dailymotion\.com\/(.*)\.(flv|on2|mp4|avi|mkv|mp3|rm|rmvb|m4v|mov|wmv|3gp|mpg|mpeg) 20 80% 40 ignore-no-cache override-expire override-lastmod ignore-private
refresh_pattern vid\.akm\.dailymotion\.com\/(.*)\.(flv|on2|mp4|avi|mkv|mp3|rm|rmvb|m4v|mov|wmv|3gp|mpg|mpeg) 20 80% 40 ignore-no-cache override-expire override-lastmod ignore-private
refresh_pattern \.dmcdn\.net\/(.*)\.(flv|on2|mp4|avi|mkv|mp3|rm|rmvb|m4v|mov|wmv|3gp|mpg|mpeg) 20 80% 40 ignore-no-cache override-expire override-lastmod ignore-private
refresh_pattern video\.(.*)\.fbcdn\.net\/(.*)/[0-9_]+\.(mp4|flv|avi|mkv|m4v|mov|wmv|3gp|mpg|mpeg) 20 80% 40 ignore-no-cache override-expire override-lastmod ignore-private
refresh_pattern (.*)\.myspacecdn\.com\/(.*)\/[a-zA-Z0-9]+\/vid\.(flv|mp4|avi|mkv|mp3|rm|rmvb|m4v|mov|wmv|3gp|mpg|mpeg) 20 80% 40 ignore-no-cache override-expire override-lastmod ignore-private
refresh_pattern (.*)\.myspacecdn\.(.*)\.footprint\.net\/(.*)\/[a-zA-Z0-9]+\/vid\.(flv|mp4|avi|mkv|mp3|rm|rmvb|m4v|mov|wmv|3gp|mpg|mpeg) 20 80% 40 ignore-no-cache override-expire override-lastmod ignore-private
refresh_pattern c\.wrzuta\.pl\/w[a-zA-Z0-9]+\/[a-zA-Z0-9]+$ 20 80% 40 ignore-no-cache override-expire override-lastmod ignore-private
refresh_pattern \.hardsextube\.com\/.*\/.*\.(flv|mp4|avi|mkv|mp3|rm|rmvb|m4v|mov|wmv|3gp|mpg|mpeg) 20 80% 40 ignore-no-cache override-expire override-lastmod ignore-private
refresh_pattern -xh\.clients\.cdn[0-9a-zA-Z]?[0-9a-zA-Z]?[0-9a-zA-Z]?\.com\/data\/(.*)\.flv 20 80% 40 ignore-no-cache override-expire override-lastmod ignore-private

acl vc_deny_url url_regex -i crossdomain.xml
acl vc_method method GET
acl vc_header req_header X-Requested-With -i videocache
url_rewrite_access deny !vc_method
url_rewrite_access deny vc_header
url_rewrite_access deny vc_deny_url
url_rewrite_access allow vc_dom
url_rewrite_access allow vc_url
url_rewrite_access allow vc_dom_r
redirector_bypass on
#These lines are needed.
strip_query_terms off
maximum_object_size 200 MB
acl this_machine src 127.0.0.1
http_access allow this_machine
# --END-- videocache config for squid

    acl localnet src 192.0.0.0/8
    acl all src all
    acl manager proto cache_object
    acl localhost src 127.0.0.1/32
    acl to_localhost dst 127.0.0.0/8 0.0.0.0/32
    acl SSL_ports port 443
    acl CONNECT method CONNECT

    http_access allow manager localhost
    http_access deny manager

    http_access deny CONNECT !SSL_ports
    http_access allow localnet
    http_access deny all

    http_port 3128
    hierarchy_stoplist cgi-bin ?
    access_log /usr/local/squid/var/logs/access.log 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
    acl shoutcast rep_header X-HTTP09-First-Line ^ICY.[0-9]
    upgrade_http0.9 deny shoutcast
    acl apache rep_header Server ^Apache
    broken_vary_encoding allow apache
    coredump_dir /usr/local/squid/var/cache

    cache_mem 400 MB
    maximum_object_size 4096000 KB
    minimum_object_size 0 KB
    maximum_object_size_in_memory 2560 KB

    cache_dir ufs /usr/local/squid/var/cache-data 20480 16 256
    cache_swap_low 80
    cache_swap_high 97
    strip_query_terms off
    request_body_max_size 5 MB
    memory_pools on
    memory_pools_limit 50 MB 
 
6. 配置本地DNS服务器(本步骤可省略),添加namesever指向本地的DNS
 
$ vi /etc/resolve.conf
 
nameserver 114.114.114.114
nameserver 8.8.8.8
search localdomain
 
7. 安装redis,安装完成后自动启动
 
$ apt-get install redis-server
8. 安装videocache,videocache对python依赖,需要很多类库
 
$ tar -xvf videocache-master.tar
$ cd videocache-master
$ ./install.sh
 
如果代理服务器的IP地址发生变化,请修改/etc/videocache.conf文件中的cache_host=你的IP地址
 
安装过程中会自动安装缺少的python类库,比如缺少类库名字叫A,也可以用手动执行 apt-get install python-A 进行安装
 
安装完成后,会弹出五部,照做就行
 
$ vc-update
$ apachectl -k restart
$ vc-scheduler -s restart
 
8. 启动squid
 
$ cd /usr/local/squid/
$ chmod 777 ./var
$ ./sbin/squid -k shutdown
$ ./sbin/squid  -z
$ ./sbin/squid -d 1
 
第一次运行时,先运行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
 
==================
 
现在缓存系统已经启动,所有缓存文件缓存到 /var/www/html/videocache目录下,可以通过  http://ip/videocache/   查看缓存的内容
 
缓存服务器的默认端口是3128
 
===========================
 
下面是squid常用的命令,squidclient在squid安装bin目录下
 
squidclient -p 80 mgr:info      #取得squid运行状态信息:
squidclient -p 80 mgr:mem     #取得squid内存使用情况:
squidclient -p 80 mgr:objects #取得squid已经缓存的列表,使用时要小心,可能会造成squid崩溃:
squidclient -p 80 mgr:diskd  #取得squid的磁盘使用情况:
squidclient -p 80-m PURGE http://www.361way.com  #强制更新某个url,即清理某连接的squid

 

你可能感兴趣的:(Squid)