跳到主要内容
警告

本教程由社区贡献,不获得 Open WebUI 团队的支持。它仅作为演示,说明如何根据您的特定用例定制 Open WebUI。想要贡献?请查看贡献教程。

Open WebUI 的 HAProxy 配置

HAProxy(高可用代理)是一个专门的负载均衡和反向代理解决方案,具有高度可配置性,旨在以相对较低的资源占用处理大量连接。欲了解更多信息,请参阅:https://www.haproxy.org/

安装 HAProxy 和 Let's Encrypt

首先,安装 HAProxy 和 Let's Encrypt 的 certbot

Redhat 衍生版

sudo dnf install haproxy certbot openssl -y

Debian 衍生版

sudo apt install haproxy certbot openssl -y

HAProxy 配置基础

HAProxy 的配置默认存储在 /etc/haproxy/haproxy.cfg。此文件包含决定 HAProxy 如何运行的所有配置指令。

HAProxy 与 Open WebUI 配合使用的基本配置非常简单。

 #---------------------------------------------------------------------
# Global settings
#---------------------------------------------------------------------
global
# to have these messages end up in /var/log/haproxy.log you will
# need to:
#
# 1) configure syslog to accept network log events. This is done
# by adding the '-r' option to the SYSLOGD_OPTIONS in
# /etc/sysconfig/syslog
#
# 2) configure local2 events to go to the /var/log/haproxy.log
# file. A line like the following can be added to
# /etc/sysconfig/syslog
#
# local2.* /var/log/haproxy.log
#
log 127.0.0.1 local2

chroot /var/lib/haproxy
pidfile /var/run/haproxy.pid
maxconn 4000
user haproxy
group haproxy
daemon

#adjust the dh-param if too low
tune.ssl.default-dh-param 2048
#---------------------------------------------------------------------
# common defaults that all the 'listen' and 'backend' sections will
# use if not designated in their block
#---------------------------------------------------------------------
defaults
mode http
log global
option httplog
option dontlognull
option http-server-close
option forwardfor #except 127.0.0.0/8
option redispatch
retries 3
timeout http-request 300s
timeout queue 2m
timeout connect 120s
timeout client 10m
timeout server 10m
timeout http-keep-alive 120s
timeout check 10s
maxconn 3000

#http
frontend web
#Non-SSL
bind 0.0.0.0:80
#SSL/TLS
bind 0.0.0.0:443 ssl crt /path/to/ssl/folder/

#Let's Encrypt SSL
acl letsencrypt-acl path_beg /.well-known/acme-challenge/
use_backend letsencrypt-backend if letsencrypt-acl

#Subdomain method
acl chat-acl hdr(host) -i subdomain.domain.tld
#Path Method
acl chat-acl path_beg /owui/
use_backend owui_chat if chat-acl

#Pass SSL Requests to Lets Encrypt
backend letsencrypt-backend
server letsencrypt 127.0.0.1:8688

#OWUI Chat
backend owui_chat
# add X-FORWARDED-FOR
option forwardfor
# add X-CLIENT-IP
http-request add-header X-CLIENT-IP %[src]
http-request set-header X-Forwarded-Proto https if { ssl_fc }
server chat <ip>:3000

您会看到我们为 Open WebUI 和 Let's Encrypt 都配置了 ACL 记录(路由)。要将 WebSocket 与 OWUI 配合使用,您需要配置 SSL,最简单的方法是使用 Let's Encrypt。

您可以使用子域名方法或路径方法将流量路由到 Open WebUI。子域名方法需要一个专用的子域名(例如,chat.yourdomain.com),而路径方法允许您通过您域名上的特定路径访问 Open WebUI(例如,yourdomain.com/owui/)。选择最适合您需求的方法并相应地更新配置。

信息

您需要将端口 80 和 443 暴露给您的 HAProxy 服务器。这些端口是 Let's Encrypt 验证您的域名和处理 HTTPS 流量所必需的。您还需要确保您的 DNS 记录已正确配置,指向您的 HAProxy 服务器。如果您在家中运行 HAProxy,则需要在路由器中进行端口转发,将端口 80 和 443 转发到您的 HAProxy 服务器。

使用 Let's Encrypt 颁发 SSL 证书

在启动 HAProxy 之前,您需要生成一个自签名证书作为占位符,直到 Let's Encrypt 颁发正式证书。以下是生成自签名证书的方法:

openssl req -x509 -newkey rsa:2048 -keyout /tmp/haproxy.key -out /tmp/haproxy.crt -days 3650 -nodes -subj "/CN=localhost"

然后将密钥和证书合并到一个 HAProxy 可以使用的 PEM 文件中

cat /tmp/haproxy.crt /tmp/haproxy.key > /etc/haproxy/certs/haproxy.pem

信息

请务必根据您的需求和配置更新 HAProxy 配置。

设置好 HAProxy 配置后,您可以使用 certbot 获取和管理您的 SSL 证书。Certbot 将处理与 Let's Encrypt 的验证过程,并在证书即将过期时自动更新您的证书(假设您使用 certbot 自动续订服务)。

您可以通过运行 haproxy -c -f /etc/haproxy/haproxy.cfg 来验证 HAProxy 配置。如果没有错误,您可以使用 systemctl start haproxy 启动 HAProxy,并使用 systemctl status haproxy 验证其运行状态。

为了确保 HAProxy 随系统启动,请运行 systemctl enable haproxy

配置好 HAProxy 后,您可以使用 Let's Encrypt 颁发有效的 SSL 证书。首先,您需要向 Let's Encrypt 注册。这只需执行一次。

certbot register --agree-tos --email your@email.com --non-interactive

然后您可以请求您的证书

certbot certonly -n --standalone --preferred-challenges http --http-01-port-8688 -d yourdomain.com

证书颁发后,您需要将证书和私钥文件合并为一个 HAProxy 可以使用的 PEM 文件。

cat /etc/letsencrypt/live/{domain}/fullchain.pem /etc/letsencrypt/live/{domain}/privkey.pem > /etc/haproxy/certs/{domain}.pem
chmod 600 /etc/haproxy/certs/{domain}.pem
chown haproxy:haproxy /etc/haproxy/certs/{domain}.pem

然后您可以重启 HAProxy 以应用新证书:systemctl restart haproxy

HAProxy 管理器(简易部署选项)

如果您希望有工具自动管理您的 HAProxy 配置和 Let's Encrypt SSL,我编写了一个简单的 Python 脚本并创建了一个 Docker 容器,您可以使用它们来创建和管理 HAProxy 配置,并管理 Let's Encrypt 证书生命周期。

https://github.com/shadowdao/haproxy-manager

警告

如果您使用此脚本或容器,请不要将端口 8000 公开暴露!