Django's caching middleware not working in Gun

2019-08-12 23:41发布

I have a Django project deployed with Gunicorn and nginx. I also have a memcached running and the Django cache middleware () set up to cache the site.

Everything works fine when running with DEBUG=True, but when I switch to DEBUG=False I get the following error in Gunicorn log when trying to access the site:

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'

The strange thing is, that I get another error in the error email Django is sending me. Here I get the following: (An error in the GZIP Middleware. 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)

Anyone a clue why this is happening?

Thanks in advance, Anton

1条回答
我命由我不由天
2楼-- · 2019-08-13 00:18

It was the ordering of my middlewares!

This is the ordering I had, which is wrong:

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'
)

I moved the GZipMiddleware right below the UpdateCacheMiddleware (ok, there is my little middleware that strips google analytics cookies in between) and now everything is working as expected.

So this is the right order of middlewares:

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'
)

Maybe this will help somebody in the future...

查看更多
登录 后发表回答