<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://lms.onnocenter.or.id/wiki/index.php?action=history&amp;feed=atom&amp;title=Squid%3A_Cache_Youtube_dibantu_Nginx</id>
	<title>Squid: Cache Youtube dibantu Nginx - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://lms.onnocenter.or.id/wiki/index.php?action=history&amp;feed=atom&amp;title=Squid%3A_Cache_Youtube_dibantu_Nginx"/>
	<link rel="alternate" type="text/html" href="https://lms.onnocenter.or.id/wiki/index.php?title=Squid:_Cache_Youtube_dibantu_Nginx&amp;action=history"/>
	<updated>2026-04-23T02:35:03Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.45.1</generator>
	<entry>
		<id>https://lms.onnocenter.or.id/wiki/index.php?title=Squid:_Cache_Youtube_dibantu_Nginx&amp;diff=36346&amp;oldid=prev</id>
		<title>Onnowpurbo: New page:  Youtube caching with Squid + Nginx Filed under: Linux Related — Tags: nginx cache, youtube cache, youtube cache with nginx — Syed Jahanzaib / Pinochio~:) @ 11:52 AM             31 Vot...</title>
		<link rel="alternate" type="text/html" href="https://lms.onnocenter.or.id/wiki/index.php?title=Squid:_Cache_Youtube_dibantu_Nginx&amp;diff=36346&amp;oldid=prev"/>
		<updated>2013-04-03T00:04:48Z</updated>

		<summary type="html">&lt;p&gt;New page:  Youtube caching with Squid + Nginx Filed under: Linux Related — Tags: nginx cache, youtube cache, youtube cache with nginx — Syed Jahanzaib / Pinochio~:) @ 11:52 AM             31 Vot...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt; Youtube caching with Squid + Nginx&lt;br /&gt;
Filed under: Linux Related — Tags: nginx cache, youtube cache, youtube cache with nginx — Syed Jahanzaib / Pinochio~:) @ 11:52 AM&lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
 &lt;br /&gt;
