Skip to content

Instantly share code, notes, and snippets.

@akagr
Last active February 17, 2017 07:08
Show Gist options
  • Save akagr/6989327 to your computer and use it in GitHub Desktop.
Save akagr/6989327 to your computer and use it in GitHub Desktop.
Serve gzipped version of rails assets from apache server and set their expiry headers to 1 year
# The Expires* directives requires the Apache module `mod_expires` to be enabled.
<Location /assets/>
# Use of ETag is discouraged when Last-Modified is present
Header unset ETag
FileETag None
# RFC says only cache for 1 year
ExpiresActive On
ExpiresDefault "access plus 1 year"
</Location>
# Let apache serve the pre-compiled .gz version of static assets,
# if available, and the user-agent can handle it. Set all headers
# correctly when doing so.
#
# SOMEWHAT EXPERIMENTAL. If you think it's causing problems,
# just remove the following three LocationMatch.
<LocationMatch "^/assets/.*\.(css|js)$">
RewriteEngine on
# Make sure the browser supports gzip encoding before we send it,
# and that we have a precompiled .gz version.
RewriteCond %{HTTP:Accept-Encoding} \b(x-)?gzip\b
RewriteCond %{REQUEST_FILENAME}.gz -s
RewriteRule ^(.+)$ $1.gz
# Make sure Content-Type is set for 'real' type, not gzip,
# and Content-Encoding is there to tell browser it needs to
# unzip to get real type.
#
# Make sure Vary header is set; while apache docs suggest it
# ought to be set automatically by our RewriteCond that uses an HTTP
# header, does not seem to be reliably working.
<LocationMatch "^/assets/.*\.css\.gz$">
ForceType text/css
Header set Content-Encoding gzip
Header add Vary Accept-Encoding
<LocationMatch "^/assets/.*\.js\.gz$">
ForceType application/javascript
Header set Content-Encoding gzip
Header add Vary Accept-Encoding
</LocationMatch>
@anushamummina
Copy link

Hi Akagr,

Thank you for the gist. Could you please tell me in which file we need to add it so that it will apply to rails application.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment