Skip to content

Instantly share code, notes, and snippets.

@benbonnet
Last active July 15, 2016 08:30
Show Gist options
  • Save benbonnet/a4db14931b058ac201c9bd90321d0472 to your computer and use it in GitHub Desktop.
Save benbonnet/a4db14931b058ac201c9bd90321d0472 to your computer and use it in GitHub Desktop.
nginx cache + google cloud storage
# The following url will be available :
# getting the original image : http://your-domain.tld/og/{bucketname}/{imagepath}
# sizing an image (here, will fit in a 200x200px area) : http://your-domain.tld/t/200x200/{bucketname}/{imagepath}
# important part : set the cache folder (here /tmp/nginx-images) and the cache name (here thumbnail_cache)
proxy_cache_path /tmp/nginx-images levels=1:2 keys_zone=thumbnail_cache:16M inactive=60d max_size=200M;
server {
listen 80;
server_name your-domain.tld;
location / {
proxy_pass http://localhost:10101;
proxy_cache thumbnail_cache;
proxy_cache_key "$host$document_uri$is_args$arg_key";
proxy_cache_lock on;
proxy_cache_valid 30d; # Cache valid thumbnails for 30 days.
proxy_cache_valid any 15s;
proxy_cache_use_stale error timeout invalid_header updating;
proxy_http_version 1.1;
expires 30d;
}
}
server {
listen 10101;
server_name localhost;
set $backend 'storage.googleapis.com';
resolver 8.8.8.8;
resolver_timeout 5s;
proxy_buffering off;
proxy_http_version 1.1;
proxy_pass_request_body off;
proxy_pass_request_headers off;
proxy_set_header Host $backend;
proxy_method GET;
image_filter_jpeg_quality 85;
image_filter_buffer 12M;
image_filter_interlace on;
error_page 404 =404 /empty.gif;
location ~ ^/og/(.*) {
set $image_path '$1';
image_filter_jpeg_quality 75;
proxy_pass http://$backend/$1;
}
location ~ ^/t/([\d-]+)x([\d-]+)/(.*) {
#http://stackoverflow.com/a/4243129
#secure_link $arg_key; # The hash is stored in the `key` querystring arg.
#secure_link_md5 "$uri my-secret-key";
#if ($secure_link = "") {
# return 404;
#}
set $image_path '$3';
image_filter resize $1 $2;
proxy_pass http://$backend/$3;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment