Skip to content

Instantly share code, notes, and snippets.

@etianen
Created April 11, 2013 12:55
Show Gist options
  • Save etianen/5363153 to your computer and use it in GitHub Desktop.
Save etianen/5363153 to your computer and use it in GitHub Desktop.
An nginx configuration to creating an authenticated, caching, Twitter API proxy.
# This defines a 10 megabyte cache for the proxy service, and needs to live
# outside of the virtual host configuration. Adjust the path according to
# your environment.
proxy_cache_path /var/cache/nginx/twitter_api_proxy levels=1:2 keys_zone=twitter_api_proxy:10m;
# The virtual host configuration.
server {
# If your want to secure your proxy with SSL, replace with the appropriate SSL configuration.
listen 80;
# Replace this with the name of the domain you wish to run your proxy on.
server_name api.twitter.yourdomain.com;
# Replace this with your own document root.
root /var/www;
# This setting attempts to use files in the document root before
# hitting the Twitter proxy. This allows you to put a permissive
# crossdomain.xml file in your document root, and have it show up
# in the browser.
location / {
try_files $uri $uri/index.html @twitter;
}
# The Twitter proxy code!
location @twitter {
# Caching settings, to avoid rate limits on the API service.
proxy_cache twitter_api_proxy;
proxy_cache_use_stale error updating timeout;
proxy_cache_valid 200 302 404 5m; # The server cache expires after 5 minutes - adjust as required.
proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie;
# Hide Twitter's own caching headers - we're applying our own.
proxy_hide_header X-Accel-Expires;
proxy_hide_header Expires;
proxy_hide_header Cache-Control;
proxy_hide_header pragma;
proxy_hide_header set-cookie;
expires 5m; # The browser cache expires after 5 minutes - adjust as required.
# Set the correct host name to connect to the Twitter API.
proxy_set_header Host api.twitter.com;
# Add authentication headers - edit and add in your own bearer token.
proxy_set_header Authorization "Bearer INSERT_YOUR_BEARER_TOKEN"
# Actually proxy the request to Twitter API!
proxy_pass https://api.twitter.com;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment