balanced / www.balancedpayments.com

The website for Balanced.
https://www.balancedpayments.com
Other
29 stars 51 forks source link

Cache requests to GitHub in nginx natively #80

Closed nodesocket closed 10 years ago

nodesocket commented 10 years ago

Here is some gangsta stuff, to cache GitHub API requests in nginx.

Outside of the server block (http):

proxy_cache_path /tmp/cache levels=1:2 keys_zone=GITHUB:5m inactive=2h max_size=10m;

In the server block:

location ~^/github-proxy/(?<p>.*) {
    resolver 8.8.8.8;
    proxy_pass https://api.github.com/$p;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_cache_methods GET;
    proxy_cache GITHUB;
    proxy_cache_valid 200 2h;
    proxy_connect_timeout 5s;
    proxy_read_timeout 15s;
}

Use like:

http://localhost:9090/github-proxy/orgs/balanced/repos
nodesocket commented 10 years ago

@mjallday and @bninja can you guys take a look. Any further optimizations or problems? What should we use as the DNS resolv since 8.8.8.8 is blocked by AWS?

bninja commented 10 years ago

@nodesocket nice work. any need to limit what api calls we'll proxy (e.g.just info for balanced repos)?

bninja commented 10 years ago

@nodesocket not sure what resolver we should use nowaways but e.g js. uses an internal one (https://github.com/balanced/confidence/blob/master/cookbooks/nginx/templates/default/etc/nginx/conf.d/liberty.conf.erb#L34) so maybe use that too.

mahmoudimus commented 10 years ago

@bninja you're right - resolver 8.8.8.8; will probably result in nginx timeouts.

nodesocket commented 10 years ago

Take a peek at my pr:

https://github.com/nodesocket/confidence/commit/354b87b9c52e96d8a96246db166ba24fc3023fdb