DJango + nginx + gunicorn : how to stop caching

标签 django nginx gunicorn django-2.0

我在 nginx 服务器上使用 gunicorn 运行 Django 2.0。我的所有页面都没有立即反射(reflect)更改。如何停止缓存

以下是我的文件:

配置文件

server {
    listen 80;
    server_name sample.com;

    location = /favicon.ico { access_log off; log_not_found off; }

    location /static/ {
        root /home/testapp/testapp_project/test_artciles;
    }
    location /media/ {
        root /home/testapp/testapp_project/test_artciles;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/home/testapp/testapp_project/test_artciles.sock;
    }

}

Django设置.py
import os
from django.contrib import messages

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


SECRET_KEY = 'ssdjhasjdkhjaskdhkajsdhkjasdhkjh'

DEBUG = True

ALLOWED_HOSTS = ['xx.xxx.xx.xxx','127.0.0.1']

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
    'django_extensions',

    # local apps
    'articles',
    'fcm_django',
    'corsheaders',
    'rest_framework',
)

MIDDLEWARE = (
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'webarticles.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(os.path.join(BASE_DIR, 'templates')),
                 ],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'webarticles.wsgi.application'


DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'test.sqlite3'),
    }
}



LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Kolkata'

USE_I18N = True

USE_L10N = True

USE_TZ =True

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]



# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.8/howto/static-files/

STATIC_URL = '/static/'

MEDIA_URL = '/media/'

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

STATIC_ROOT = os.path.join(BASE_DIR, 'static')


STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'frontend'),
)

CORS_ORIGIN_WHITELIST = ('xx.xxx.xx.xxx','127.0.0.1')

我确信它与服务器有关,因为当我在转移到服务器之前在我自己的笔记本电脑上的本地主机中运行此代码时,事情会立即反射(reflect)出来。

最佳答案

如果您在谈论服务器缓存,那么以下是对我有用的步骤:

  • 我的实现细节是
  • gunicorn         19.9.0,
    Django           1.11.16,
    nginx version: nginx/1.10.3,
    Os (Ubuntu)   
    
  • Gunicorn 尚未缓存,除非您已安装并启用 https://pypi.org/project/gunicorn_cache/
  • 禁用 django 缓存,
  • from django.views.decorators.cache import never_cache
    
    
    @never_cache
    def generating the cached data/results():
    
  • 禁用 ngnix 缓存
  • location todisablecaching {
      proxy_no_cache 1;
      proxy_cache_bypass 1;
      ...
    }
    

    https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_no_cache ,

    https://nginx.org/en/docs/http/ngx_http_proxy_module.html#proxy_cache_bypass

    我希望它也适用于你。

    关于DJango + nginx + gunicorn : how to stop caching,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48123986/

    相关文章:

    django - 隐藏 django admin fieldset 只读字段中的标签

    python - 如何在装饰器存在的情况下找到从 url 解析的 View

    ruby - "Incompatible library version"on nokogiri

    php - 仅当 Nginx 文件不存在时才将 URL 重定向到 PHP

    ssl - 将我的站点 cert.key 和 cert.pem 放在 Nginx Docker 容器中是一种好习惯吗?

    django - NGINX 配置中的 proxy_pass 选项有什么作用?

    python - django + virtualenv + gunicorn - 没有名为 django.core.wsgi 的模块?

    Django管理raw_id_fields表显示

    Django 使用用户/IP 进行日志记录

    python - Supervisor 无法启动 Gunicorn : ENOENT