Skip to content

Instantly share code, notes, and snippets.

@Vijar
Created July 6, 2015 00:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vijar/33dd37cf97fc90b25479 to your computer and use it in GitHub Desktop.
Save Vijar/33dd37cf97fc90b25479 to your computer and use it in GitHub Desktop.
Try S3 for static content, then fallback to EC2 using Nginx as reverse proxy
location ~* ^/static/(.*) {
set $s3_bucket 'your_bucket.s3.amazonaws.com';
set $url_full '$1';
# Not sure if I need the following options or not, need to look into it.
proxy_http_version 1.1;
proxy_set_header Host $s3_bucket;
proxy_set_header Authorization '';
proxy_hide_header x-amz-id-2;
proxy_hide_header x-amz-request-id;
proxy_hide_header Set-Cookie;
proxy_ignore_headers "Set-Cookie";
proxy_buffering off;
proxy_pass http://$s3_bucket/$url_full;
# Proxy to ec2 as fallback
proxy_intercept_errors on;
recursive_error_pages on;
error_page 404 = @dynamic_ec2;
# use aws dns resolver for potential speed
resolver 172.16.0.23 valid=300s;
resolver_timeout 10s;
}
location @dynamic_ec2{
set $ec2_host 'your_bucket.s3.amazonaws.com';
proxy_pass http://$ec2_host$uri;
}
# source: http://linuxplayer.org/2013/06/nginx-try-files-on-multiple-named-location-or-server
# source: https://coderwall.com/p/rlguog
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment