Source: Dev.to | Dec 17, 2025 (UTC)
Tired of seeing this in your Django logs?
They do no harm because Django handles them properly based on allowed hosts env, but those bot requests pass the reverse-proxy and end up in the Python application – which is not as fast as a reverse proxy built in Go, C, C++, Zig or something else.
Here is how to stop those bot requests using just a django command which generates the Caddyfile template.
The generated Caddyfile will just block by default all routes that are not registered by Django and will block any hosts other that the one specified (ex: bot trying to curl on VPS IP).
We’ll use Django Templates to pass existing paths to Caddyfile template.
# Catch any request not matching your domain (like IP scans) :80, :443 { abort } {{ app_url }} { # Create a named matcher for the host @strict_host { not host {{ app_url }} } # Abort if the host isn't exactly yours handle @strict_host { abort } # Enable high-performance compression encode zstd gzip # 1. Static Files (Update with path to staticfiles) # Handled first to ensure they are always accessible. handle_path /static/* { root * /webapp/staticfiles file_server } # 2. Define Allowed Routes # This matcher includes all Django URLs plus your explicit SSE route. @allowed { path /favicon.ico path /robots.txt path /sitemap.xml path /sse-events* {% for route in routes %} path {{ route }} {% endfor %} } # 3. Handle Allowed Traffic handle @allowed { # SSE Sidecar (Specific route handling inside the allowed block) reverse_proxy /sse-events* docker-service-name-sse-sidecar:5687 # Main Django App (Default for other allowed routes) reverse_proxy docker-service-name-app:8000 } # 4. Fallback: Block everything else handle { respond "Access Denied" 403 } }
Enter fullscreen mode
Exit fullscreen mode



Залишити відповідь