使用 Nginx Proxy Manager 將 HTTPS 流量反代至 Home Assistant 實體機

📌 架構說明
在這個案例中,我們的目標是從外部瀏覽器透過 https://homeassistant.richardnote.com
安全存取 Home Assistant,但 Home Assistant 是跑在另一台實體的 Raspberry Pi(IP:192.168.0.34
)上,並沒有 SSL 憑證。
我們的架構如下:
- 路由器(Router)將外部的 443 port 轉發至本地的
192.168.0.43
- 在
.43
上有一個 Docker 容器跑著 Nginx Proxy Manager - 該 Nginx 負責反向代理,把請求導到
.34
上的 Home Assistant(port:8123)
以下是網路拓樸圖:
services:
app:
container_name: 'NginxProxyManager'
image: 'jc21/nginx-proxy-manager:latest'
restart: unless-stopped
ports:
- '80:80'
- '81:81'
- '443:443'
manager/letsencrypt:/etc/letsencrypt
networks:
agent-server-network:
ipv4_address: 172.29.0.4
networks:
agent-server-network:
name: "database"
external: true
🔧 NPM 設定 Proxy Host
開啟 NPM 後,新增一個 Proxy Host,設定如下:
Details:
欄位 | 設定值 |
---|---|
Domain Names | homeassistant.richardnote.com |
Scheme | http (因為 HA 是 http) |
Forward Hostname | 192.168.0.34 |
Forward Port | 8123 (HA 預設 port) |
Websockets | ✅ 打開 |
Block Exploits | ✅ 打開 |
SSL:
- ✅ Request a new SSL Certificate
- ✅ Force SSL
- ✅ HTTP/2 Support
- ✅ HSTS Enabled
- 輸入 Email
- ✅ 同意 Let's Encrypt 條款
⚠ 確保你的 router 有開放 80 與 443 port 到 NPM 主機(192.168.0.43)
⚠ 錯誤排查:400 Bad Request?
即便設定看起來沒問題,若 Home Assistant 顯示 400 Bad Request
,原因通常是它不信任這個反向代理伺服器。
解法如下。
🛠 Home Assistant 允許 NPM 作為 Proxy
你必須修改 Home Assistant 的 configuration.yaml
,加入:
http:
use_x_forwarded_for: true
trusted_proxies:
- 192.168.0.43 # NPM 實體主機 IP
這段代表:
- use_x_forwarded_for:允許使用 Nginx 傳遞來的
X-Forwarded-For
取得訪客真實 IP - trusted_proxies:允許哪台機器可以幫你代理轉送(只信任這些 IP)
然後記得重啟 Home Assistant。
✅ 成功驗證
你應該會看到熟悉的 Home Assistant 登入畫面,且連線為 HTTPS 🔒