Last active
November 9, 2023 03:40
-
-
Save kpavlovsky/31813d99ae847636407e7d47d08920b1 to your computer and use it in GitHub Desktop.
Nginx Cache Setup for Django Site. Uses Django Dummy Cache Backend by default to set required Cache-Control Headers for downstream Nginx Cache
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
REDIS_URL = os.getenv('REDIS_URL', 'redis://localhost:6379') | |
CACHES = { | |
"redis": { | |
"BACKEND": "django_redis.cache.RedisCache", | |
"LOCATION": REDIS_URL, | |
"OPTIONS": { | |
"CLIENT_CLASS": "django_redis.client.DefaultClient", | |
} | |
}, | |
"default": { | |
"BACKEND": "django.core.cache.backends.dummy.DummyCache", | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
proxy_cache_path /path/to/cache levels=1:2 keys_zone=examplesite:10m max_size=1g inactive=60m use_temp_path=off; | |
server { | |
listen 443 ssl; | |
server_name example.com; | |
# ssl and other stuff excluded | |
location / { | |
proxy_cache examplesite; | |
proxy_cache_revalidate on; | |
proxy_cache_min_uses 1; | |
proxy_cache_use_stale error timeout updating http_500 http_502 http_503 http_504; | |
proxy_cache_background_update on; | |
proxy_cache_lock on; | |
proxy_cache_bypass $http_pragma; | |
add_header X-Cache-Status $upstream_cache_status; | |
proxy_pass http://127.0.0.1:18003; | |
proxy_read_timeout 600; | |
proxy_send_timeout 600; | |
proxy_set_header Host $http_host; | |
proxy_set_header X-Real-IP $remote_addr; | |
proxy_set_header X-Forwarded-Proto $scheme; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.shortcuts import render | |
from django.views.decorators.cache import cache_page | |
@cache_page(60 * 15) | |
def articles(request, *args, **kwargs): | |
"""Not cached by the app, response will have Cache-Control headers, Cached by downstream NGINX""" | |
context = { | |
"object_list": Article.objects.filter(is_active=True) | |
} | |
return render(request, template_name="articles/articles_index.html", context=context) | |
@cache_page(60 * 15, cache="redis") | |
def other_page(request, *args, **kwargs): | |
"""Cached by the app in redis cache backend and by downstream NGINX since Cache-Control headers are also set""" | |
context = { | |
"result": of_extensive_compute() | |
} | |
render(request, template_name="articles/whatever.html", context=context) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment