Skip to content

Instantly share code, notes, and snippets.

@vmoravec
Forked from josue/nginx_s3_proxy.conf
Created July 29, 2019 15:19
Show Gist options
  • Save vmoravec/6de664fc892e221cfd3638b02b2b2c65 to your computer and use it in GitHub Desktop.
Save vmoravec/6de664fc892e221cfd3638b02b2b2c65 to your computer and use it in GitHub Desktop.
Simple Nginx Proxy to S3 Bucket Asset
server {
listen 80;
listen 443 default_server ssl;
ssl on;
ssl_certificate /etc/ssl/certs/myssl.crt;
ssl_certificate_key /etc/ssl/private/myssl.key;
server_name *.example.com;
root /var/www/vhosts/website;
location / {
index index.php index.html index.htm;
if (-f $request_filename) {
break;
}
if (-d $request_filename) {
break;
}
# using PHP app routing
rewrite ^(.+)$ /index.php?url=$1 last;
}
# Proxy any URL request to S3 bucket and remove any Amazon headers
location ~ "^/asset/(.*)$" {
add_header X-Asset-Location $hostname;
set $bucket "<BUCKET-NAME>";
set $key $1;
rewrite .* /$key break;
# no client headers
proxy_pass_request_headers off;
# let amazon take the buffering load
proxy_buffering off;
# let amazon retry a few times if first timeouts are 5xx response
proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
proxy_set_header Host $bucket.s3.amazonaws.com;
proxy_pass http://s3.amazonaws.com;
proxy_hide_header "x-amz-id-2";
proxy_hide_header "x-amz-request-id";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment