【问题标题】:DJango + nginx + gunicorn: how to stop cachingDJango + nginx + gunicorn:如何停止缓存
【发布时间】:2018-06-15 21:05:36
【问题描述】:

我在 nginx 服务器上使用 gunicorn 运行 Django 2.0。我所有的页面都没有立即反映这些变化。如何停止缓存

以下是我的文件:

nginx.conf

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

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

【问题讨论】:

  • 您需要显示“未更新”页面的代码;它与您在此处显示的任何内容无关。
  • 你解决了吗?

标签: django nginx gunicorn django-2.0


【解决方案1】:

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

  1. 我的实现细节是
gunicorn         19.9.0,
Django           1.11.16,
nginx version: nginx/1.10.3,
Os (Ubuntu)   
  1. Gunicorn 尚未缓存,除非您已安装并启用 https://pypi.org/project/gunicorn_cache/

  2. 禁用 django 缓存,

from django.views.decorators.cache import never_cache


@never_cache
def generating the cached data/results():
  1. 禁用 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

我希望它也对你有用。

【讨论】:

    【解决方案2】:

    更改服务器上的代码后,您应该重新启动 gunicorn 以更新工作版本:

    sudo systemctl restart gunicorn
    

    或者您可以使用 --reload 模式,一旦代码更改,该模式将更新 gunicorn:

    gunicorn --workers=2 test:app --reload
    

    您可以在文档https://docs.gunicorn.org/en/stable/settings.html#reload 上阅读有关它的信息

    【讨论】:

    • 欢迎来到 Stackoverflow,感谢您的努力。但是,您的答案似乎是在猜测,并没有真正解释您的解决方案。也许您可以通过添加为什么重新启动可以解决问题的信息来改进您的答案。谢谢!
    • @nCessity 哈哈。问题是“我的所有页面都没有立即反映更改。”,AFAIK 如果您更新服务器上的代码并重新启动 nginx 更改不会影响当前状态,因为 gunicorn 在重新启动之前使用缓存版本。
    • 这是有价值的信息,它应该包含在您的答案中。如果您还添加了信息的来源(例如 gunicorn 文档),那就更好了。不要理解我的错误:我不认为你的答案是错误的。我认为这是猜测(“可能”)并且没有解释为什么这是解决方案。两者都可以而且应该改进。见stackoverflow.com/help/how-to-answermeta.stackoverflow.com/questions/405939/…
    • @nCessity 在上面添加了链接和使用方法
    猜你喜欢
    • 2016-04-24
    • 2018-09-01
    • 2022-01-27
    • 2019-01-11
    • 2013-03-25
    • 2013-03-01
    • 2021-01-04
    • 2017-12-11
    • 2021-03-19
    相关资源
    最近更新 更多