31 Votes&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Advantages of Youtube Caching   !!!&lt;br /&gt;
&lt;br /&gt;
In most part of the world, bandwidth is very expensive, therefore it is (in some scenarios) very useful to Cache Youtube videos or any other flash videos, so if one of user downloads video / flash file , why again the same user or other user can’t download the same file from the CACHE, why he sucking the internet pipe for same content again n again?&lt;br /&gt;
Peoples on same LAN ,sometimes watch similar videos. If I put some youtube video link on on FACEBOOK, TWITTER or likewise , and all my friend will  watch that video and that particular video gets viewed many times in few hours. Usually the videos are shared over facebook or other social networking sites so the chances are high for multiple hits per popular videos for my LAN users / friends.&lt;br /&gt;
&lt;br /&gt;
This is the reason why I wrote this article.&lt;br /&gt;
Disadvantages of Youtube Caching   !!!&lt;br /&gt;
&lt;br /&gt;
The chances, that another user will watch the same video, is really slim. if I search for something specific on youtube, i get more then hundreds of search results for same video. What is the chance that another user will search for the same thing, and will click on the same link / result? Youtube hosts more than 10 million videos. Which is too much to cache anyway. You need lot of space to cache videos. Also accordingly you will be needing ultra modern fast hardware with tons of  SPACE to handle such kind of cache giant. anyhow Try it&lt;br /&gt;
&lt;br /&gt;
AFAIK you are not supposed to cache youtube videos, youtube don’t like it. I don’t understand why. Probably because their ranking mechanism relies on views, and possibly completed views, which wouldn’t be measurable if the content was served from a local cache.&lt;br /&gt;
&lt;br /&gt;
After unsuccessful struggling with storeurl.pl method , I was searching for alternate method to cache youtube videos. Finally I found ruby base method using Nginx to cache YT. Using this method I was able to cache all Youtube videos almost perfectly. (not 100%, but it works fine in most cases with some modification.I am sure there will be some improvement in near future).&lt;br /&gt;
Updated: 24thth August, 2012&lt;br /&gt;
&lt;br /&gt;
Thanks to Mr. Eliezer Croitoru &amp;amp; Mr.Christian Loth &amp;amp; others for there kind guidance.&lt;br /&gt;
&lt;br /&gt;
Following components were used in this guide.&lt;br /&gt;
&lt;br /&gt;
Proxy Server Configuration:&lt;br /&gt;
Ubuntu Desktop 10.4&lt;br /&gt;
Nginix version: nginx/0.7.65&lt;br /&gt;
Squid Cache: Version 2.7.STABLE7&lt;br /&gt;
&lt;br /&gt;
Client Configuration for testing videos:&lt;br /&gt;
Windows XP with Internet Explorer 6&lt;br /&gt;
Windows 7 with Internet Explorer 8&lt;br /&gt;
&lt;br /&gt;
Lets start with the Proxy Server Configuration:&lt;br /&gt;
1) Update Ubuntu&lt;br /&gt;
&lt;br /&gt;
First install Ubuntu, After installation, configure its networking components, then update it by following command&lt;br /&gt;
apt-get install update&lt;br /&gt;
2) Install SSH Server [Optional]&lt;br /&gt;
&lt;br /&gt;
Now install SSH server so that you can manage your server remotely using PUTTY or any other ssh tool.&lt;br /&gt;
&lt;br /&gt;
apt-get install openssh-server&lt;br /&gt;
3) Install Squid Server&lt;br /&gt;
&lt;br /&gt;
Now install Squid Server by following command&lt;br /&gt;
apt-get install squid&lt;br /&gt;
[This will install squid 2.7 by default]&lt;br /&gt;
&lt;br /&gt;
Now edit squid configuration files by using following command&lt;br /&gt;
&lt;br /&gt;
nano /etc/squid/squid.conf&lt;br /&gt;
&lt;br /&gt;
Remove all lines and paste the following data&lt;br /&gt;
1&lt;br /&gt;
2&lt;br /&gt;
3&lt;br /&gt;
4&lt;br /&gt;
5&lt;br /&gt;
6&lt;br /&gt;
7&lt;br /&gt;
8&lt;br /&gt;
9&lt;br /&gt;
10&lt;br /&gt;
11&lt;br /&gt;
12&lt;br /&gt;
13&lt;br /&gt;
14&lt;br /&gt;
15&lt;br /&gt;
16&lt;br /&gt;
17&lt;br /&gt;
18&lt;br /&gt;
19&lt;br /&gt;
20&lt;br /&gt;
21&lt;br /&gt;
22&lt;br /&gt;
23&lt;br /&gt;
24&lt;br /&gt;
25&lt;br /&gt;
26&lt;br /&gt;
27&lt;br /&gt;
28&lt;br /&gt;
29&lt;br /&gt;
30&lt;br /&gt;
31&lt;br /&gt;
32&lt;br /&gt;
33&lt;br /&gt;
34&lt;br /&gt;
35&lt;br /&gt;
36&lt;br /&gt;
37&lt;br /&gt;
38&lt;br /&gt;
39&lt;br /&gt;
40&lt;br /&gt;
41&lt;br /&gt;
42&lt;br /&gt;
43&lt;br /&gt;
44&lt;br /&gt;
45&lt;br /&gt;
46&lt;br /&gt;
47&lt;br /&gt;
48&lt;br /&gt;
49&lt;br /&gt;
50&lt;br /&gt;
51&lt;br /&gt;
52&lt;br /&gt;
53&lt;br /&gt;
54&lt;br /&gt;
55&lt;br /&gt;
56&lt;br /&gt;
57&lt;br /&gt;
58&lt;br /&gt;
59&lt;br /&gt;
60&lt;br /&gt;
61&lt;br /&gt;
62&lt;br /&gt;
63&lt;br /&gt;
64&lt;br /&gt;
65&lt;br /&gt;
66&lt;br /&gt;
67&lt;br /&gt;
68&lt;br /&gt;
69&lt;br /&gt;
70&lt;br /&gt;
71&lt;br /&gt;
72&lt;br /&gt;
73&lt;br /&gt;
74&lt;br /&gt;
75&lt;br /&gt;
76&lt;br /&gt;
77&lt;br /&gt;
78&lt;br /&gt;
79&lt;br /&gt;
80&lt;br /&gt;
81&lt;br /&gt;
82&lt;br /&gt;
83&lt;br /&gt;
84&lt;br /&gt;
85&lt;br /&gt;
86&lt;br /&gt;
87&lt;br /&gt;
88&lt;br /&gt;
89&lt;br /&gt;
90&lt;br /&gt;
91&lt;br /&gt;
92&lt;br /&gt;
93&lt;br /&gt;
94&lt;br /&gt;
95&lt;br /&gt;
96&lt;br /&gt;
97&lt;br /&gt;
	&lt;br /&gt;
