Nginx/Django caching site even when caching explic

2020-05-01 12:07发布

I am working on a development site built in Django in a virtualenv on an Nginx server using uwsgi.

In the uwsgi config file I have:

py-autoreload = 1

Most of the time caching is enabled in settings.py:

CACHES = { 
    'default': {
       'BACKEND': 'django.core.cache.backends.filebased.FileBasedCache',
       'LOCATION': '/var/www/example.com/cache',
    }   
}

but when I am working on the site, I replace the BACKEND and LOCATION lines above with:

CACHES = { 
    'default': {
        'BACKEND': 'django.core.cache.backends.dummy.DummyCache',
    }   
}

The problem I have is that Django or Nginx is caching the site even when I disable caching and empty the cache.

It is very strange -- I can actually delete the main application folder (not the project folder) and the site continues to work.

I ran into this while modifying templates and finding that my updates were not reflected in site content.

I have tried option-reloading in Safari, loading in Chrome, private/incognito windows etc.

The only thing that reliably works is to restart the server.

Does Django, Nginx, uwsgi or virtualenv have some secret caching system that I don't know about?

2条回答
We Are One
2楼-- · 2020-05-01 12:57

Must be either a client-side caching or Nginx caching ;)

(1) in the first case, the browser in not requesting an updated resource because it has been told that the previous resource is still valid;

To confirm this, try using Chrome in incognito and see what happens.

If this is the case, I would add the "never_cache" decorator to the Django views:

from django.views.decorators.cache import never_cache

class MyView(View):

    @never_cache
    def dispatch(self,request,*args,**kwargs):
        return super().dispatch(request,*args,**kwargs)

or, in a function based view:

@never_cache
def myview(request, ...):
   ...

Using the "never_cache" decorator, you instruct the browser not to cache the page, so whenever the user requires it, he's browser will in turn hit the server.

(2) if not, must be Nginx; check all nginx config files:

  • /etc/nginx/nginx.conf
  • /etc/nginx/sites-available/*.conf

and comment out any parameter containing "cache", then restart the service.

I would also turn off "sendfile" as suggested here: https://jeremyfelt.com/2013/01/08/clear-nginx-cache-in-vagrant/

by adjusting file /etc/nginx/nginx.conf as follows:

...
html {
    ...
    sendfile off;
    ...
查看更多
一夜七次
3楼-- · 2020-05-01 13:02

I think you must have to share upper part of your view for more clarification.

But you can try this:

return render(request, template, {'context':context})

查看更多
登录 后发表回答