http {
upstream storage_backend {
http://storage.example.com:80;
keepalive 256 single;
}
proxy_cache_path /opt/cache levels=1:2 keys_zone=images:256m inactive=4h max_size=20G;
server {
listen 80;
server_name example.com;
# image serving location
location /images/ {
# images path
root /data/www;
# if there is no image - let's fetch it
error_page 404 = @fetch;
}
# image fetching location
location @fetch {
internal;
# passing request to storage
proxy_pass http://storage_backend;
# setting up caching:
# - key for caching
proxy_cache_key $request_uri;
# - minimal request to store image in cache
proxy_cache_min_uses 7;
# - How long will valid response from storage will be valid, 4 days for example
proxy_cache_valid 200 4d;
}
}
}
http {
upstream storage_backend {
http://storage.example.com:80;
keepalive 256 single;
}
server {
listen 80;
server_name example.com;
# image serving location
location /images/ {
# images path
root /data/www;
# if there is no image - let's fetch it
error_page 404 = @fetch;
}
# image fetching location
location @fetch {
internal;
# passing request to storage
proxy_pass http://storage_backend;
# Saving image from storage
proxy_store on;
# Setting permissions
proxy_store_access user:rw group:rw all:r;
# Temporary directory for unfinished fetches
proxy_temp_path /data/temp;
# Image will be saving to this folder with "proxy_store -> on",
# so we just need to serve it as usual
root /data/www;
}
}
}</sorce>