gzip 不能在 Django 中使用 Whitenoise
gzip not working in Django with Whitenoise
我在 Heroku 上部署了一个 Django 网站,使用 Whitenoise 提供静态文件。
静态文件工作正常,但根据我用来测试它的各种网站(包括 google 工具),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 "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
应该是有问题
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.
WhiteNoise 只为您的静态文件启用 gzip,而不是为您的整个站点启用 gzip,因此您需要检查其中一个静态文件,例如
curl -I -H "Accept-Encoding: gzip" http://www.sikumia.co.il/static/some-file.css
我在 Heroku 上部署了一个 Django 网站,使用 Whitenoise 提供静态文件。
静态文件工作正常,但根据我用来测试它的各种网站(包括 google 工具),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 "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
应该是有问题
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.
WhiteNoise 只为您的静态文件启用 gzip,而不是为您的整个站点启用 gzip,因此您需要检查其中一个静态文件,例如
curl -I -H "Accept-Encoding: gzip" http://www.sikumia.co.il/static/some-file.css