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.

Gabriel Florit
  • 2,886
  • 1
  • 22
  • 36
  • 1
    Maybe it doesn't feel right but surely they are telling you that. On python.org you have the documentation regarding gzip usage: http://docs.python.org/library/gzip.html – gforcada Oct 06 '11 at 22:01
  • 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? – John Beynon Oct 07 '11 at 07:48
  • 8
    I need to compress because my users will prefer to load up a page that weighs 300KB instead of 1MB! – Gabriel Florit Oct 07 '11 at 14:58

2 Answers2

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/

Community
  • 1
  • 1
Tavis Rudd
  • 1,156
  • 6
  • 12
  • I'm not using Pyramid, as stated above. And that Flask link isn't a complete implementation. – Gabriel Florit Oct 24 '11 at 17:23
  • 1
    Both Pyramid and Flask are wsgi based. You need wsgi middleware, not flask specific middleware. – Tavis Rudd Oct 24 '11 at 17:26
  • 1
    Here's another http://code.google.com/p/ibkon-wsgi-gzip-middleware/source/browse/trunk/gzip_middleware.py and another http://www.evanfosmark.com/2008/12/python-wsgi-middleware-for-automatic-gzipping/ Again, this is wsgi middleware not flask, gunicorn or Pyramid. – Tavis Rudd Oct 24 '11 at 17:36
  • Great, your answer pointed me in the right direction! I also found this: http://pylonsbook.com/en/1.1/the-web-server-gateway-interface-wsgi.html#altering-the-response. Works now, thanks! – Gabriel Florit Oct 24 '11 at 19:46
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.

Colonel Panic
  • 132,665
  • 89
  • 401
  • 465