# SQUID 2.7/ Nginx TEST CONFIG FILE&lt;br /&gt;
# Email: aacable@hotmail.com&lt;br /&gt;
# Web  : http://aacable.wordpress.com&lt;br /&gt;
# PORT and Transparent Option&lt;br /&gt;
http_port 8080 transparent&lt;br /&gt;
server_http11 on&lt;br /&gt;
icp_port 0&lt;br /&gt;
 &lt;br /&gt;
# Cache is set to 5GB in this example (zaib)&lt;br /&gt;
store_dir_select_algorithm round-robin&lt;br /&gt;
cache_dir aufs /cache1 5000 16 256&lt;br /&gt;
cache_replacement_policy heap LFUDA&lt;br /&gt;
memory_replacement_policy heap LFUDA&lt;br /&gt;
 &lt;br /&gt;
# If you want to enable DATE time n SQUID Logs,use following&lt;br /&gt;
emulate_httpd_log on&lt;br /&gt;
logformat squid %tl %6tr %&amp;gt;a %Ss/%03Hs %&amp;lt;st %rm %ru %un %Sh/%&amp;lt;A %mt&lt;br /&gt;
log_fqdn off&lt;br /&gt;
 &lt;br /&gt;
# How much days to keep users access web logs&lt;br /&gt;
# You need to rotate your log files with a cron job. For example:&lt;br /&gt;
# 0 0 * * * /usr/local/squid/bin/squid -k rotate&lt;br /&gt;
logfile_rotate 14&lt;br /&gt;
debug_options ALL,1&lt;br /&gt;
cache_access_log /var/log/squid/access.log&lt;br /&gt;
cache_log /var/log/squid/cache.log&lt;br /&gt;
cache_store_log /var/log/squid/store.log&lt;br /&gt;
 &lt;br /&gt;
#[zaib] I used DNSAMSQ service for fast dns resolving&lt;br /&gt;
#so install by using &amp;quot;apt-get install dnsmasq&amp;quot; first&lt;br /&gt;
dns_nameservers 127.0.0.1 221.132.112.8&lt;br /&gt;
 &lt;br /&gt;
