步骤
反向代理将 TeslaMate API 置于您自己的域名之后,配有受信任的 HTTPS 证书以及用户名和密码。HedgieMate 会在每个请求中发送 Basic Auth 凭据。
适合人群: 已经在运行反向代理,或拥有公网 IP 和域名的用户。
-
将 teslamate-api.example.com 这样的主机名指向您的服务器,并将端口 80 和 443 转发到运行代理的机器。
-
创建密码哈希。使用 Caddy 时,运行下方的命令;使用 Nginx 时,创建 htpasswd 文件。
Caddycaddy hash-password --plaintext 'your-password'
Nginxhtpasswd -c /etc/nginx/.htpasswd hedgiemate
-
参照下方任一示例将站点添加到代理配置中,然后重新加载代理。
Caddyfileteslamate-api.example.com { basic_auth { hedgiemate <paste the hash here> } reverse_proxy teslamateapi:8080 }nginx.confserver { listen 443 ssl; server_name teslamate-api.example.com; ssl_certificate /etc/letsencrypt/live/teslamate-api.example.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/teslamate-api.example.com/privkey.pem; location / { auth_basic "TeslaMate API"; auth_basic_user_file /etc/nginx/.htpasswd; proxy_pass http://127.0.0.1:8080; proxy_set_header Host $host; } } -
在 Safari 中打开 https://teslamate-api.example.com/api/v1/cars。它应先要求输入用户名和密码,然后显示 JSON。
-
在 HedgieMate 中,将 HTTPS 地址填入“API 端点 URL”,将“认证类型”设为“基本认证”,并填写相同的“用户名”和“密码”。
在 HedgieMate 中填写的内容
| 字段 | 值 |
|---|---|
| API 端点 URL | https://teslamate-api.example.com |
| 认证类型 | 基本认证 |
| 用户名和密码 | 您在代理上设置的凭据 |
须知
- Caddy 会自动申请并续期 Let's Encrypt 证书。使用 Nginx 时,请用 Certbot。
- 切勿通过明文 HTTP 发布 TeslaMate API。受信任的 Let's Encrypt 证书比依赖“允许自签名证书”更好。
- 如果 Safari 显示 502 或 504,说明代理无法访问 teslamateapi 容器。请检查容器名和端口。