[Answered ]-Django's caching middleware not working in Gunicorn with Debug=False

2👍

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…

👤Anton

Leave a comment