#ACL Section&lt;br /&gt;
acl all src 0.0.0.0/0.0.0.0&lt;br /&gt;
acl manager proto cache_object&lt;br /&gt;
acl localhost src 127.0.0.1/255.255.255.255&lt;br /&gt;
acl to_localhost dst 127.0.0.0/8&lt;br /&gt;
acl SSL_ports port 443 563 # https, snews&lt;br /&gt;
acl SSL_ports port 873 # rsync&lt;br /&gt;
acl Safe_ports port 80 # http&lt;br /&gt;
acl Safe_ports port 21 # ftp&lt;br /&gt;
acl Safe_ports port 443 563 # https, snews&lt;br /&gt;
acl Safe_ports port 70 # gopher&lt;br /&gt;
acl Safe_ports port 210 # wais&lt;br /&gt;
acl Safe_ports port 1025-65535 # unregistered ports&lt;br /&gt;
acl Safe_ports port 280 # http-mgmt&lt;br /&gt;
acl Safe_ports port 488 # gss-http&lt;br /&gt;
acl Safe_ports port 591 # filemaker&lt;br /&gt;
acl Safe_ports port 777 # multiling http&lt;br /&gt;
acl Safe_ports port 631 # cups&lt;br /&gt;
acl Safe_ports port 873 # rsync&lt;br /&gt;
acl Safe_ports port 901 # SWAT&lt;br /&gt;
acl purge method PURGE&lt;br /&gt;
acl CONNECT method CONNECT&lt;br /&gt;
http_access allow manager localhost&lt;br /&gt;
http_access deny manager&lt;br /&gt;
http_access allow purge localhost&lt;br /&gt;
http_access deny purge&lt;br /&gt;
http_access deny !Safe_ports&lt;br /&gt;
http_access deny CONNECT !SSL_ports&lt;br /&gt;
http_access allow localhost&lt;br /&gt;
http_access allow all&lt;br /&gt;
http_reply_access allow all&lt;br /&gt;
icp_access allow all&lt;br /&gt;
 &lt;br /&gt;
#[zaib]I used UBUNTU so user is proxy, in FEDORA you may use use squid&lt;br /&gt;
cache_effective_user proxy&lt;br /&gt;
cache_effective_group proxy&lt;br /&gt;
cache_mgr aacable@hotmail.com&lt;br /&gt;
visible_hostname proxy.aacable.net&lt;br /&gt;
unique_hostname aacable@hotmail.com&lt;br /&gt;
 &lt;br /&gt;
cache_mem 8 MB&lt;br /&gt;
minimum_object_size 0 bytes&lt;br /&gt;
maximum_object_size 100 MB&lt;br /&gt;
maximum_object_size_in_memory 128 KB&lt;br /&gt;
 &lt;br /&gt;
refresh_pattern ^ftp: 1440 20% 10080&lt;br /&gt;
refresh_pattern ^gopher: 1440 0% 1440&lt;br /&gt;
refresh_pattern -i (/cgi-bin/|\?) 0 0% 0&lt;br /&gt;
refresh_pattern (Release|Packages(.gz)*)$ 0 20% 2880&lt;br /&gt;
refresh_pattern . 0 50% 4320&lt;br /&gt;
acl apache rep_header Server ^Apache&lt;br /&gt;
broken_vary_encoding allow apache&lt;br /&gt;
 &lt;br /&gt;
