我在 Heroku 上部署了一个 Django 网站,使用 Whitenoise 提供静态文件。
静态文件工作正常,但根据我用来测试它的各种网站(包括谷歌工具),Gzip 无法正常工作。
这是我的生产设置文件中的代码:
DATABASES['default'] = dj_database_url.config()
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')
ALLOWED_HOSTS = ['*']
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
我的静态文件配置:
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATIC_URL = '/static/'
STATIC_ROOT = 'staticfiles'
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "static"),
)
我的 wsgi.py 文件
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)
可能是什么原因?
我运行了评论中建议的一些命令,看起来 gzip 不起作用:
➜ ~ 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 “接受编码: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