A simple, self-hosted proxy to secure your Ollama API with a private Bearer Token.
A simple yet powerful proxy server for Ollama that allows you to secure API access with a private Bearer Token. It's the perfect solution if you want to expose your Ollama instance on a local network or the internet safely.
By default, the Ollama API is open and accessible to anyone on the same network. This project acts as a "gatekeeper" – it sits between the user and the Ollama server, verifying that every request includes a valid API key.
Primary use case:
- You have an Ollama server running on one machine.
- You want to securely access its API from another machine, an application, or even through a Cloudflare tunnel, without exposing it to the entire world.
- Security: Protects Ollama API access with a Bearer Token.
- Full Forwarding: Forwards all requests (including parameters and body) to Ollama, with full support for streaming responses (
stream: true). - Easy Deployment: Ready-to-use configuration with Docker and Docker Compose.
- Lightweight: Built on the fast FastAPI framework.
The request flow is straightforward:
sequenceDiagram
participant Client
participant Ollama API Proxy
participant Ollama Instance
Client->>Ollama API Proxy: Request with "Authorization: Bearer <YOUR_KEY>"
Ollama API Proxy->>Ollama API Proxy: 1. Verify API Key
alt Key is valid
Ollama API Proxy->>Ollama Instance: 2. Forward original request
Ollama Instance-->>Ollama API Proxy: Response
Ollama API Proxy-->>Client: Forward response
else Key is invalid
Ollama API Proxy-->>Client: 401 Unauthorized
end
You can run this proxy either by building from the source or by using the pre-built Docker image directly.
This is the easiest way to get started. You don't need to clone the repository.
-
Create a
docker-compose.ymlfile: Create a new file nameddocker-compose.ymland paste the following content into it:version: "3.8" services: ollama-proxy: image: ghcr.io/rafalgawlik/ollama-proxy:latest container_name: ollama-proxy restart: unless-stopped pull_policy: always ports: - "8087:8087" environment: # CHANGE TO YOUR OWN SECRET API KEY! - PROXY_API_KEY=supersecret # ENTER THE CORRECT URL OF YOUR OLLAMA INSTANCE - OLLAMA_URL=http://your-ollama:11434
-
Customize the environment variables:
PROXY_API_KEY: Set your own unique key here.OLLAMA_URL: Enter the IP address and port where your Ollama instance is running.
-
Run the container: In the same directory where you created the file, run:
docker-compose up -d
The proxy will now be running on port
8087.
Use this method if you want to modify the code.
-
Clone the repository:
git clone [https://github.com/rafalgawlik/ollama-proxy.git](https://github.com/rafalgawlik/ollama-proxy.git) cd ollama-proxy -
Build and run the container:
docker-compose up --build -d
To send a request to Ollama through the proxy, you need to add the Authorization header with your key.
curl http://localhost:8087/api/generate \
-H "Content-Type: application/json" \
-H "Authorization: Bearer supersecret" \
-d '{
"model": "llama3",
"prompt": "Why is the sky blue?",
"stream": true
}'Note: Remember to replace
supersecretwith your API key andlocalhostwith the IP address of the machine running the proxy.
curl http://localhost:8087/api/generate \
-H "Content-Type: application/json" \
-H "Authorization: Bearer supersecret" \
-d '{
"model": "llama3",
"prompt": "Why is the sky blue?",
"stream": false
}'.
├── docker-compose.yml # Defines the proxy service, environment variables, and network
├── Dockerfile # Instructions for building the app's Docker image
├── main.py # The main application logic in FastAPI
└── requirements.txt # Python dependencies
Made with ❤️ using Python, FastAPI, and Docker.