Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Nginx as Google Cloud Storage Cache
# Cache 10GB for 1 Month
proxy_cache_path /var/cache/nginx keys_zone=GS:10m inactive=720h max_size=10240m;
upstream gs {
server '';
keepalive 100;
server {
set $my_domain "";
listen 80;
server_name $my_domain;
# Logs
access_log off;
error_log /var/log/nginx/$my_domain.error.log debug;
# Cache Control
expires max;
add_header Cache-Control "public, max-age=31536000";
# Nameserver
resolver valid=300s;
resolver_timeout 10s;
# Proxy Cache
proxy_temp_path /tmp/nginx;
proxy_cache_lock on;
proxy_cache_key "$uri"; # Ignore Parameters
proxy_cache_purge on from;
# Limit Request Methods to GET|HEAD|PURGE
if ( $request_method !~ "GET|HEAD|PURGE" ) {
return 405;
location / {
proxy_set_header Host;
proxy_hide_header x-goog-hash;
proxy_hide_header x-goog-generation;
proxy_hide_header x-goog-metageneration;
proxy_hide_header x-goog-stored-content-encoding;
proxy_hide_header x-goog-stored-content-length;
proxy_hide_header x-goog-storage-class;
proxy_hide_header x-xss-protection;
proxy_hide_header accept-ranges;
proxy_hide_header alternate-protocol;
proxy_hide_header Set-Cookie;
proxy_ignore_headers "Set-Cookie";
proxy_intercept_errors on;
proxy_cache GS;
proxy_cache_valid 200 720h; # Cache For 1 Month
proxy_cache_bypass $http_cache_purge;
add_header X-Cache $upstream_cache_status;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_pass http://gs/$my_domain$uri;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment