4

I am running Django 2.0 using gunicorn on nginx server. All my pages are not reflecting the changes immediately. How to stop caching

the below are my files:

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

I am sure its something to do with the server because when i ran this code in localhost on my own laptop before transferring to server, things are reflecting immediately.

Santhosh
  • 9,965
  • 20
  • 103
  • 243

2 Answers2

3

You should restart gunicorn to update working version after you changed code on server:

sudo systemctl restart gunicorn

or you can use --reload mode that will update gunicorn once code changed:

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

You can read about it on docs https://docs.gunicorn.org/en/stable/settings.html#reload

Gvidoou
  • 41
  • 4
  • 1
    Welcome to Stackoverflow and thank you for your effort. However, your answer seems to be guessing and is not really explaining your solution. Maybe you could improve your answer by adding information why a restart solves the issue of the question. Thank you! – nCessity Feb 23 '21 at 22:48
  • @nCessity lol. Question was "All my pages are not reflecting the changes immediately.", AFAIK if you update code on server and restarting nginx changese will not affect current state cause gunicorn use cached version until it get restarted. – Gvidoou Mar 15 '21 at 20:47
  • That is valuable information and it should go into your answer. It would be even better if you added the source of your information (e.g. gunicorn docs) as well. Don't understand me wrong: I don't think that your answer is wrong. I think it is guessing ("Probably") and does not explain why this is the solution. Both can and should be improved. See https://stackoverflow.com/help/how-to-answer and also https://meta.stackoverflow.com/questions/405939/do-good-answers-require-an-explanation – nCessity Mar 16 '21 at 19:42
  • @nCessity added link and way to use it above – Gvidoou Mar 22 '21 at 08:55
2

If you are talking about server caching, then here are the steps worked for me:

  1. My implementation details are
gunicorn         19.9.0,
Django           1.11.16,
nginx version: nginx/1.10.3,
Os (Ubuntu)   
  1. Gunicorn is not caching already unless you have installed and enabled https://pypi.org/project/gunicorn_cache/

  2. Disabling django caching,

from django.views.decorators.cache import never_cache


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

I hope it work for you as well.

MoAdel
  • 59
  • 5