Skip to content

Instantly share code, notes, and snippets.

@alekseyl
Last active March 28, 2018 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alekseyl/e6ddedc882fee4c9a499a99ffb792be4 to your computer and use it in GitHub Desktop.
Save alekseyl/e6ddedc882fee4c9a499a99ffb792be4 to your computer and use it in GitHub Desktop.
nginx/rails cache config example
proxy_cache_pass /path/to/cache levels= keys_zone=rails_cache:10m max_size=10g use_temp_path=off inactive=120m;
location / {
try_files $uri @rails;
}
location @rails {
# lets skip authorized user.
proxy_no_cache $cookie_remember_user_token;
proxy_cache_bypass $cookie_remember_user_token;
#MANIPULATING HEADER
# since we cannot manage Cache-Control without affecting Rack::Cache,
# it's best not to consider it at all
proxy_ignore_headers Cache-Control;
# Hiding rails Cache-Control header
# to be sure we remove max-age from Cache-control
proxy_hide_header Cache-Control;
# And replacing it with no-cache or max-age=0, must-revalidate
add_header Cache-Control no-cache;
# TUNING NGINX CACHE
# Since we ignoring Cache-Control we need to add proxy_cache_valid
proxy_cache_valid any 2s;
#return stale in special cases:
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504;
# preventing ping hard with same request
proxy_cache_lock on;
# this really make sense only if you are setting
# conditional headers in rails ( fresh_when e.t.c )
proxy_cache_revalidate on;
# ALTERNATIVE FOR UNCACHEABLE:
# to return cache only while application busy with last response:
# proxy_cache_valid any 0s;
# proxy_cache_use_stale updating;
# proxy_cache_lock on;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment