How to enable gzip compression on Heroku Cedar (Python/Flask/Gunicorn)

9.5k Views Asked by At

How do I enable GZIP compression on the new Heroku Cedar stack? This is straight from their site:

Since requests to Cedar apps are made directly to the application server – not proxied through an HTTP server like nginx – any compression of responses must be done within your application. For Rack apps, this can be accomplished with the Rack::Deflater middleware. For gzipped static assets, make sure that Rack::Deflater is loaded before ActionDispatch::Static in your middleware stack.

If I'm reading this correctly, my Python application code is now responsible for gzipping the responses? How would I go about compressing my static assets (e.g. css/js)? I'm using Flask and GUnicorn.

2

There are 2 best solutions below

4
On BEST ANSWER
0
On

According to the WSGI spec, published 2003, apps shouldn't gzip responses but leave that to the server (presumed to be Apache, running the app CGI-stylee).

applications and middleware must not apply any kind of Transfer-Encoding to their output, such as chunking or gzipping; as "hop-by-hop" operations, these encodings are the province of the actual web server/gateway.

But today in 2013, often the app is the server. WSGI didn't anticipate this. That's a problem, according to http://www.b-list.org/weblog/2009/aug/10/wsgi/

WSGI’s curious insistence on compatibility with CGI also means that, here in 2009, the Python web-development world still hasn’t been able to significantly improve on 1997’s application programming model.