Skip to main content

Django middleware for Kamal healthcheck requests

Project description

django-kamal-healthcheck

Django middleware that handles Kamal healthcheck requests.

The problem

kamal-proxy sends healthcheck requests to /up/ with a Host header that won't match your Django ALLOWED_HOSTS. The request hits Django's SecurityMiddleware or host validation and gets rejected before your app can respond, so kamal-proxy thinks the container is unhealthy.

The solution

HealthCheckMiddleware sits first in the middleware stack and intercepts healthcheck requests, returning a 200 OK response before any host validation or SSL redirect logic runs.

Installation

If you use uv:

uv add django-kamal-healthcheck

or

pip install django-kamal-healthcheck

Configuration

Add kamal_healthcheck to INSTALLED_APPS and put the middleware first in MIDDLEWARE:

# settings.py

INSTALLED_APPS = [
    "kamal_healthcheck",
    # ...
]

MIDDLEWARE = [
    "kamal_healthcheck.middleware.HealthCheckMiddleware",  # must precede SecurityMiddleware
    "django.middleware.security.SecurityMiddleware",
    # ...
]

That's it. Requests to /up/ will return a 200 OK plain text response.

Custom healthcheck path

The default path is /up/, matching kamal-proxy's default. To change it:

KAMAL_HEALTHCHECK_PATH = "/healthz/"

System checks

The app registers two Django system checks that run automatically:

  • kamal_healthcheck.W001: Warns if SECURE_SSL_REDIRECT = True and SecurityMiddleware is ordered before HealthCheckMiddleware. kamal-proxy handles SSL termination and does not follow redirects, so this combination causes healthcheck redirect loops.

  • kamal_healthcheck.W002: Warns if HealthCheckMiddleware is not the first entry in MIDDLEWARE. It must run before host validation and other security middleware.

Requirements

  • Python >= 3.10
  • Django >= 4.2

License

MIT

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

django_kamal_healthcheck-0.1.0.tar.gz (13.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_kamal_healthcheck-0.1.0-py3-none-any.whl (4.4 kB view details)

Uploaded Python 3

File details

Details for the file django_kamal_healthcheck-0.1.0.tar.gz.

File metadata

  • Download URL: django_kamal_healthcheck-0.1.0.tar.gz
  • Upload date:
  • Size: 13.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for django_kamal_healthcheck-0.1.0.tar.gz
Algorithm Hash digest
SHA256 a635041690f453cafa771e37b91875ae1b25d55d14ce63eb9955bca549425667
MD5 2b9101ab5d63bb6002a49243cdf38a05
BLAKE2b-256 006a0c174c84aa1e1dd7c4aae97c495e4b61916daed31bf54fb2d449d4da2c3a

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_kamal_healthcheck-0.1.0.tar.gz:

Publisher: ci.yml on smulloni/django-kamal-healthcheck

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file django_kamal_healthcheck-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_kamal_healthcheck-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8a6482aa9df00cb9de86cfe3d6c9e9fb4d7cc396f1705d9c7d9efedfbc850b53
MD5 1441f674ab881e4dcf5ad722076911f5
BLAKE2b-256 efd946849224a1229341b8bf48730ed6968fc8389ee4c26a069760e184d468cc

See more details on using hashes here.

Provenance

The following attestation bundles were made for django_kamal_healthcheck-0.1.0-py3-none-any.whl:

Publisher: ci.yml on smulloni/django-kamal-healthcheck

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page