Nginx: Leverage browser caching for remote resources (images)

cachecache-controlimagenginx

There are some images in one site of mine which are hosted to a remote server.
I haven't figured out yet how to set Cache-Control to remote sources.
For local files I'm using:

location ~* \.(jpg|jpe?g|gif|png|ico|cur|gz|svgz|mp4|ogg|ogv|webm|htc|css|js|otf|eot|svg|ttf|woff|woff2)(\?ver=[0-9.]+)?$ {
    expires 1M;
    add_header Access-Control-Allow-Origin '*';
    add_header Pragma public;
    add_header Cache-Control "public, must-revalidate, proxy-revalidate";
    access_log off;
    }

Which is working great.

I tried to add a location block containing the remote location of the files following the same principle with the local file, say for example https://example.com/img/ but nginx didnt do anything.
The purpose of all this is to increase site speed.

Any suggestions will be appreciated.

Best Answer

You can't change the headers for remote resources by changing anything on your server. You need to change them on the server that's serving the files.

The only way I can think to achieve this would be to proxy the images through a dedicated path on your server, but that would require changing the HTML to point to that path.