Your Self-Hosted Agent Runs. Now Put the Gate Outside the Model
Executive Summary
The transition from deploying a Large Language Model (LLM) to securing it in a production-grade homelab environment is often overlooked. Most self-hosting documentation concludes at the docker run command, leaving the agent exposed to the local network or the public internet via reverse proxies without adequate authentication layers. This guide addresses the critical gap between model inference and access control. We implement a sovereign, self-hosted agent architecture where the "gate"—the authentication and authorization layer—is positioned outside the model container. This ensures that the computational resource (the LLM) remains isolated, stateless, and protected from unauthorized API calls, while the gateway handles identity verification, rate limiting, and audit logging.
We utilize Nginx Proxy Manager for its user-friendly TLS management and Authelia as the central authentication provider. The LLM backend is served via Ollama, configured to listen only on localhost. This topology ensures that no external traffic can reach the model directly; all requests must pass through the authenticated gateway. This approach mitigates risks associated with credential leakage, unauthorized API consumption, and prompt injection attacks originating from unverified sources.
Implementation Architecture
The core principle is strict network segmentation. The LLM container binds to 127.0.0.1, making it invisible to the host’s network interface. The gateway (Nginx) listens on 0.0.0.0 but requires a valid session cookie or token from Authelia before forwarding the request to Ollama.
Below is the Docker Compose configuration that establishes this topology. Note the specific port mappings and network constraints.
version: '3.8'
services:
# 1. The LLM Backend - Isolated to localhost only
ollama:
image: ollama/ollama:latest
container_name: ollama
volumes:
- ./ollama_data:/root/.ollama
ports:
- "127.0.0.1:11434:11434" # Critical: Bind to localhost only
networks:
- internal_net
restart: unless-stopped
# 2. The Gateway - Public facing, handles TLS and Routing
nginx:
image: jc21/nginx-proxy-manager:latest
container_name: nginx
ports:
- "80:80"
- "443:443"
- "81:81" # Admin UI
volumes:
- ./data:/data
- ./letsencrypt:/etc/letsencrypt
networks:
- internal_net
- public_net
depends_on:
- authelia
restart: unless-stopped
# 3. The Gatekeeper - Authentication Provider
authelia:
image: authelia/authelia:latest
container_name: authelia
volumes:
- ./authelia:/config
networks:
- internal_net
restart: unless-stopped
networks:
internal_net:
driver: bridge
public_net:
driver: bridge
Configuration Details
- Ollama Binding: The line
- "127.0.0.1:11434:11434"is non-negotiable. If mapped to0.0.0.0, the model is accessible to anyone on the local network, bypassing the gateway. - Authelia Configuration: Authelia requires a
configuration.ymlfile. You must define users, groups, and access control rules. For example, to allow onlyadminto access the Ollama proxy:
# authelia/configuration.yml excerpt
access_control:
default_policy: deny
rules:
- domain: "llama.yourdomain.com"
policies:
- require_one_of:
- group: admins
- Nginx Proxy Configuration: In the Nginx Proxy Manager UI, create a Host pointing to
http://ollama:11434. Enable "Block Common Exploits" and "Websockets Support." Crucially, under the "Access Lists" tab, link this host to the Authelia rule defined above. This ensures that any HTTP request tollama.yourdomain.comis intercepted by Authelia, which validates the user's session before allowing Nginx to forward the request to Ollama.
ShVaSa Intuition
The security posture of self-hosted AI is not defined by the complexity of the model, but by the rigor of the access control layer. By placing the gate outside the model, we adhere to the principle of least privilege. The LLM becomes a dumb compute endpoint, incapable of initiating outbound connections or exposing its internal state.
This architecture also simplifies auditing. Authelia logs every authentication attempt. If a brute-force attack occurs, it is blocked at the gateway level before consuming GPU cycles. Furthermore, this setup allows for seamless integration with existing identity providers (LDAP, OAuth2) via Authelia, enabling enterprise-grade single sign-on (SSO) for personal homelabs. The separation of concerns ensures that updating the LLM version (e.g., from Llama 3.1 to 4.0) does not require changes to the authentication infrastructure, maintaining operational stability.
Operational Pitfalls and Security Practices
Common Pitfalls
- Port Leakage: Forgetting to bind Ollama to
127.0.0.1is the most common error. Always verify withnetstat -tulpn | grep 11434. - Certificate Mismatches: Authelia and Nginx must share the same TLS certificate. Ensure your DNS records point correctly and that Let's Encrypt can validate the domain.
- Session Timeout: Authelia’s default session timeout may be too short for long-running agent interactions. Adjust
sessionsettings inconfiguration.ymlto balance security and usability.
Backup and Recovery
- Ollama Models: Store models on a persistent volume (
./ollama_data). Regularly backup this directory to an off-site location. - Authelia Database: Authelia uses SQLite by default. Backup the
authelia.dbfile. For production, switch to PostgreSQL for better concurrency and backup capabilities. - Nginx Config: The
datadirectory contains all proxy configurations. Backup this directory to preserve your routing rules.
Security Hardening
- Firewall Rules: Use
ufworfirewalldto restrict access to ports 80 and 443 only from trusted IPs if possible. - Fail2Ban: Install Fail2Ban to monitor Nginx logs and ban IPs with excessive failed authentication attempts.
- Regular Updates: Keep all containers updated. Run
docker compose pull && docker compose up -dweekly to patch vulnerabilities.
Mermaid Flowchart: Request Lifecycle
The following diagram illustrates the request flow, highlighting the authentication checkpoint.
sequenceDiagram
participant User as Client Browser
participant Nginx as Nginx Proxy Manager
participant Authelia as Authelia (Gate)
participant Ollama as LLM Backend
User->>Nginx: GET /chat (HTTP)
Nginx->>Authelia: Check Session
Authelia-->>Nginx: No Session (401)
Nginx-->>User: Redirect to Login
User->>Authelia: Login (Credentials)
Authelia-->>User: Set Auth Cookie
User->>Nginx: GET /chat (with Cookie)
Nginx->>Authelia: Validate Cookie
Authelia-->>Nginx: 200 OK (User: admin)
Nginx->>Ollama: Forward Request (localhost:11434)
Ollama-->>Nginx: Response (JSON)
Nginx-->>User: Final Response
This architecture provides a robust, sovereign foundation for self-hosted AI. By rigorously separating the model from the access layer, you ensure that your computational resources are protected, auditable, and secure against unauthorized access.
Grounding & Source Attribution
- Original Topic: Your Self-Hosted Agent Runs. Now Put the Gate Outside the Model
- Primary Author / Dispatcher: KarmaBox via Dev.to
- Curated and Synthesized by: ShVaSa Lab (Sharath Japa)
- Preserved Canonical URL: https://shvasa.us/posts/your-self-hosted-agent-runs-now-put-the-gate-outside-the-model