温馨提示×

Ubuntu Jellyfin中如何启用SSL

小樊
35
2025-12-18 22:14:39
栏目: 云计算

Ubuntu 上启用 Jellyfin SSL 的两种常用方式

  • 方式一:使用 Nginx/Apache 反向代理 + Let’s Encrypt(Certbot),对外仅暴露 443,推荐、维护成本低、自动续期方便。
  • 方式二:在 Jellyfin 内启用 HTTPS 监听端口(默认 8920),直接对外提供 HTTPS

前置准备

  • 准备一个可解析到服务器的域名(如:media.example.com),并在域名 DNS 指向服务器公网 IP。
  • 开放防火墙端口:
    • 反向代理方案:放行 80/443(UFW 可用命令:sudo ufw allow "WWW Full")。
    • 直连方案:放行 8096(HTTP)8920(HTTPS)
  • 安装证书工具(任选其一):
    • Nginx:sudo apt install certbot python3-certbot-nginx -y
    • Apache:sudo apt install certbot python3-certbot-apache -y

方式一 反向代理 Nginx 配置示例(推荐)

  1. 安装与启用模块
  • sudo apt install nginx -y
  • 启用模块:sudo a2enmod proxy proxy_http ssl proxy_wstunnel remoteip http2 headers(若使用 Nginx,则无需 a2enmod,Nginx 原生支持)
  1. 获取证书(Certbot 自动修改 Nginx 配置)
  • sudo certbot --nginx -d media.example.com
  1. 手动配置 Nginx(可选,若 Certbot 自动配置不满足需求)
  • 新建配置:sudo nano /etc/nginx/sites-available/jellyfin
  • 粘贴以下内容(按需替换域名与路径):
server {
    listen 80;
    server_name media.example.com;
    return 301 https://$host$request_uri;
}

server {
    listen 443 ssl http2;
    server_name media.example.com;

    ssl_certificate /etc/letsencrypt/live/media.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/media.example.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;

    # 反向代理到 Jellyfin HTTP 端口
    location / {
        proxy_pass http://127.0.0.1:8096;
        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 X-Forwarded-Port $server_port;

        # WebSocket 支持
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}
  • 启用站点:sudo ln -s /etc/nginx/sites-available/jellyfin /etc/nginx/sites-enabled/
  • 测试并重载:sudo nginx -t && sudo systemctl reload nginx
  1. 访问测试
  • 打开浏览器访问:https://media.example.com,应自动跳转至 HTTPS 并提供有效证书。

方式二 在 Jellyfin 内启用 HTTPS 直连

  1. 申请证书(Webroot 方式,证书将保存在 /etc/letsencrypt/live/your_domain/
  • sudo certbot certonly --webroot -w /var/www/html -d media.example.com
  1. 配置 Jellyfin 启用 HTTPS 端口
  • 编辑配置文件:sudo nano /etc/jellyfin/config.xml
  • 确保包含或调整为如下关键项(端口可按需改为 8920):
<HttpServer Port="8096" />
<HttpsPortNumber>8920</HttpsPortNumber>
<CertificatePath>/etc/letsencrypt/live/media.example.com/fullchain.pem</CertificatePath>
<CertificateKeyPath>/etc/letsencrypt/live/media.example.com/privkey.pem</CertificateKeyPath>
<EnableHttps>true</EnableHttps>
<RequireHttps>true</RequireHttps>
  • 重启服务:sudo systemctl restart jellyfin
  1. 防火墙放行 8920(若启用 UFW:sudo ufw allow 8920/tcp

  2. 访问测试

  • 打开浏览器访问:https://media.example.com:8920

验证与常见问题

  • 证书续期
    • 测试自动续期:sudo certbot renew --dry-run
    • 设置定时任务(已默认随 Certbot 安装):sudo systemctl enable --now certbot.timer
  • 常见问题排查
    • 证书路径或权限错误:证书文件必须对 jellyfin 进程可读(证书通常位于 /etc/letsencrypt/live/your_domain/,属 root,Jellyfin 以 jellyfin 用户运行时需确保权限或改用反向代理)。
    • WebSocket 不可用(播放器无法连接):反向代理需开启 HTTP/1.1Upgrade 头(见上文配置)。
    • 端口冲突:确保 8096/8920 未被其他进程占用(sudo ss -tulpen | grep -E '8096|8920')。
    • 云服务器安全组:除系统防火墙外,还需在云平台安全组放行 80/4438920

安全与维护建议

  • 对外仅暴露 443,将 8096 限制为 127.0.0.1 访问(反向代理方案)。
  • 启用 HSTS(在 Nginx 的 443 配置中添加:add_header Strict-Transport-Security "max-age=63072000; includeSubDomains; preload";)。
  • 定期更新系统与 Jellyfin:sudo apt update && sudo apt upgrade -y

以上两种方式均可稳定启用 HTTPS。生产环境更推荐 Nginx/Apache 反向代理,便于统一管理证书、HTTP/2、压缩与访问控制。

0