<p>I am serving all content through apache with <code>Content-Encoding: zip</code> but that compresses on the fly.  A good amount of my content is static files on the disk.  I want to gzip the files beforehand rather than compressing them every time they are requested.</p>

<p>This is something that, I believe, <code>mod_gzip</code> did in Apache 1.x automatically, but just having the file with .gz next to it.  That's no longer the case with <code>mod_deflate</code>.</p>

[{'answer_id': 75682, 'author': 'Aristotle Pagaltzis', 'author_id': 9410, 'author_profile': 'https://Stackoverflow.com/users/9410', 'pm_score': 5, 'selected': True, 'text': '<p>This functionality was misplaced in mod_gzip anyway. In Apache 2.x, <a href="http://httpd.apache.org/docs/2.2/content-negotiation.html" rel="noreferrer">you do that with content negotiation</a>. Specifically, you need to enable <code>MultiViews</code> with the <a href="http://httpd.apache.org/docs/2.2/mod/core.html#options" rel="noreferrer"><code>Options</code> directive</a> and you need to specify your encoding types with the <a href="http://httpd.apache.org/docs/2.2/mod/mod_mime.html#addencoding" rel="noreferrer"><code>AddEncoding</code> directive</a>.</p>\n'}, {'answer_id': 75697, 'author': 'Brian Matthews', 'author_id': 1969, 'author_profile': 'https://Stackoverflow.com/users/1969', 'pm_score': 0, 'selected': False, 'text': '<p>You can use <a href="http://httpd.apache.org/docs/2.2/mod/mod_cache.html" rel="nofollow noreferrer"><code>mod_cache</code></a> to proxy local content in memory or on disk. I don\'t know if this will work as expected with <code>mod_deflate</code>.</p>\n'}, {'answer_id': 75727, 'author': 'Aeon', 'author_id': 13289, 'author_profile': 'https://Stackoverflow.com/users/13289', 'pm_score': 1, 'selected': False, 'text': '<p>mod_gzip compressed content on the fly as well. You can pre-compress the files by actually logging into your server, and doing it from shell.</p>\n\n<pre><code>cd /var/www/.../data/\nfor file in *; do\n    gzip -c $file &gt; $file.gz;\ndone;\n</code></pre>\n'}, {'answer_id': 97155, 'author': 'Otto', 'author_id': 9594, 'author_profile': 'https://Stackoverflow.com/users/9594', 'pm_score': 3, 'selected': False, 'text': '<p>To answer my own question with the really simple line I was missing in my confiuration:</p>\n\n<pre><code>Options FollowSymLinks MultiViews\n</code></pre>\n\n<p>I was missing the MultiViews option.  It\'s there in the Ubuntu default web server configuration, so don\'t be like me and drop it off.</p>\n\n<p>Also I wrote a quick Rake task to compress all the files.</p>\n\n<pre><code>namespace :static do\n    desc "Gzip compress the static content so Apache doesn\'t need to do it on-the-fly."\n    task :compress do\n        puts "Gzipping js, html and css files."\n        Dir.glob("#{RAILS_ROOT}/public/**/*.{js,html,css}") do |file|\n            system "gzip -c -9 #{file} &gt; #{file}.gz"\n        end\n    end\nend\n</code></pre>\n'}, {'answer_id': 609051, 'author': 'brianegge', 'author_id': 14139, 'author_profile': 'https://Stackoverflow.com/users/14139', 'pm_score': 2, 'selected': False, 'text': '<p>I have an Apache 2 built from source, and I found I had to modify the following in my <strong>httpd.conf</strong> file:</p>\n\n<p>Add MultiViews to Options:</p>\n\n<blockquote>\n<pre><code>Options Indexes FollowSymLinks MultiViews\n</code></pre>\n</blockquote>\n\n<p>Uncomment AddEncoding:</p>\n\n<blockquote>\n<pre><code>AddEncoding x-compress .Z\nAddEncoding x-gzip .gz .tgz\n</code></pre>\n</blockquote>\n\n<p>Comment AddType:</p>\n\n<blockquote>\n<pre><code>#AddType application/x-compress .Z\n#AddType application/x-gzip .gz .tgz\n</code></pre>\n</blockquote>\n'}, {'answer_id': 9169300, 'author': 'Arno Schäfer', 'author_id': 1193560, 'author_profile': 'https://Stackoverflow.com/users/1193560', 'pm_score': 3, 'selected': False, 'text': '<p>I am afraid MultiViews will not work as expected: the doc says Multiviews works "if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist...", in other words: if you have a file foo.js and foo.js.gz in the same directory, just activating MultiViews will not cause the .gz file to be sent even if the AcceptEncoding gzip header is transmitted by the browser (you can verify this behavior by temporarily disabling mod_deflate and monitoring the response with e.g. HTTPFox).</p>\n\n<p>I am not sure if there is a way around this with MultiViews (maybe you can rename the original file and then add a special AddEncoding directive), but I believe you can construct a mod_rewrite rule to handle this.</p>\n'}, {'answer_id': 25556606, 'author': 'Vivien', 'author_id': 2191299, 'author_profile': 'https://Stackoverflow.com/users/2191299', 'pm_score': 0, 'selected': False, 'text': '<p>I have a lot of big .json files. Most readers are in this situation. The preview answers didn\'t talk about the returned "Content-type".</p>\n\n<p>I you want the following request return a pre-compressed file with "Content-Type: application/json" transparently, use Multiview with ForceType</p>\n\n<pre><code>http://www.domain.com/(...)/bigfile.json\n-&gt; Content-Encoding:gzip, Content-Type: Content-Encoding:gzip\n</code></pre>\n\n<p>1) files must be rename: "file.ext.ext"</p>\n\n<p>2) Multiview works great with ForceType</p>\n\n<p>In the file system:</p>\n\n<pre><code>// Note there is no bigfile.json\n(...)/bigfile.json.gz\n(...)/bigfile.json.json\n</code></pre>\n\n<p>In your apache config:</p>\n\n<pre><code>&lt;Directory (...)&gt;\n    AddEncoding gzip .gz\n    Options +Multiviews\n    &lt;Files *.json.gz&gt;\n        ForceType application/json\n    &lt;/Files&gt;\n&lt;/Directory&gt;\n</code></pre>\n\n<p>Short and simple :)</p>\n'}, {'answer_id': 34932031, 'author': 'Kevinoid', 'author_id': 503410, 'author_profile': 'https://Stackoverflow.com/users/503410', 'pm_score': 3, 'selected': False, 'text': '<p>It is possible to serve pre-compressed files using <code>mod_negotiation</code> although it is a bit finicky.  The primary difficulty is that <strong><a href="https://httpd.apache.org/docs/2.4/mod/mod_negotiation.html#multiviews" rel="nofollow noreferrer">only requests for files which do not exist are negotiated</a></strong>.  So if <code>foo.js</code> and <code>foo.js.gz</code> both exist, responses for <code>/foo.js</code> will always be uncompressed (although responses for <code>/foo</code> would work correctly).</p>\n\n<p>The easiest solution I\'ve found (<a href="https://feeding.cloud.geek.nz/posts/serving-pre-compressed-files-using/" rel="nofollow noreferrer">from François Marier</a>) is to rename uncompressed files with a double file extension, so <code>foo.js</code> is deployed as <code>foo.js.js</code> so requests for <code>/foo.js</code> negotiate between <code>foo.js.js</code> (no encoding) and <code>foo.js.gz</code> (gzip encoding).</p>\n\n<p>I combine that trick with the following configuration:</p>\n\n<pre><code>Options +MultiViews\nRemoveType .gz\nAddEncoding gzip .gz\n\n# Send .tar.gz without Content-Encoding: gzip\n&lt;FilesMatch ".+\\.tar\\.gz$"&gt;\n    RemoveEncoding .gz\n    # Note:  Can use application/x-gzip for backwards-compatibility\n    AddType application/gzip .gz\n&lt;/FilesMatch&gt;\n</code></pre>\n\n<p>I <a href="https://kevinlocke.name/bits/2016/01/20/serving-pre-compressed-files-with-apache-multiviews/" rel="nofollow noreferrer">wrote a post</a> which discusses the reasoning for this configuration and some alternatives in detail.</p>\n'}]
['https://Stackoverflow.com/questions/75482', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/9594/']