# Youtube Cache Section [zaib]&lt;br /&gt;
url_rewrite_program /etc/nginx/nginx.rb&lt;br /&gt;
url_rewrite_host_header off&lt;br /&gt;
acl youtube_videos url_regex -i ^http://[^/]+\.youtube\.com/videoplayback\?&lt;br /&gt;
acl range_request req_header Range .&lt;br /&gt;
acl begin_param url_regex -i [?&amp;amp;]begin=&lt;br /&gt;
acl id_param url_regex -i [?&amp;amp;]id=&lt;br /&gt;
acl itag_param url_regex -i [?&amp;amp;]itag=&lt;br /&gt;
acl sver3_param url_regex -i [?&amp;amp;]sver=3&lt;br /&gt;
cache_peer 127.0.0.1 parent 8081 0 proxy-only no-query connect-timeout=10&lt;br /&gt;
cache_peer_access 127.0.0.1 allow youtube_videos id_param itag_param sver3_param !begin_param !range_request&lt;br /&gt;
cache_peer_access 127.0.0.1 deny all&lt;br /&gt;
&lt;br /&gt;
Save &amp;amp; Exit.&lt;br /&gt;
4) Install Nginx&lt;br /&gt;
&lt;br /&gt;
Now install Nginix by&lt;br /&gt;
apt-get install nginx&lt;br /&gt;
&lt;br /&gt;
Now edit its config file by using following command&lt;br /&gt;
nano /etc/nginx/nginx.conf&lt;br /&gt;
&lt;br /&gt;
Remove all lines and paste the following data&lt;br /&gt;
1&lt;br /&gt;
2&lt;br /&gt;
3&lt;br /&gt;
4&lt;br /&gt;
5&lt;br /&gt;
6&lt;br /&gt;
7&lt;br /&gt;
8&lt;br /&gt;
9&lt;br /&gt;
10&lt;br /&gt;
11&lt;br /&gt;
12&lt;br /&gt;
13&lt;br /&gt;
14&lt;br /&gt;
15&lt;br /&gt;
16&lt;br /&gt;
17&lt;br /&gt;
18&lt;br /&gt;
19&lt;br /&gt;
20&lt;br /&gt;
21&lt;br /&gt;
22&lt;br /&gt;
23&lt;br /&gt;
24&lt;br /&gt;
25&lt;br /&gt;
26&lt;br /&gt;
27&lt;br /&gt;
28&lt;br /&gt;
29&lt;br /&gt;
30&lt;br /&gt;
31&lt;br /&gt;
32&lt;br /&gt;
33&lt;br /&gt;
34&lt;br /&gt;
35&lt;br /&gt;
36&lt;br /&gt;
37&lt;br /&gt;
38&lt;br /&gt;
39&lt;br /&gt;
40&lt;br /&gt;
41&lt;br /&gt;
42&lt;br /&gt;
43&lt;br /&gt;
44&lt;br /&gt;
45&lt;br /&gt;
46&lt;br /&gt;
47&lt;br /&gt;
48&lt;br /&gt;
49&lt;br /&gt;
50&lt;br /&gt;
51&lt;br /&gt;
52&lt;br /&gt;
53&lt;br /&gt;
54&lt;br /&gt;
55&lt;br /&gt;
56&lt;br /&gt;
57&lt;br /&gt;
	&lt;br /&gt;
