在Windows10環境,用Docker部署Nginx反向HTTPS代理的OpenWebUI

在某些環境要求之下,需要使用HTTPS才能正常使用,像是Webhook。但不是所有Docker部署的服務都能設定HTTPS,或是不方便設定,於是就需要一個作爲代理。

Docker化能讓部署變簡單,最近因爲N8N我練習不少,能用Docker去做就少一個虛擬機,對於邊緣主機(運算效能有限),使用Docker又能讓這些裝置多做點什麼。

Nginx可以監聽特定Port,然後把流量再轉到特定主機上。因此,Nginx可以部署HTTPS,代理然後再改用HTTP轉到其他內部服務。

OpenWebUI使用3000 port,並且是HTTP。本例將會使用Nginx作爲反向代理,讓Client能用HTTPS連線並正常使用OpenWebUI服務。

環境

Docker Desktop已經安裝完成。

已經準備好憑證:server.crt、server.key

流程

建立open-webui

透過字元提示命令(CMD)輸入以下指令。

docker run --rm  -p 3000:8080 --add-host=host.docker.internal:host-gateway -v open-webui:/app/backend/data --name open-webui --restart always ghcr.io/open-webui/open-webui:main

此指令有被調整過,在運作結束後會移除Container。下次讓open-webui再運作只要輸入相同指令。

建立Nginx

在一個資料夾,將憑證公鑰與私鑰放入「server.crt」、「server.key」,並新增兩個檔案。

「Dockerfile」

FROM nginx:alpine
# 將本機的 nginx.conf 複製到容器預設配置位置(此處覆蓋預設設定)
COPY nginx.conf /etc/nginx/conf.d/default.conf
# 將 SSL 憑證複製到容器內的指定目錄
COPY server.crt /etc/nginx/ssl/server.crt
COPY server.key /etc/nginx/ssl/server.key

「nginx.conf」

map $http_upgrade $connection_upgrade {
    default "upgrade";
    ''      "close";
}
############################
# HTTPS - 443 轉發
############################
server {
    listen 443 ssl; 
    server_name <Your Server ip or name>; #本例openwebui.ddnsfree.com

    # SSL 憑證 (請替換為實際檔案)
    ssl_certificate     /etc/nginx/ssl/server.crt;
    ssl_certificate_key /etc/nginx/ssl/server.key;

    # 建議的 SSL 安全強化 (可依需求調整)
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers  HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;

    # 主要反向代理到 OpenWebUI
    location / {
        proxy_pass <OpenWebUI ip or name>; #本例 http://172.16.3.47:3000;

        # SSE/WebSocket所需
        proxy_http_version 1.1;

        # 常見標頭
        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;

        # 關閉緩衝,讓 SSE (text/event-stream) 或 chunked streaming 能即時傳遞
        proxy_buffering off;
        proxy_cache off;
        proxy_set_header X-Accel-Buffering no;

        # 若 OpenWebUI 需要 WebSocket (部分功能可能用到)
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        # 關閉 gzip,避免 SSE 被壓縮
        gzip off;
        chunked_transfer_encoding on;
    }
}

然後在該目錄,使用字元提示命令,輸入以下:

# 在「當前資料夾」找到 Dockerfile 以及相關檔案,然後以「my-nginx」做為映像名稱,建立出一個新的 Docker 映像。
docker build -t my-nginx .

# 此指令可讓你在本機 443 埠上對外提供 HTTPS(Nginx)服務,容器在停止後會自動刪除,且能透過 nginx-proxy 作為容器名稱進行管理。
docker run --rm --name nginx-proxy -p 443:443 my-nginx

成果

以HTTPS連入成功。

並且可以成功對話。