Middleware based health check for Django
Project description
Django Load Balancer Health Check
Aliveness check for Django that bypasses ALLOWED_HOSTS
Purpose
When running on app platforms (Heroku/DigitalOcean/etc), Kubernetes, Elastic Load Balancer, and other similar platforms 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 is because the Django Common Middleware tests the host header and raises a DisalowedHost exception if it doesn't match what is in ALLOWED_HOSTS. This package provides an alternative health/aliveness check that is returned by middlware and thus bypasses the ALLOWED_HOSTS check. In order to accomplish this, django-lb-health-check middleware checks if the incoming URL is for the known health check URL and returns a response - bypassing the majority of the Django platform. It is not designed as a replacement for something like django-health-check, but instead as a better alternative to a TCP based aliveness check that ensures your Django project has been started and is responding to HTTP instead of just having a port open.
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.1.1.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | e82b7c989a587929b9a838291d1f7bc87f3adb478786f2e0aad9f374c3400f7b |
|
MD5 | 57ee673204d70749fedfa5d542cf9767 |
|
BLAKE2b-256 | 95f90e10e2577a0c9fdfafeb7c087bd0eb7df0b21968f49e4650ddca155d81a0 |
Hashes for django_lb_health_check-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 33059754387282d3eb872cfe1da989b5fb573d91301de8a22cb19e8f2fb8a2b0 |
|
MD5 | bda8e3178e1e2d9060f026091f6be703 |
|
BLAKE2b-256 | 39e1650664b92d91be3c1ec7879911919ee3225d2e2bb09447e975cdb16e36aa |