部署指南

通过反向代理连接

用 Caddy 或 Nginx 在 TeslaMate API 前加上 HTTPS 和 Basic Auth。

步骤

反向代理将 TeslaMate API 置于您自己的域名之后,配有受信任的 HTTPS 证书以及用户名和密码。HedgieMate 会在每个请求中发送 Basic Auth 凭据。

适合人群: 已经在运行反向代理,或拥有公网 IP 和域名的用户。

  1. 将 teslamate-api.example.com 这样的主机名指向您的服务器,并将端口 80 和 443 转发到运行代理的机器。

  2. 创建密码哈希。使用 Caddy 时,运行下方的命令;使用 Nginx 时,创建 htpasswd 文件。

    Caddy
    caddy hash-password --plaintext 'your-password'
    Nginx
    htpasswd -c /etc/nginx/.htpasswd hedgiemate
  3. 参照下方任一示例将站点添加到代理配置中,然后重新加载代理。

    Caddyfile
    teslamate-api.example.com {
    	basic_auth {
    		hedgiemate <paste the hash here>
    	}
    	reverse_proxy teslamateapi:8080
    }
    nginx.conf
    server {
        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;
        }
    }
  4. 在 Safari 中打开 https://teslamate-api.example.com/api/v1/cars。它应先要求输入用户名和密码,然后显示 JSON。

  5. 在 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 容器。请检查容器名和端口。

遇到困难?

配置过程不顺畅?别担心,我们随时为您提供协助。