Nginx/Django缓存站点,即使明确禁用了缓存 [英] Nginx/Django caching site even when caching explicitly disabled

查看:99
本文介绍了Nginx/Django缓存站点,即使明确禁用了缓存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 uwsgi Nginx 服务器上的 virtualenv 中在 Django 中构建的开发站点上工作.

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

在uwsgi配置文件中,我有:

In the uwsgi config file I have:

py-autoreload = 1

大多数时间都在 settings.py 中启用了缓存:

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',
    }   
}

但是在网站上工作时,我将上面的BACKEND和LOCATION行替换为:

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',
    }   
}

我的问题是Django或Nginx正在缓存站点,即使我禁用了缓存并清空了缓存.

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.

我曾尝试在Safari中重新加载选项,在Chrome,私密/隐身窗口中加载等.

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.

Django,Nginx,uwsgi或virtualenv是否有一些我不知道的秘密缓存系统?

推荐答案

必须是客户端缓存或Nginx缓存;)

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

(1)在第一种情况下,浏览器未请求更新的资源,因为 已经被告知先前的资源仍然有效;

(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;

要确认这一点,请尝试以隐身模式使用Chrome并查看会发生什么情况.

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

如果是这种情况,我将"never_cache"装饰器添加到Django视图中:

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, ...):
   ...

使用"never_cache"修饰符,指示浏览器不要缓存页面, 因此,只要用户需要,他的浏览器就会依次访问服务器.

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)如果不是,则必须为Nginx;检查所有Nginx配置文件:

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

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

并注释掉任何包含"cache"的参数,然后重新启动服务.

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/

通过如下调整文件/etc/nginx/nginx.conf:

...
html {
    ...
    sendfile off;
    ...

这篇关于Nginx/Django缓存站点,即使明确禁用了缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