本教程是社区贡献内容,并非 Open WebUI 团队提供支持。它仅作为如何根据您的特定用例自定义 Open WebUI 的演示。想贡献吗?请查看贡献教程。
使用 Nginx 配置 HTTPS
确保用户与 Open WebUI 之间的安全通信至关重要。HTTPS(超文本传输安全协议)对传输的数据进行加密,保护其免受窃听和篡改。通过将 Nginx 配置为反向代理,您可以无缝地为 Open WebUI 部署添加 HTTPS,从而增强安全性和可信度。
本指南提供了三种设置 HTTPS 的方法
- 自签名证书:非常适合使用 docker 进行开发和内部使用。
- Let's Encrypt:非常适合需要受信任 SSL 证书的生产环境,使用 docker。
- Windows + 自签名证书:适用于 Windows 上开发和内部使用的简化说明,无需 docker。
选择最适合您部署需求的方法。
- Nginx Proxy Manager
- Let's Encrypt
- 自签名证书
- Windows
Nginx Proxy Manager
Nginx Proxy Manager (NPM) 使您能够轻松管理反向代理,并使用 Let's Encrypt 的有效 SSL 证书保护您的本地应用程序,例如 Open WebUI。此设置支持 HTTPS 访问,这是许多移动浏览器出于安全要求使用语音输入功能所必需的,同时又无需将应用程序的特定端口直接暴露在互联网上。
先决条件
- 运行 Docker 的家庭服务器以及运行中的 open-webui 容器。
- 一个域名(可以是免费的,如 DuckDNS,或付费的,如 Namecheap/GoDaddy)。
- Docker 和 DNS 配置的基础知识。
步骤
-
为 Nginx 文件创建目录
mkdir ~/nginx_config
cd ~/nginx_config -
使用 Docker 设置 Nginx Proxy Manager
nano docker-compose.yml
services:
app:
image: 'jc21/nginx-proxy-manager:latest'
restart: unless-stopped
ports:
- '80:80'
- '81:81'
- '443:443'
volumes:
- ./data:/data
- ./letsencrypt:/etc/letsencrypt
运行容器
docker-compose up -d
-
配置 DNS 和域名
- 登录您的域名提供商(例如 DuckDNS)并创建一个域名。
- 将域名指向您的代理的本地 IP(例如 192.168.0.6)。
- 如果使用 DuckDNS,请从其仪表板获取 API 令牌。
以下是在 https://www.duckdns.org/domains 中操作的简单示例:
- 设置 SSL 证书
- 访问 Nginx Proxy Manager,地址为 http://server_ip:81。例如:
192.168.0.6:81
- 使用默认凭据登录(admin@example.com / changeme)。按要求更改它们。
- 前往 SSL Certificates → Add SSL Certificate → Let's Encrypt。
- 输入您的电子邮件和从 DuckDNS 获取的域名。一个域名包含星号,另一个不包含。例如:
*.hello.duckdns.org
和hello.duckdns.org
。 - 选择 Use a DNS challenge,选择 DuckDNS,并粘贴您的 API 令牌。例如:
dns_duckdns_token=f4e2a1b9-c78d-e593-b0d7-67f2e1c9a5b8
- 同意 Let's Encrypt 的条款并保存。如果需要更改传播时间(120 秒)。
- 创建代理主机
- 对于每个服务(例如 openwebui, nextcloud),前往 Hosts → Proxy Hosts → Add Proxy Host。
- 填写域名(例如 openwebui.hello.duckdns.org)。
- 将 Scheme 设置为 HTTP(默认),启用
Websockets support
并指向您的 Docker IP(如果 open-webui 的 docker 与 NGINX manager 运行在同一台计算机上,此 IP 将与之前的 IP 相同(例如:192.168.0.6
))。 - 选择之前生成的 SSL 证书,强制使用 SSL,并启用 HTTP/2。
- 添加您用于连接 open-webui 的 URL(否则会遇到 HTTPS 错误)
- 前往您的 open-webui → Admin Panel → Settings → General
- 在 Webhook URL 文本框中,输入您将通过 Nginx 反向代理连接到 open-webui 的 URL。例如:
hello.duckdns.org
(不是必需的)或openwebui.hello.duckdns.org
(这是必需的)。
访问 WebUI:
通过 HTTPS 访问 Open WebUI,地址为 hello.duckdns.org
或 openwebui.hello.duckdns.org
(取决于您的设置方式)。
防火墙注意事项:请注意,本地防火墙软件(如 Portmaster)可能会阻止内部 Docker 网络流量或所需的端口。如果您遇到问题,请检查您的防火墙规则,确保允许此设置所需的通信。
Let's Encrypt
Let's Encrypt 提供大多数浏览器信任的免费 SSL 证书,非常适合生产环境。
先决条件
- 系统上已安装 Certbot。
- DNS 记录已正确配置以指向您的服务器。
步骤
-
为 Nginx 文件创建目录
mkdir -p conf.d ssl
-
创建 Nginx 配置文件
conf.d/open-webui.conf
:server {
listen 80;
server_name your_domain_or_IP;
location / {
proxy_pass http://host.docker.internal:3000;
# Add WebSocket support (Necessary for version 0.5.0 and up)
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# (Optional) Disable proxy buffering for better streaming response from models
proxy_buffering off;
# (Optional) Increase max request size for large attachments and long audio messages
client_max_body_size 20M;
proxy_read_timeout 10m;
}
} -
简化的 Let's Encrypt 脚本
enable_letsencrypt.sh
:#!/bin/bash
# Description: Simplified script to obtain and install Let's Encrypt SSL certificates using Certbot.
DOMAIN="your_domain_or_IP"
EMAIL="your_email@example.com"
# Install Certbot if not installed
if ! command -v certbot &> /dev/null; then
echo "Certbot not found. Installing..."
sudo apt-get update
sudo apt-get install -y certbot python3-certbot-nginx
fi
# Obtain SSL certificate
sudo certbot --nginx -d "$DOMAIN" --non-interactive --agree-tos -m "$EMAIL"
# Reload Nginx to apply changes
sudo systemctl reload nginx
echo "Let's Encrypt SSL certificate has been installed and Nginx reloaded."使脚本可执行
chmod +x enable_letsencrypt.sh
-
更新 Docker Compose 配置
将 Nginx 服务添加到您的
docker-compose.yml
services:
nginx:
image: nginx:alpine
ports:
- "80:80"
- "443:443"
volumes:
- ./conf.d:/etc/nginx/conf.d
- ./ssl:/etc/nginx/ssl
depends_on:
- open-webui -
启动 Nginx 服务
docker compose up -d nginx
-
运行 Let's Encrypt 脚本
执行脚本以获取并安装 SSL 证书
./enable_letsencrypt.sh
访问 WebUI
通过 HTTPS 访问 Open WebUI,地址为
自签名证书
使用自签名证书适用于对信任要求不高的开发或内部使用场景。
步骤
-
为 Nginx 文件创建目录
mkdir -p conf.d ssl
-
创建 Nginx 配置文件
conf.d/open-webui.conf
:server {
listen 443 ssl;
server_name your_domain_or_IP;
ssl_certificate /etc/nginx/ssl/nginx.crt;
ssl_certificate_key /etc/nginx/ssl/nginx.key;
ssl_protocols TLSv1.2 TLSv1.3;
location / {
proxy_pass http://host.docker.internal:3000;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# (Optional) Disable proxy buffering for better streaming response from models
proxy_buffering off;
# (Optional) Increase max request size for large attachments and long audio messages
client_max_body_size 20M;
proxy_read_timeout 10m;
}
} -
生成自签名 SSL 证书
openssl req -x509 -nodes -days 365 -newkey rsa:2048 \
-keyout ssl/nginx.key \
-out ssl/nginx.crt \
-subj "/CN=your_domain_or_IP" -
更新 Docker Compose 配置
将 Nginx 服务添加到您的
docker-compose.yml
services:
nginx:
image: nginx:alpine
ports:
- "443:443"
volumes:
- ./conf.d:/etc/nginx/conf.d
- ./ssl:/etc/nginx/ssl
depends_on:
- open-webui -
启动 Nginx 服务
docker compose up -d nginx
访问 WebUI
通过 HTTPS 访问 Open WebUI,地址为
在 Windows 上不使用 Docker 的情况下,使用自签名证书和 Nginx
对于基本的内部/开发安装,您可以使用 nginx 和自签名证书将 Open WebUI 代理到 HTTPS,从而允许在局域网内使用麦克风输入等功能。(默认情况下,大多数浏览器不允许在不安全的非 localhost URL 上进行麦克风输入)
本指南假设您使用 pip 安装了 Open WebUI 并正在运行 open-webui serve
步骤 1:安装 openssl 用于生成证书
您首先需要安装 openssl
您可以从 Shining Light Productions (SLP) 网站下载并安装预编译的二进制文件。
或者,如果您安装了 Chocolatey,可以使用它快速安装 OpenSSL
- 打开命令提示符或 PowerShell。
- 运行以下命令安装 OpenSSL
choco install openssl -y
验证安装
安装后,打开命令提示符并输入
openssl version
如果显示 OpenSSL 版本(例如,OpenSSL 3.x.x ...
),则表示安装正确。
步骤 2:安装 nginx
从 nginx.org 下载适用于 Windows 的官方 Nginx,或使用 Chocolatey 等包管理器。将下载的 ZIP 文件解压到某个目录(例如 C:\nginx)。
步骤 3:生成证书
运行以下命令
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout nginx.key -out nginx.crt
将生成的 nginx.key 和 nginx.crt 文件移动到您选择的文件夹,或移动到 C:\nginx 目录。
步骤 4:配置 nginx
用文本编辑器打开 C:\nginx\conf\nginx.conf
如果您希望 Open WebUI 在本地局域网中可访问,请务必使用 ipconfig
记录您的局域网 IP 地址,例如 192.168.1.15
按如下方式设置
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 120;
#gzip on;
# needed to properly handle websockets (streaming)
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
# Redirect all HTTP traffic to HTTPS
server {
listen 80;
server_name 192.168.1.15;
return 301 https://$host$request_uri;
}
# Handle HTTPS traffic
server {
listen 443 ssl;
server_name 192.168.1.15;
# SSL Settings (ensure paths are correct)
ssl_certificate C:\\nginx\\nginx.crt;
ssl_certificate_key C:\\nginx\\nginx.key;
ssl_protocols TLSv1.2 TLSv1.3;
ssl_ciphers ECDHE-ECDSA-CHACHA20-POLY1305:ECDHE-RSA-CHACHA20-POLY1305:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256;
ssl_prefer_server_ciphers on;
# OCSP Stapling
#ssl_stapling on;
#ssl_stapling_verify on;
# Proxy settings to your local service
location / {
# proxy_pass should point to your running localhost version of open-webui
proxy_pass http://localhost:8080;
# Add WebSocket support (Necessary for version 0.5.0 and up)
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
# (Optional) Disable proxy buffering for better streaming response from models
proxy_buffering off;
# (Optional) Increase max request size for large attachments and long audio messages
client_max_body_size 20M;
proxy_read_timeout 10m;
}
}
}
保存文件,并通过运行 nginx -t
检查配置是否存在错误或语法问题。根据您的安装方式,您可能需要先 cd C:\nginx
运行 nginx
启动 nginx。如果 nginx 服务已启动,您可以通过运行 nginx -s reload
重新加载新配置
您现在应该可以通过 https://192.168.1.15(或您自己的局域网 IP)访问 Open WebUI。请务必根据需要允许 Windows 防火墙访问。
后续步骤
设置 HTTPS 后,通过以下地址安全访问 Open WebUI
如果您使用域名,请确保您的 DNS 记录配置正确。对于生产环境,建议使用 Let's Encrypt 获取受信任的 SSL 证书。