gzip not working in Django with Whitenoise

泄露秘密 提交于 2019-12-08 19:23:08

问题


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

回答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



回答2:


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.



来源:https://stackoverflow.com/questions/31808874/gzip-not-working-in-django-with-whitenoise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!