What Happened

Recently I needed to serve a 10MB+ JSON file as a static API endpoint.
(Don’t ask me why the API content-length is that high)
Turns out Garage and Ferron do not compress it for me, so I had to do that manually.

garage-uncompressed

The trick

Compress it locally and instruct the browser to treat that file as gzip by passing the right header.
Content-Encoding: gzip

  1. Compress the file locally

$ gzip -c example.json > example.json.gz

-rw-r--r-- 1 staff staff 11352295 12 11 16:39 example.json
-rw-r--r-- 1 staff staff 2451680 14 11 01:59 example.json.gz
And put the file to Garage as usual

  1. Update the reverse proxy (I use Ferron as the example, ferron.kdl)
    In this case, the *.gz suffix in the URL will be treated as gzip-compressed content and tell the browser to decompress it.

snippet "gz_content" {
condition "is_gz_request" {
is_regex "{path}" "\\.(gz)(?:$|[?#])" case_insensitive=#true
}
}
bucket.example.com {
proxy "http://garage:3902"
proxy_request_header_replace "Host" "bucket.example.com"

use "gz_content"
if "is_gz_request" {
header "content-encoding" "gzip"
}
}

Restart Ferron, and this is the final result:
garage-compressed

The size is reduced by almost 80%, which is about 8MB saved per request.