# This config file is not written by me,&lt;br /&gt;
# My Email address is inserted Just for tracking purposes&lt;br /&gt;
# For more info, visit http://code.google.com/p/youtube-cache/&lt;br /&gt;
# Syed Jahanzaib / aacable [at] hotmail.com&lt;br /&gt;
user www-data;&lt;br /&gt;
worker_processes 4;&lt;br /&gt;
pid /var/run/nginx.pid;&lt;br /&gt;
events {&lt;br /&gt;
worker_connections 768;&lt;br /&gt;
}&lt;br /&gt;
http {&lt;br /&gt;
sendfile on;&lt;br /&gt;
tcp_nopush on;&lt;br /&gt;
tcp_nodelay on;&lt;br /&gt;
keepalive_timeout 65;&lt;br /&gt;
types_hash_max_size 2048;&lt;br /&gt;
include /etc/nginx/mime.types;&lt;br /&gt;
default_type application/octet-stream;&lt;br /&gt;
access_log /var/log/nginx/access.log;&lt;br /&gt;
error_log /var/log/nginx/error.log;&lt;br /&gt;
gzip on;&lt;br /&gt;
gzip_static on;&lt;br /&gt;
gzip_comp_level 6;&lt;br /&gt;
gzip_disable .msie6.;&lt;br /&gt;
gzip_vary on;&lt;br /&gt;
gzip_types text/plain text/css text/xml text/javascript application/json application/x-javascript application/xml application/xml+rss;&lt;br /&gt;
gzip_proxied expired no-cache no-store private auth;&lt;br /&gt;
gzip_buffers 16 8k;&lt;br /&gt;
gzip_http_version 1.1;&lt;br /&gt;
include /etc/nginx/conf.d/*.conf;&lt;br /&gt;
include /etc/nginx/sites-enabled/*;&lt;br /&gt;
# starting youtube section&lt;br /&gt;
server {&lt;br /&gt;
listen 127.0.0.1:8081;&lt;br /&gt;
location / {&lt;br /&gt;
root /usr/local/www/nginx_cache/files;&lt;br /&gt;
#try_files &amp;quot;/id=$arg_id.itag=$arg_itag&amp;quot; @proxy_youtube; # Old one&lt;br /&gt;
#try_files  &amp;quot;$uri&amp;quot; &amp;quot;/id=$arg_id.itag=$arg_itag.flv&amp;quot; &amp;quot;/id=$arg_id-range=$arg_range.itag=$arg_itag.flv&amp;quot; @proxy_youtube; #old2&lt;br /&gt;
try_files &amp;quot;/id=$arg_id.itag=$arg_itag.range=$arg_range.algo=$arg_algorithm&amp;quot; @proxy_youtube;&lt;br /&gt;
}&lt;br /&gt;
location @proxy_youtube {&lt;br /&gt;
resolver 221.132.112.8;&lt;br /&gt;
proxy_pass http://$host$request_uri;&lt;br /&gt;
proxy_temp_path &amp;quot;/usr/local/www/nginx_cache/tmp&amp;quot;;&lt;br /&gt;
#proxy_store &amp;quot;/usr/local/www/nginx_cache/files/id=$arg_id.itag=$arg_itag&amp;quot;; # Old 1&lt;br /&gt;
proxy_store &amp;quot;/usr/local/www/nginx_cache/files/id=$arg_id.itag=$arg_itag.range=$arg_range.algo=$arg_algorithm&amp;quot;;&lt;br /&gt;
proxy_ignore_client_abort off;&lt;br /&gt;
proxy_method GET;&lt;br /&gt;
proxy_set_header X-YouTube-Cache &amp;quot;aacable@hotmail.com&amp;quot;;&lt;br /&gt;
proxy_set_header Accept &amp;quot;video/*&amp;quot;;&lt;br /&gt;
proxy_set_header User-Agent &amp;quot;YouTube Cacher (nginx)&amp;quot;;&lt;br /&gt;
proxy_set_header Accept-Encoding &amp;quot;&amp;quot;;&lt;br /&gt;
proxy_set_header Accept-Language &amp;quot;&amp;quot;;&lt;br /&gt;
proxy_set_header Accept-Charset &amp;quot;&amp;quot;;&lt;br /&gt;
proxy_set_header Cache-Control &amp;quot;&amp;quot;;}&lt;br /&gt;
}&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
Save &amp;amp; Exit.&lt;br /&gt;
&lt;br /&gt;
Now Create directories to hold cache files&lt;br /&gt;
&lt;br /&gt;
mkdir /usr/local/www&lt;br /&gt;
mkdir /usr/local/www/nginx_cache&lt;br /&gt;
mkdir /usr/local/www/nginx_cache/tmp&lt;br /&gt;
mkdir /usr/local/www/nginx_cache/files&lt;br /&gt;
chown www-data /usr/local/www/nginx_cache/files/ -Rf&lt;br /&gt;
&lt;br /&gt;
Now create nginx .rb file&lt;br /&gt;
&lt;br /&gt;
touch /etc/nginx/nginx.rb&lt;br /&gt;
chmod 755 /etc/nginx/nginx.rb&lt;br /&gt;
nano /etc/nginx/nginx.rb&lt;br /&gt;
&lt;br /&gt;
Paste the following data in this newly created file&lt;br /&gt;
1&lt;br /&gt;
2&lt;br /&gt;
3&lt;br /&gt;
4&lt;br /&gt;
5&lt;br /&gt;
6&lt;br /&gt;
7&lt;br /&gt;
8&lt;br /&gt;
9&lt;br /&gt;
10&lt;br /&gt;
11&lt;br /&gt;
12&lt;br /&gt;
13&lt;br /&gt;
14&lt;br /&gt;
15&lt;br /&gt;
16&lt;br /&gt;
17&lt;br /&gt;
18&lt;br /&gt;
19&lt;br /&gt;
20&lt;br /&gt;
21&lt;br /&gt;
22&lt;br /&gt;
23&lt;br /&gt;
24&lt;br /&gt;
25&lt;br /&gt;
26&lt;br /&gt;
27&lt;br /&gt;
28&lt;br /&gt;
29&lt;br /&gt;
30&lt;br /&gt;
31&lt;br /&gt;
32&lt;br /&gt;
33&lt;br /&gt;
34&lt;br /&gt;
35&lt;br /&gt;
36&lt;br /&gt;
37&lt;br /&gt;
38&lt;br /&gt;
39&lt;br /&gt;
40&lt;br /&gt;
41&lt;br /&gt;
42&lt;br /&gt;
43&lt;br /&gt;
44&lt;br /&gt;
45&lt;br /&gt;
46&lt;br /&gt;
47&lt;br /&gt;
48&lt;br /&gt;
49&lt;br /&gt;
50&lt;br /&gt;
51&lt;br /&gt;
	&lt;br /&gt;
#!/usr/bin/env ruby1.8&lt;br /&gt;
# This script is not written by me,&lt;br /&gt;
# My Email address is inserted Just for tracking purposes&lt;br /&gt;
# For more info, visit http://code.google.com/p/youtube-cache/&lt;br /&gt;
# Syed Jahanzaib / aacable [at] hotmail.com&lt;br /&gt;
# url_rewrite_program &amp;lt;path&amp;gt;/nginx.rb&lt;br /&gt;
# url_rewrite_host_header off&lt;br /&gt;
 &lt;br /&gt;
require &amp;quot;syslog&amp;quot;&lt;br /&gt;
require &amp;quot;base64&amp;quot;&lt;br /&gt;
 &lt;br /&gt;
class SquidRequest&lt;br /&gt;
attr_accessor :url, :user&lt;br /&gt;
attr_reader :client_ip, :method&lt;br /&gt;
 &lt;br /&gt;
def method=(s)&lt;br /&gt;
@method = s.downcase&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def client_ip=(s)&lt;br /&gt;
@client_ip = s.split(&amp;#039;/&amp;#039;).first&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def read_requests&lt;br /&gt;
# URL &amp;lt;SP&amp;gt; client_ip &amp;quot;/&amp;quot; fqdn &amp;lt;SP&amp;gt; user &amp;lt;SP&amp;gt; method [&amp;lt;SP&amp;gt; kvpairs]&amp;lt;NL&amp;gt;&lt;br /&gt;
STDIN.each_line do |ln|&lt;br /&gt;
r = SquidRequest.new&lt;br /&gt;
r.url, r.client_ip, r.user, r.method, *dummy = ln.rstrip.split(&amp;#039; &amp;#039;)&lt;br /&gt;
(STDOUT &amp;lt;&amp;lt; &amp;quot;#{yield r}\n&amp;quot;).flush&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def log(msg)&lt;br /&gt;
Syslog.log(Syslog::LOG_ERR, &amp;quot;%s&amp;quot;, msg)&lt;br /&gt;
end&lt;br /&gt;
 &lt;br /&gt;
def main&lt;br /&gt;
Syslog.open(&amp;#039;nginx.rb&amp;#039;, Syslog::LOG_PID)&lt;br /&gt;
log(&amp;quot;Started&amp;quot;)&lt;br /&gt;
 &lt;br /&gt;
read_requests do |r|&lt;br /&gt;
if r.method == &amp;#039;get&amp;#039; &amp;amp;&amp;amp; r.url !~ /[?&amp;amp;]begin=/ &amp;amp;&amp;amp; r.url =~ %r{\Ahttp://[^/]+\.youtube\.com/(videoplayback\?.*)\z}&lt;br /&gt;
log(&amp;quot;YouTube Video [#{r.url}].&amp;quot;)&lt;br /&gt;
&amp;quot;http://127.0.0.1:8081/#{$1}&amp;quot;&lt;br /&gt;
else&lt;br /&gt;
r.url&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
end&lt;br /&gt;
main&lt;br /&gt;
&lt;br /&gt;
Save &amp;amp; Exit.&lt;br /&gt;
5) Install RUBY&lt;br /&gt;
&lt;br /&gt;
What is RUBY?&lt;br /&gt;
Ruby is a dynamic, open source programming language with a focus on simplicity and productivity. It has an elegant syntax that is natural to read and easy to write.&lt;br /&gt;
&lt;br /&gt;
Now install RUBY by following command&lt;br /&gt;
&lt;br /&gt;
apt-get install ruby&lt;br /&gt;
6) Configure Squid Cache DIR and Permissions&lt;br /&gt;
&lt;br /&gt;
Now create cache dir and assign proper permission to proxy user&lt;br /&gt;
&lt;br /&gt;
mkdir /cache1&lt;br /&gt;
chown proxy:proxy /cache1&lt;br /&gt;
chmod -R  777 /cache1&lt;br /&gt;
&lt;br /&gt;
Now initialize squid cache directories by&lt;br /&gt;
&lt;br /&gt;
squid -z&lt;br /&gt;
&lt;br /&gt;
You should see Following message&lt;br /&gt;
&lt;br /&gt;
Creating Swap Directories&lt;br /&gt;
7) Finally Start/restart SQUID &amp;amp; Nginx&lt;br /&gt;
&lt;br /&gt;
service squid start&lt;br /&gt;
service nginx restart&lt;br /&gt;
&lt;br /&gt;
Now from test pc, open youtube and play any video, after it download completely, delete the browser cache, and play the same video again, This time it will be served from the cache. You can verify it by monitoring your WAN link utilization while playing the cached file.&lt;br /&gt;
&lt;br /&gt;
Look at the below WAN utilization graph, it was taken while watching the clip which is not in cache&lt;br /&gt;
&lt;br /&gt;
WAN utilization of Proxy, While watching New Clip (Not in cache)&lt;br /&gt;
&lt;br /&gt;
Now Look at the below WAN utilization graph, it was taken while watching the clip which is now in CACHE.&lt;br /&gt;
&lt;br /&gt;
WAN utilization of Proxy, While watching already cached Clip&lt;br /&gt;
&lt;br /&gt;
Playing Video, loaded from the cache chunk by chunk&lt;br /&gt;
&lt;br /&gt;
It will load first chunk from the cache, if the user keep watching the clip, it will load next chunk at the end of first chunk, and will continue to do so.&lt;br /&gt;
&lt;br /&gt;
Video cache files can be found in following locations.&lt;br /&gt;
/usr/local/www/nginx_cache/files&lt;br /&gt;
&lt;br /&gt;
e.g:&lt;br /&gt;
&lt;br /&gt;
ls -lh /usr/local/www/nginx_cache/files&lt;br /&gt;
&lt;br /&gt;
The above file shows the clip is in 360p quality, and the length of the clip is 5:54 Seconds.&lt;br /&gt;
itag=34 shows the video quality is 360p.&lt;br /&gt;
&lt;br /&gt;
Credits: Thanks to Mr. Eliezer Croitoru &amp;amp; Mr.Christian Loth &amp;amp; others for there kind guidance.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Referensi==&lt;br /&gt;
&lt;br /&gt;
* http://aacable.wordpress.com/2012/08/13/youtube-caching-with-squid-nginx/&lt;/div&gt;</summary>
		<author><name>Onnowpurbo</name></author>
	</entry>
</feed>