gpt4 book ai didi

DJango + nginx + gunicorn : how to stop caching

转载 作者:行者123 更新时间:2023-12-01 04:44:20 28 4
gpt4 key购买 nike

我在 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/

    28 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com