As you must know, Ollama has no built-in authentication. If you open port 11434 to the internet, anyone can use your GPU, pull models, and send requests at your cost. This guide shows you exactly how to expose Ollama securely, keeping the raw port locked down, running it as a proper systemd service, and putting Nginx in front with HTTPS and a password.
Why not just open port 11434? Ollama has no built-in access control. Any exposed instance allows unauthenticated model usage, resource hijacking, and prompt injection from anyone who finds the port. The right way is to keep Ollama on localhost and let Nginx handle the public-facing side.
Before starting, here is the security model you are building:
HTML/XML
Internet → Nginx (HTTPS + Basic Auth, port 443) → Ollama (localhost:11434)
Ollama listens only on 127.0.0.1:11434. Nginx sits in front, handles SSL termination, checks credentials, and forwards clean requests to Ollama internally.
Now, follow the steps below to expose Ollama securely.
Step 2: Lock Ollama to Localhost Only
By default, Ollama listens on 127.0.0.1:11434. You want to make sure this does not accidentally change. The safe way is to set the OLLAMA_HOST environment variable explicitly in the systemd service override, not in the main service file, which gets overwritten on updates.
To do this, run the command below:
Bash
sudosystemctleditollama
This opens a blank override file. Add the following:
Important Note: Never set OLLAMA_HOST=0.0.0.0 unless Ollama is inside a private Docker network. Setting it to 0.0.0.0 binds to all interfaces and exposes the API to everyone. That is the unsafe pattern this guide avoids.
Confirm Ollama is only listening on localhost:
Bash
ss-tlnp|grep11434
You should only see 127.0.0.1:11434, not 0.0.0.0:11434.
Make sure port 11434 is NOT in the allow list. Check your rules:
Bash
sudoufwstatus
Step 5: Install Nginx and Set Up Basic Auth Password
Install Nginx if it is not already on your server:
Bash
sudoaptupdatesudoaptinstallnginx-y
Start and enable it:
Bash
sudosystemctlenablenginxsudosystemctlstartnginx
Because this is how you expose Ollama securely to specific users, you need a password file. Install apache2-utils to get the htpasswd tool:
Bash
sudoaptinstallapache2-utils-y
Create the password file and your first user:
Bash
sudohtpasswd-c/etc/nginx/.htpasswdyourusername
You will be prompted to enter and confirm a password. The file stores a hashed version of it.
To add more users later, you can omit the -c so you do not overwrite the file:
Bash
sudohtpasswd/etc/nginx/.htpasswdanotheruser
Step 6: Configure Nginx as a Reverse Proxy
Create a new Nginx config file for your domain:
Bash
sudonano/etc/nginx/sites-available/ollama
Paste the following content with your domain:
Bash
server{listen80;server_nameyourdomain.com;location/{auth_basic"Ollama API – Restricted";auth_basic_user_file/etc/nginx/.htpasswd;proxy_passhttp://127.0.0.1:11434;proxy_http_version1.1;proxy_set_headerHost$host;proxy_set_headerX-Real-IP$remote_addr;proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for;proxy_set_headerX-Forwarded-Proto$scheme;proxy_set_headerOrigin"";proxy_set_headerReferer"";proxy_bufferingoff;proxy_read_timeout 300s;proxy_connect_timeout 60s;}}
What each directive does:
HTML/XML
1auth_basic with auth_basic_user_file #Enforces username and password before any request reaches Ollama.23proxy_pass http://127.0.0.1:11434 #Sends the request to Ollama on localhost.45proxy_buffering off #Required for streaming responses so tokens appear in real time.67proxy_read_timeout 300s #Gives large models enough time to generate long outputs.89Origin "" and Referer "" #Strips headers that can cause CORS issues from the proxy side.
Note: Never hard-code credentials in scripts. Use environment variables or a .env file with restricted permissions:
Bash
chmod600.env
Conclusion
Getting Ollama to work on localhost is the easy part. The hard part is making it accessible from the internet without creating a security hole. By following this guide, you have the right way to expose Ollama securely for real-world use. You keep full control, your API is password-protected and encrypted, and you can check everything through journalctl when something needs debugging.
For heavy models, multi-user inference, or always-on deployments, a dedicated GPU server makes a real difference. You can check out the best GPU Dedicated Servers, built to run LLMs at scale.
No, as long as you used sudo systemctl edit ollama to create an override file. The override lives in /etc/systemd/system/ollama.service.d/override.conf and is not changed by updates.
Yes. In your Nginx config, replace the single location / block with specific location /api/generate and location /api/chat blocks, and return 403 for everything else.
Run ss -tlnp | grep 11434 on the server. You should only see 127.0.0.1:11434. If you see 0.0.0.0:11434, fix the OLLAMA_HOST variable immediately.
We use cookies to run PerLod and to understand how the site is used. You can accept everything, or open Manage to turn individual categories off. Cookie policy.