Created
April 9, 2014 19:20
-
-
Save ericksond/10305147 to your computer and use it in GitHub Desktop.
nginx dynamic caching using proxy_store with reverse-proxy fallback
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # servers to proxy to | |
| upstream servers { | |
| server 10.0.0.1; | |
| server 10.0.0.2; | |
| } | |
| server { | |
| listen 80; | |
| server_name www.domain.com; | |
| server_name www2.domain.com | |
| # this proxy_method flag for logging will be helpful | |
| # in determining if object is passed, cached or stored | |
| set $proxy_method STORE; | |
| set $store_extra ''; | |
| # store dynamic content as physical files | |
| # appended with a .json extension | |
| location ~ /GetJSON { | |
| root /var/tmp/nginx/json; | |
| expires max; | |
| try_files $request_uri.json @fetch_json ; | |
| } | |
| location @fetch_json { | |
| internal; | |
| proxy_pass http://servers$request_uri; | |
| proxy_store /var/tmp/nginx/json${request_uri}.json; | |
| proxy_store_access user:rw group:rw all:r; | |
| set $proxy_method PASS; | |
| } | |
| # any other static content will be handled by nginx | |
| # reverse proxy and caching | |
| location / { | |
| proxy_ignore_headers X-Accel-Expires Expires Cache-Control Set-Cookie; | |
| proxy_cache MYCACHE; | |
| expires max; | |
| proxy_cache_use_stale error; | |
| proxy_cache_valid any 30d; | |
| proxy_pass http://servers$request_uri; | |
| add_header X-NGINX-Cache $upstream_cache_status; | |
| set $proxy_method CACHE; | |
| } | |
| # this custom logging will show a 'HIT' or a 'MISS' tag | |
| # per content | |
| log_format up_head '[$time_local] "$request" ' | |
| '$status $body_bytes_sent "$http_referer"' | |
| '"$http_x_forwarded_for" "$upstream_cache_status"' | |
| '"$proxy_method"' | |
| access_log /var/log/nginx/cache.log up_head; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment