Skip to content

Installation

Install from PyPI

pip install nai-security

With optional integrations:

pip install "nai-security[all]"

Individual extras:

pip install "nai-security[axes]"
pip install "nai-security[import-export]"
pip install "nai-security[unfold]"
pip install "nai-security[celery]"
pip install "nai-security[redis]"

[all] is axes + import-export + unfold + redis. Celery is separate.

For local tests and audit tools (uv, pinned by uv.lock):

uv sync --all-extras
make test

1. Add the app

INSTALLED_APPS = [
    # Add the app for each extra you installed and want active.
    # "unfold",         # [unfold]        — themed admin
    # "import_export",  # [import-export] — bulk import/export on block lists
    # "axes",           # [axes]          — login lockout
    "nai_security",
]

Installing an extra and not listing its app is safe — that feature is simply inactive. Listing an app whose package is not installed is not: Django fails at startup.

2. Add middleware (order matters)

SecurityMiddleware must come after AuthenticationMiddleware. The package raises ImproperlyConfigured at startup if order is wrong.

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "nai_security.middleware.SecurityMiddleware",
    "nai_security.middleware.RateLimitLoggingMiddleware",  # optional
]

3. Configure GeoIP path

GEOIP_PATH = "/var/lib/geoip/GeoLite2-Country.mmdb"
# or a directory containing GeoLite2-Country.mmdb

GEOIP_PATH may be the .mmdb file or a directory; a directory is resolved to GeoLite2-Country.mmdb inside it.

Download DB:

python manage.py download_geoip

4. Migrate

python manage.py migrate nai_security
# or
python manage.py migrate

5. Cache / Redis

The package caches settings, block lookups and whitelist results through Django’s cache framework (django.core.cache). Configure a shared backend in production — Redis, Memcached, or the database:

CACHES = {
    "default": {
        "BACKEND": "django.core.cache.backends.redis.RedisCache",
        "LOCATION": "redis://127.0.0.1:6379/1",
    }
}

On Django’s default LocMemCache the cache is per process, and two things break:

  • A toggle saved in the admin, or a new BlockedIP, invalidates only the worker that handled the save. Every other worker keeps serving its stale value for up to 5 minutes — 30 seconds for an IP that was not blocked the last time that worker looked it up.
  • LocMemCache culls at MAX_ENTRIES = 300. The middleware writes up to three keys per distinct client IP, so roughly 100 addresses is enough to evict the SecuritySettings singleton with them.

Django's Redis backend needs the redis client: install nai-security[redis] (or [all]). Memcached and database caches need nothing extra.

Verify

  1. Start your project
  2. Open admin → Security models appear
  3. Hit a blocked IP/country and confirm a SecurityLog row is created

Next: Configuration · Admin-Guide