ShVaSa Blog
DIY & Self-Hosting • 5 min read

Your Self-Hosted Agent Runs. Now Put the Gate Outside the Model

Published on 2026-11-16 by Sharath Japa, ShVaSa Lab Lead
diyselfhostingsystemsopen-source
Discuss on r/shvasa

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.

YAML
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

  1. Ollama Binding: The line - "127.0.0.1:11434:11434" is non-negotiable. If mapped to 0.0.0.0, the model is accessible to anyone on the local network, bypassing the gateway.
  2. Authelia Configuration: Authelia requires a configuration.yml file. You must define users, groups, and access control rules. For example, to allow only admin to access the Ollama proxy:
YAML
# authelia/configuration.yml excerpt
access_control:
  default_policy: deny
  rules:
    - domain: "llama.yourdomain.com"
      policies:
        - require_one_of:
            - group: admins
  1. 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 to llama.yourdomain.com is 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

Backup and Recovery

  1. Ollama Models: Store models on a persistent volume (./ollama_data). Regularly backup this directory to an off-site location.
  2. Authelia Database: Authelia uses SQLite by default. Backup the authelia.db file. For production, switch to PostgreSQL for better concurrency and backup capabilities.
  3. Nginx Config: The data directory contains all proxy configurations. Backup this directory to preserve your routing rules.

Security Hardening

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

Author Avatar

Sharath Japa, ShVaSa Lab Lead

Systems & AI Infrastructure Researcher, DGX Station homelab lead. Engineering RL environments, edge silicon inference, and local reasoning model distillation. Verified Profiles: Google Scholar • Google Patents • LinkedIn • GitHub (@sharathjapa) • Medium • r/shvasa.


Canonical Source: https://shvasa.us/posts/your-self-hosted-agent-runs-now-put-the-gate-outside-the-model

Advertisement

Related Research & Architectural Blueprints