Nginx – Setting Expires Epoch Header Selectively

cachehttp-headersnginx

I have a folder containing imagery on my server that is used to service both a mobile app, via a CDN, and to work with a backend webapp used to manage the content delivered to the app. For the latter – the backend webapp – I want to ensure that images delivered from the folder in question are not cached by the browser so I can show updated versions in the backend app as soon as they have been updated by their author. The CDN pulls the same images for delivery to the mobile app and has its refresh settings at 30 minutes so it does not deliver badly dated content imagery.

My question – how can I configure Nginx so it injects the expires epoch header only for requests originating from the webapp. Part of the answer to this question is in this SO thread. Is there a way to modify the solution proposed there so it examines the referrer prior to injecting the header? So, for instance all requests originating from https://example.com/backoffice are responded to with the expires epoch header while others – e.g. those coming from the CDN pull requests – are not.

Best Answer

A simple if ought to be sufficient here:

server {

    location ... {

        if ($http_referer ~* ^https://example.com/backoffice) {
            expires epoch;
        }

    }

}

Keep in mind that in general it is vastly preferable for your web application to make caching decisions and respond with the appropriate directives.