10

I have a Django website deployed on Heroku, using Whitenoise for serving static files.

The static files work fine but Gzip is not working according to various websites that I used to test it (including google tools).

this is the code in my production settings files:

DATABASES['default'] = dj_database_url.config()


SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

ALLOWED_HOSTS = ['*']

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

My static files configuration:

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

STATIC_URL = '/static/'
STATIC_ROOT = 'staticfiles'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

my wsgi.py file

import os

from django.core.wsgi import get_wsgi_application

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sikumim.settings")

application = get_wsgi_application()

#HEROKU DEPLOYMENT

from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)

what could be the cause?

I ran a few commands suggested in the comments, looks like gzip isn't working:

➜ ~ curl -I -H "Accept-Encoding: gzip" http://127.0.0.1:8000/

HTTP/1.0 200 OK
Date: Mon, 17 Aug 2015 13:56:02 GMT
Server: WSGIServer/0.2 CPython/3.4.0
X-Frame-Options: SAMEORIGIN
Vary: Cookie
Content-Type: text/html; charset=utf-8
Set-Cookie:  csrftoken=SsgKEp76HDhG5L7otWxqBJeMyb00Vp03; expires=Mon,      15-Aug-2016 13:56:02 GMT; Max-Age=31449600; Path=/

➜ ~ curl -I -H "Accept-Encoding: gzip" http://www.sikumia.co.il

HTTP/1.1 200 OK
Connection: keep-alive
Server: gunicorn/19.3.0
Date: Mon, 17 Aug 2015 13:57:37 GMT
Transfer-Encoding: chunked
X-Frame-Options: SAMEORIGIN
Vary: Cookie
Content-Type: text/html; charset=utf-8
Set-Cookie: csrftoken=23M5ODiFKRnU3fDYMe3j2Rn3dwtCsNMX; expires=Mon, 15-Aug-2016 13:57:37 GMT; Max-Age=31449600; Path=/
Via: 1.1 vegur
davegri
  • 2,206
  • 2
  • 26
  • 45
  • Google page insights tool says that gzip isn't active, also all the other tools are saying the same thing – davegri Aug 15 '15 at 18:50
  • Two questions - 1) are you checking locally with curl (e.g., `curl -i -H "Accept-Encoding: gzip" http://localhost:8000/path/to/static` and 2) how are you running the app in your Procfile? I got gzip working with `gunicorn`, but not with `runserver` (which shouldn't be your production configuration anyway). – bimsapi Aug 17 '15 at 13:25
  • I'm sorry I'm a bit of a newbie, i'm running the app on heroku. there is only one line in my procfile: web: gunicorn sikumim.wsgi I'll try out that curl command – davegri Aug 17 '15 at 13:49
  • 2
    @davegri - try one of your actual static files, e.g., http://www.sikumia.co.il/static/css/base.92c9631393b3.css. It is getting compressed. The default page on '/' isn't, presumably because it's a template getting generated by Django. Note that Whitenoise will gzip the static content, but not the dynamic content (a little confusing, I know). To compress the dynamic content, you need to add the Django middleware (https://docs.djangoproject.com/en/1.8/ref/middleware/#module-django.middleware.gzip). Hope this helps! – bimsapi Aug 19 '15 at 03:56
  • Make sure you read the comment in the docs and linked PDF on the BREACH compression side channel attack as well (http://breachattack.com/resources/BREACH%20-%20SSL,%20gone%20in%2030%20seconds.pdf). – bimsapi Aug 19 '15 at 04:03

2 Answers2

1

There should be the problem

https://docs.djangoproject.com/en/1.8/ref/middleware/#gzip-middleware

It will NOT compress content if any of the following are true:

The content body is less than 200 bytes long. The response has already set the Content-Encoding header. The request (the browser) hasn’t sent an Accept-Encoding header containing gzip. You can apply GZip compression to individual views using the gzip_page() decorator.

1

WhiteNoise only enabled gzip for your static files, not for your entire site, so you need to check one of your static files e.g

curl -I -H "Accept-Encoding: gzip" http://www.sikumia.co.il/static/some-file.css
D. Evans
  • 3,242
  • 22
  • 23