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

2

15

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.

Lawsuit answered 6/10, 2011 at 21:55 Comment(3)
Maybe it doesn't feel right but surely they are telling you that. On python.org you have the documentation regarding gzip usage: docs.python.org/library/gzip.htmlGregoriagregorian
just playing devils advocate here - why do you need to use gzip? You're not paying for bandwidth in/out of Heroku so why the need to compress?Boroughenglish
I need to compress because my users will prefer to load up a page that weighs 300KB instead of 1MB!Lawsuit
W
9

You just need a wsgi middleware that gzips the response. See Gzipping all HTTP traffic with Pyramid or http://librelist.com/browser//flask/2010/6/14/gzip-compression/

Western answered 21/10, 2011 at 16:35 Comment(4)
I'm not using Pyramid, as stated above. And that Flask link isn't a complete implementation.Lawsuit
Both Pyramid and Flask are wsgi based. You need wsgi middleware, not flask specific middleware.Western
Here's another code.google.com/p/ibkon-wsgi-gzip-middleware/source/browse/… and another evanfosmark.com/2008/12/… Again, this is wsgi middleware not flask, gunicorn or Pyramid.Western
Great, your answer pointed me in the right direction! I also found this: pylonsbook.com/en/1.1/…. Works now, thanks!Lawsuit
N
2

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.

Neisa answered 28/7, 2013 at 18:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.