Monday, July 6, 2009

How To Serve Pre-Compressed Static Files in Apache

There are example snippets in various corners of the web on how to do this, but surprisingly it's hard to find real working examples.

Here's a snippet of Apache configuration that will serve out pre-compressed gzip files for javascript and css and set the proper Content-Type and Content-Encoding. For example, if the client requested myfile.js and accepts gzip encoding, Apache will look for a file named myfile.js.gz and send the contents of the compressed file instead. If the compressed file doesn't exist, it sends the uncompressed version.

Also note that this is proxy caching server friendly.


# Netscape 4.x has some problems... only compress html files
BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 has problems... don't compress anything
BrowserMatch ^Mozilla/4\.0[678] no-gzip

# MSIE masquerades as Netscape
BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

RewriteEngine on

# If the browser accepts gzip and the requested file exists with
# a .gz appended, then rewrite the request to the .gz file
RewriteCond %{HTTP:Accept-Encoding} gzip
RewriteCond %{REQUEST_FILENAME}.gz -f
RewriteRule (.*\.(css|js))$ $1\.gz [L]

#Set content type to JavaScript and the encoding to gzip
<FilesMatch ".*\.js\.gz$">
ForceType application/x-javascript
Header set Content-Encoding gzip
</FilesMatch>

#Set content type to CSS and the encoding to gzip
<FilesMatch ".*\.css\.gz$">
ForceType text/css
Header set Content-Encoding gzip
</FilesMatch>

# Tell caching proxy servers to cache the file based on both
# browser type and encoding
Header append Vary User-Agent
Header append Vary Accept-Encoding

# Do this to set proper ETags for server clusters
FileETag MTime Size

5 comments:

  1. One note about this. It does not work when the file system is different than the URL (aliases, war file, etc.). The only way to support this directly is to either use the RewriteCond with a -F flag or always guarantee that a .gz file exists with any files you're serving like this.

    However, beware of using the -F or -U flag because they cause a subrequest to itself. If you're doing this for performance reasons, it defeats the purpose of pre-compressing the file in the first place (i.e. you might as well use mod_deflate).

    ReplyDelete
  2. Casino & Slot Machine Locations - Mapyro
    Find Casino & Slot Machine Locations in Hinterland, 인천광역 출장안마 IL 전라남도 출장마사지 with Mapyro users' ratings, 경기도 출장마사지 see activity, directions, and 광양 출장샵 information. 양산 출장샵

    ReplyDelete
  3. In many U.S. casinos, players are limited to half in} one to three positions at a desk. Free blackjack apps can be interesting for novices who are simply studying means to|tips on how to} play blackjack or who are trying to develop a profitable technique. Gameplay is similar as for blackjack apps to win xn--2o2b21qv5bour7xc.com actual cash, except that you don’t actually place any bets firstly of every hand.

    ReplyDelete
  4. Best for components that need to be easy at a microscopic level. Suitable CNC machining virtually all} metals, but principally used for stainless steels. A small quantity of fabric is removed through the laser cutting process, which is called as} a cutting kerf. Compared to waterjet cutting or plasma cutting, laser cutting produces the bottom kerf at 0.three mm on average, but the precise quantity is dependent on the fabric and laser beam width. Sometimes the process is usually a|could be a} progressive stamping process where there are quantity of} stamping stations that steadily reduce or kind the half additional and additional until the ultimate piece is created. Not solely is forming steel convenient, it also adds strength and stiffness to an assembly.

    ReplyDelete