What is the best way to tell rails to use weak instead of strong ETAGs when using methods fresh_when
and stale?
?
The reason I ask is that nginx (correctly) removes strong ETAG headers from responses when on-the-fly gzipping is enabled.
What is the best way to tell rails to use weak instead of strong ETAGs when using methods fresh_when
and stale?
?
The reason I ask is that nginx (correctly) removes strong ETAG headers from responses when on-the-fly gzipping is enabled.
It looks like Rack::ETag will use weak-etags in the future:
I took the code from @grosser's answer and turned it into a Gem:
You can just add this to your gemfile:
And it will be installed into your middleware before
Rack::ConditionalGet
:Then all the e-tags generated by rails, either with Rack::ETag or with explicit e-tags will be converted to weak. Using a patched, or version > 1.7.3 of nginx, will then let you use e-tags and gzip compression.
RACK 1.6 defaults etags to weak - this gem is no longer helpful if you upgrade.
Here's an alternative that avoids making any changes in your application server. This directive converts all etags returned by your application to weak etags before they get stripped from the response. Put it inside your inside your nginx config:
I've checked that this works with nginx 1.7.6.
middleware:
plus add middleware
plus unit tests
plus integration tests