Add cache buster feature for media files #15155
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Nginx can be configured to bypass proxy cache when a special header is in the request. If the response is cacheable, it will replace
the cache for that request. Proxy caching of media files is desirable when using object storage as a way of minimizing bandwidth
costs, but has the drawback of leaving deleted media files for a configured amount of cache time. A cache buster can make those
media files immediately unavailable. This especially makes sense when suspending and unsuspending an account.
Configuration (trivial):
In nginx (trivial):
Of course, letting anyone bypass your cache is an abuse vector for increasing your object storage bandwidth bill, so in reality you would want to actually secure it. You see,
$http_secret_header
is not a special variable -- all HTTP headers can be accessed using this variable structure. So it's just accessing the raw value of a HTTP header "secret-header". Furthermore, theproxy_cache_bypass
directive works by treating an empty string or0
value asfalse
and everything else astrue
. So what we can do is define amap
from a better named header (likeCache-Bypass
, though it can be anything you want) to our own variable, and make it return 0 for every value except a generated secret value.Then in Mastodon's configuration, it would simply be: