Fetching and caching photos on demand with nginx -
i developing application, use of photos provided third party service. while building first prototype, got images directly service, i.e.
<img src="http://thirdpartyservice.com/img/12345.jpg">
it production though, makes no sense grabbing images service every time requested. slow, because serves millions of users @ same time.
i'd build thin layer on top of nginx, fetches, , caches images 24 hours based on demand. instead of calling service every time, i'd rather call
<img src="http://myapp.com/img?url=thirdpartyservice.com/img/12345.jpg">
if first time image requested, fetched remote service , cached on servers 24 hours.
is possible nginx?
first suggest not change url format http://myapp.com/img/12345.jpg
in case proxy same thirdpartyservice.com. here config example based on nginx wiki.
http { proxy_cache_path /data/nginx/cache levels=1:2 keys_zone=static:10m inactive=24h max_size=1g; server { location /img/ { proxy_pass http://thirdpartyservice.com; proxy_set_header host thirdpartyservice.com; proxy_cache static; proxy_cache_valid 200 1d; proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504; } } }
there lot of directives cache tweaking in official documentation.
Comments
Post a Comment