Django的缓存中间件无法在Gunicorn中使用Debug = False

时间:2013-02-20 15:22:02

标签: django caching gunicorn

我有一个与Gunicorn和nginx一起部署的Django项目。我还有一个memcached运行,并设置了Django缓存中间件()来缓存该站点。

使用DEBUG = True运行时一切正常,但是当我切换到DEBUG = False时,我在尝试访问该站点时在Gunicorn日志中收到以下错误:

2013-02-20 16:09:50 [25196] [ERROR] Error handling request
Traceback (most recent call last):
  File "/home/toursprung/.virtualenvs/myproject/lib/python2.6/site-packages/gunicorn/workers/sync.py", line 102, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
  File "/home/toursprung/.virtualenvs/myproject/lib/python2.6/site-packages/django/contrib/staticfiles/handlers.py", line 67, in __call__
    return self.application(environ, start_response)
  File "/home/toursprung/.virtualenvs/myproject/lib/python2.6/site-packages/django/core/handlers/wsgi.py", line 247, in __call__
    status_text = STATUS_CODE_TEXT[response.status_code]
AttributeError: 'NoneType' object has no attribute 'status_code'

奇怪的是,我在Django发送给我的错误邮件中收到了另一个错误。在这里,我得到以下结果:( GZIP中间件中的错误.wtf?)

Traceback (most recent call last):

  File "/home/toursprung/.virtualenvs/myproject/lib/python2.6/site-packages/django/core/handlers/base.py", line 188, in get_response
    response = middleware_method(request, response)
  File "/home/toursprung/.virtualenvs/myproject/lib/python2.6/site-packages/django/middleware/gzip.py", line 16, in process_response
    if len(response.content) < 200:
  File "/home/toursprung/.virtualenvs/myproject/lib/python2.6/site-packages/django/http/__init__.py", line 699, in _get_content
    return ''.join([str(e) for e in self._container])

UnicodeEncodeError: 'ascii' codec can't encode character u'\x8b' in position 26: ordinal not in range(128)

任何人都知道为什么会这样?

提前致谢, 安东

1 个答案:

答案 0 :(得分:2)

这是我的中间件的订购!

这是我的订单,错误

MIDDLEWARE_CLASSES = (
    'django.middleware.cache.UpdateCacheMiddleware',
    'myproject.middleware.StripCookieMiddleware', # fix caching problem with analytics cookies
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.gzip.GZipMiddleware',
    ...
    'django.middleware.cache.FetchFromCacheMiddleware'
)

我将GZipMiddleware移到UpdateCacheMiddleware正下方(好吧,我的小中间件之间剥离了谷歌分析cookie),现在一切都按预期工作了。

所以这是中间件的正确顺序:

MIDDLEWARE_CLASSES = (
    'django.middleware.cache.UpdateCacheMiddleware',
    'myproject.middleware.StripCookieMiddleware', # fix caching problem with analytics cookies
    'django.middleware.gzip.GZipMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.locale.LocaleMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    ...
    'django.middleware.cache.FetchFromCacheMiddleware'
)

也许这将有助于将来的某个人......