Squid and youtube caching
Posted in News on August 28th, 2011 by adminHello Everybody,
I’ve spent considerable time making squid cache youtube.
I firstly tried http://eu.squid-cache.org/ConfigExamples/DynamicContent/YouTube using the webserver. However, a few problems where identified, firstly, you can only delete content based on date. I also identified that if the user fast forwards the video before it finishes downloading, it will start to save a nother copy of the video as the file size is now different. The method I used:
squid.conf:
acl store_rewrite_list url_regex ^http://(.*?)/get_video\?
acl store_rewrite_list url_regex ^http://(.*?)/videodownload\?
acl store_rewrite_list url_regex ^http://(.*?)/videoplayback\?
cache allow store_rewrite_list
# Had to uncomment this again, because I couln’d login to google mail using IE6 (firefox had no trouble):
acl QUERY urlpath_regex cgi-bin \?
cache deny QUERY
refresh_pattern (get_video\?|videoplayback\?|videodownload\?) 5259487 99999999% 5259487 override-expire ignore-reload ignore-private negative-ttl=0
storeurl_access allow store_rewrite_list
storeurl_access deny all
storeurl_rewrite_program /etc/squid/youtube.pl
youtube.pl
#!/usr/bin/perl
$|=1;
while (<>) {
@X = split;
$x = $X[0];
$_ = $X[1];
if (m/^http:\/\/([0-9.]{4}|.*\.youtube\.com|.*\.googlevideo\.com|.*\.video\.google\.com).*?\&(itag=22).*?\&(id=[a-zA-Z0-9]*)/) {
print $x . “http://video-srv.youtube.com.SQUIDINTERNAL/” . $2 . “&” . $3 . “\n”;
# youtube Normal screen always HD itag 35, Normal screen never HD itag 34, itag=18 <–normal?
} elsif (m/^http:\/\/([0-9.]{4}|.*\.youtube\.com|.*\.googlevideo\.com|.*\.video\.google\.com).*?\&(itag=[0-9]*).*?\&(id=[a-zA-Z0-9]*)/) {
print $x . “http://video-srv.youtube.com.SQUIDINTERNAL/” . $2 . “&” . $3 . “\n”;
} else {
print $x . $_ . “\n”;
}
}
