Middleware based health check for Django
Project description
Django Load Balancer Health Check
Aliveness check for Django that bypasses ALLOWED_HOSTS for the purposes of load balancers
Purpose
When running on some app platforms and behind some load balancers, it is often the case that the host header is not set appropriately for health checks. When these platforms perform an HTTP health check without the proper host header an error 400 bad request will be returned by Django. This package provides a method to allow for for a simple "aliveness" health check that bypasses the ALLOWED_HOSTS
protections. ALLOWED_HOSTS
protection is bypassed only for the status aliveness check URL and not for any other requests.
This package is not an alternative to something like django-health-check, but is instead a better alternative than the TCP health check that is the default on many load balancers. The TCP health checks can only see if your uWSGI/Gunicorn/Uvicorn/etc server is alive, while this package ensures that requests are being properly routed to Django.
How it works
This package works by returning an HTTP response from a middleware class before Django's common middleware performs the host check. The Django URL routing system is also bypassed since that happens "below" all middleware. During request processing, django-lb-health-check checks if the request is a GET request and matches settings.ALIVENESS_URL
. If it is, a static plain text "200 OK" response is returned bypassing any other processing.
Usage
Install django-lb-health-check
pip install django-lb-health-check
Add lb_health_check to your middleware. It must be above django.middleware.common.CommonMiddleware and should be below django.middleware.security.SecurityMiddleware, as high in the stack as possible to prevent any queries or unneeded code from running during a health check.
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'lb_health_check.middleware.AliveCheck', # <- New middleware here
'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',
]
Set the URL you want to use for your aliveness check. Note that a GET request to this URL will shadow any other route you have defined through the Django URL mapper. Aliveness URL can be a string for a single health check URL or a list of strings if you want the aliveness check to run from multiple URLs. The multiple URL strategy is helpful if you are changing the URL of the endpoint by allowing both the old and new URLs to be checked.
ALIVENESS_URL = "/health-check/"
Test your health check after starting your server:
curl localhost:8000/health-check/
OK
Note that the example app has lb_health_check in INSTALLED_APPS. This is only nessecary for testing purposes - the app does not use any Django models, admin, views, URL routing, or the like that would require it to be listed in INSTALLED_APPS.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-lb-health-check-0.2.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 722be4b1a4b81aaac724f5fa184719868f26d248ba81c3eafdf6d8a8042c55be |
|
MD5 | fc6198cef702454ce3726168f495ee3f |
|
BLAKE2b-256 | b5d0c33fa2cb941b648a21e50e2b47517bcf215f7c9953594d6629aaf46a4ec3 |
Hashes for django_lb_health_check-0.2.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8271ebf196927b80a5ef4b14fb848232f6f2c1750c533cbecca1423963570d7f |
|
MD5 | 6a235b0406bc340d4e129411756a81cd |
|
BLAKE2b-256 | 927c67dd67951063b3e2af5cfe1db648e70542fb607bf1d3e5980ab087d5753b |