網頁

2026年1月2日 星期五

將 open webui 的 http 轉成 https

產生證書,需先執行一個 nginx 在 80 port 上,以便 cerbot 產生證書
$ sta_nginx_certbot.sh
$ docker run --rm -it \
  -v $(pwd)/nginx/certs:/etc/letsencrypt \
  -v $(pwd)/nginx/acme:/var/www/certbot \
  certbot/certbot certonly \
  --webroot \
  -w /var/www/certbot \
  -d www.sunhousetechnology.com.tw
證書產生在下列目錄
nginx/certs/live/www.sunhousetechnology.com.tw/fullchain.pem
nginx/certs/live/www.sunhousetechnology.com.tw/privkey.pem
停止 nginx_certbot

docker nginx 加入 nginx_certbot.conf, 以便在更新證書時,不用停止 nginx
server {
    listen 80 default_server;
    server_name _;

    root /var/www/html;
    index index.html index.htm index.nginx-debian.html;
    
    location /.well-known/acme-challenge/ {
        root /var/www/certbot;
    }

    location / {
        return 200 "nginx certbot ok\n";
    }
}

更新證書
$ docker run --rm \
  -v $(pwd)/nginx/certs:/etc/letsencrypt \
  -v $(pwd)/nginx/acme:/var/www/certbot \
  certbot/certbot renew

測試證書
$ docker run --rm \
  -v $(pwd)/nginx/certs:/etc/letsencrypt \
  -v $(pwd)/nginx/acme:/var/www/certbot \
  certbot/certbot renew --dry-run


# 1. Nginx 是否在 webnet
docker network inspect webnet
# 2. open-webui 是否在 webnet
docker ps
# 3. Nginx 內部能否解析
docker exec -it nginx getent hosts open-webui
# 4. Nginx 內部直連測試
docker exec -it nginx curl http://open-webui:8080
# nginx.conf 修改後 nginx 重開
docker restart nginx
# 測試命令
curl -k -H "Cache-Control: no-cache" \
     -H "Pragma: no-cache" \
     https://www.sunhousetechnology.com.tw/

為了避免一些 docker 沒有啟動,造成 nginx 無法啟動,將
    location /sub-path/ {
        proxy_pass http://sub-path:8001;
    }
改成
    location /sub-path/ {
        set $p8001 http://sub-path:8001;
        rewrite ^/sub-path/(.*)$ /$1 break;
        proxy_pass $p8001;
    }

open-webui 無法掛到 https://www.sunhousetechnology.com.tw/open-webui 必須使用 /
    location / {
        set $webui http://open-webui:8080;
        # 無法使用 rewrite, 只能用 location /
        proxy_pass $webui;
        # 避免下列 WebSocket 錯誤
        # "GET /ws/socket.io/?EIO=4&transport=websocket HTTP/1.1" 400 46
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

原先也要加入 dashboard, 但兩者同時都使用 WebSocket, 而且是 GET /ws 開頭,所以只好放棄
    location /dashboard/ {
        set $dashboard http://dashboard:8080;
        rewrite ^/dashboard/(.*)$ /$1 break;
        proxy_pass $dashboard;
        # 避免下列 WebSocket 錯誤
        # "GET /ws HTTP/1.1" 500 21
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }

沒有留言:

張貼留言