How to mirror and serve ajax.googleapis.com from your own box with nginx

This is how I set up a fairly simple local googleapis mirror using nginx and /etc/hosts. It works for most types of requests. I snagged the full list of files and their directories from Ryan McGeary's awesome ruby based googleapis-mirror which is at, https://github.com/rmm5t/googleapis-mirror .

I didn't want to run an extra webserver since I already had nginx running or I would've just used it. Grab your libraries.txt updates from there. You can see my dir structure too if you want. I use the file libraries.txt with wget.

In addition to McGeary's list I've found you also need to mirror these below too. Add them to your libraries.txt

http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
http://ajax.googleapis.com/ajax/libs/webfont/1/webfont.js
$ wget -x -i libraries.txt

The -x will keep the full paths, -i is for loading the list. After that move the ajax subdirectory to someplace. Now go into ./ajax/libs/. There's a list of softlinks you need to make in order to fullfill stupid GET requests like,

GET /ajax/libs/jquery/1.4/jquery.min.js?ver=3.4.2

See how it calls for 1.4, but in the dir stucture there is only,

ajax/libs/jquery/1.4.0/

So correct it by hand using the list below. You can throw it into a bash script or whatever if you trust me.

cd ajax/
cd libs/
cd jquery
ln -s 1.4.0/ 1.4
ln -s 1.3.0/ 1.3
ln -s 1.8.0/ 1.8
ln -s 1.7.0/ 1.7
ln -s 1.6.0/ 1.6
ln -s 1.5.0/ 1.5
cd ..
cd webfont/
ln -s 1.0.0/ 1.0
cd ..
cd mootools/
ln -s 1.3.0/ 1.3
ln -s 1.4.0/ 1.4
cd ..
cd jqueryui/
ln -s 1.6.0/ 1.6
ln -s 1.7.0/ 1.7
ln -s 1.8.0/ 1.8
ln -s 1.9.0/ 1.9
cd ..
cd ext-core/
ln -s 3.0.0/ 3.0
ln -s 3.0.0/ 3
cd ..
cd dojo/
ln -s 1.2.0/ 1.2
ln -s 1.3.0/ 1.3
ln -s 1.4.0/ 1.4
ln -s 1.5.0/ 1.5
ln -s 1.6.0/ 1.6
ln -s 1.7.0/ 1.7
ln -s 1.8.0/ 1.8
ln -s 1.5.0/ 1.5
cd ..
cd chrome-frame/
ln -s 1.0.0/ 1.0
ln -s 1.0.0/ 1

If that's all done now's the time to setup the nginx location. I did it like,

location /ajax/ {
	# make sure to put the trailing "/" on the alias path.
	alias /home/superkuh/app_installs/googleapis-mirror/ajax/;
}

Now all that's left is to set up the /etc/hosts file so that ajax.googleapis.com resolves to 127.0.0.1

# Google
127.0.0.1	ajax.googleapis.com

Reload your nginx config if you haven't (sudo /etc/init.d/nginx reload for me).

If all went well you have regular pages making requests to localhost for the javascript libraries hosted on ajax.googleapis.com To see if it's working and diagnose potential problems I watch the tail of the nginx logs.

$ watch -n 2 'tail -n 12 /var/log/nginx/access.log'
$ watch -n 5 'grep "/ajax/" < /var/log/nginx/access.log | tail -n 8'

To be honest that section of my hosts file actually looks like the below. Most of those are just opt-ed out with no replacement. It'll be nice to slowly replace them with safe, non-tracking versions.

# Google
127.0.0.1	ajax.googleapis.com
127.0.0.1       www.google-analytics.com 
127.0.0.1	plusone.google.com
127.0.0.1 	ssl.google-analytics.com
127.0.0.1 	pagead.googlesyndication.com
127.0.0.1 	pagead2.googlesyndication.com

Interests

Other

Photos

Good Books

Member of The Internet Defense League

Legal Bullshit

DMCA Requests

Terms of Use:

You may not access or use the site superkuh.com unless you are under 7 years of age. If you do not agree then you must leave now.

The US Dept. of Justice has determined that violating a website's terms of service is a felony under CFAA 1030(a)2(c). Under this same law I can declare that you may only use one IP address to access this site; circumvention is a felony. Absurd, isn't it?

It is my policy to regularly delete server logs.