Emby 多域名接入脚本教程:emby-multidomain-nl.sh

适用场景:荷兰主站已经跑着 Emby,想再挂 jp/us/hl/hk 这类中转子域名,不改坏现有 emby.xxx 配置

本文按「能照着做」的规格写:前置条件、执行、验收、回滚、排错。


1. 脚本是干什么的

emby-multidomain-nl.sh 会在荷兰 VPS 上:

  1. 用 Cloudflare DNS-01 签发/复用独立证书(默认证书名 emby-relay
  2. 新建独立 nginx 站点:/etc/nginx/sites-available/emby-relay
  3. 把新域名反代到本机 Emby:http://127.0.0.1:8096
  4. nginx -t 通过后才 reload;失败自动回滚,不影响现有站点

它不会修改现有的 sites-enabled/emby(例如 emby.imjj.de)。

典型链路:

客户端
  → 中转域名(如 hk.example.com)
  → 海外中转机 / realm
  → 荷兰 VPS nginx(emby-relay)
  → 127.0.0.1:8096 Emby

2. 前置条件

  • 系统:Debian / Ubuntu,已装 nginx
  • 本机 Emby 已监听:127.0.0.1:8096
  • 域名在 Cloudflare,可创建 API Token(Zone DNS Edit)
  • root 执行
  • 新域名建议灰云(DNS only),中转场景更稳

3. 准备 Cloudflare 令牌

Cloudflare → My Profile → API Tokens → Create Token
Edit zone DNS 模板,Zone 选你的主域名。

执行前:

export CF_TOKEN="你的_Cloudflare_API_Token"

脚本会把凭据写到:

/root/.secrets/cloudflare.ini

权限建议:

chmod 700 /root/.secrets
chmod 600 /root/.secrets/cloudflare.ini
不要把 Token 写进公开仓库、截图、群聊。

4. 放置脚本并执行

# 上传到荷兰机,例如:
# /root/emby-multidomain-nl.sh

chmod +x /root/emby-multidomain-nl.sh
export CF_TOKEN="你的_Cloudflare_API_Token"
/root/emby-multidomain-nl.sh

首次默认会处理类似:

  • jp.你的域名
  • us.你的域名

后续若已有 emby-relay 站点,可按同样模式手工扩:

  1. certbot certonly --dns-cloudflare ... --cert-name emby-relay --expand -d 旧域名 -d 新域名
  2. emby-relayserver_name 追加新域名
  3. nginx -t && systemctl reload nginx

例如新增 hk

certbot certonly   --dns-cloudflare   --dns-cloudflare-credentials /root/.secrets/cloudflare.ini   --dns-cloudflare-propagation-seconds 30   --cert-name emby-relay   -d jp.example.com -d us.example.com -d hl.example.com -d hk.example.com   --expand --non-interactive --agree-tos --register-unsafely-without-email

然后编辑:

vim /etc/nginx/sites-available/emby-relay
# server_name 增加 hk.example.com
nginx -t && systemctl reload nginx

5. 推荐 nginx 形态(脱敏示例)

server {
    listen 80;
    server_name jp.example.com us.example.com hl.example.com hk.example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl;
    server_name jp.example.com us.example.com hl.example.com hk.example.com;

    ssl_certificate     /etc/letsencrypt/live/emby-relay/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/emby-relay/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;

    client_max_body_size 0;

    location / {
        proxy_pass http://127.0.0.1:8096;
        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;
        proxy_set_header Upgrade           $http_upgrade;
        proxy_set_header Connection        "upgrade";

        proxy_buffering off;
        proxy_request_buffering off;
        proxy_read_timeout 3h;
        proxy_send_timeout 3h;
    }
}

6. DNS / 中转怎么配

方案 A:域名直连荷兰机

hk.example.com  A  →  荷兰机公网IP   (灰云)

方案 B:香港/日本/美国机 realm 中转(常用)

hk.example.com  A  →  中转机公网IP   (灰云)
中转机 realm/转发 → 荷兰机 443
荷兰机 nginx 识别 Host = hk.example.com 后反代 Emby

关键点:

  • 证书装在荷兰机 nginx(SNI 到荷兰)
  • 中转机只做 TCP/TLS 透传或你现有的 realm 方案
  • 不要让中转机自己用错误证书终结 HTTPS

7. 验收

本机:

curl -sk --resolve hk.example.com:443:127.0.0.1   https://hk.example.com/System/Info/Public

公网:

curl -I https://hk.example.com/
curl -s https://hk.example.com/System/Info/Public

期望:

  • HTTPS 证书 SAN 含新域名
  • 返回 Emby 的 ServerName / Version JSON
  • 浏览器能打开 Web 播放页

8. 回滚

/root/emby-multidomain-nl.sh --rollback

会移除 emby-relay 站点并 reload nginx。
证书默认保留;若确认不要:

certbot delete --cert-name emby-relay

9. 常见问题

  1. 证书没有新域名
    --expand 重签,确认 SAN。
  2. 外网打开是别的站
    DNS 没指到中转/荷兰,或中转机 Host/SNI 没透传。
  3. 能打开网页但播放断流
    检查 proxy_buffering off、超时、WebSocket 头。
  4. nginx -t 失败
    脚本会回滚;先看 /tmp/nginxtest.lognginx -t

10. 安全提醒

  • Cloudflare Token、私钥、服务器 IP 清单不要进文章仓库明文
  • 5900/9222 这类调试端口与 Emby 中转无关,不要混开公网
  • 证书续期靠 certbot timer;改完域名后记得验证 renew dry-run

一句话:这个脚本负责「荷兰侧安全增开 Emby 中转域名」;中转机/realm 负责把流量送过来。两边职责分开,最不容易把现网搞炸。

本文著作权归作者 [ doudoudoubao ] 享有,未经作者书面授权,禁止转载,封面图片来源于 [ 互联网 ] ,本文仅供个人学习、研究和欣赏使用。如有异议,请联系博主及时处理。

发表留言