First I would suggest not to change url format to http://myapp.com/img/12345.jpg
in case we always proxy to the same thirdpartyservice.com. Here is 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 are lot of directives about cache tweaking in official documentation.