进阶 docs.invidious.io 2026-10-13 23:01:14 · 31 阅读
第21章 利用 Docker 提升 Invidious 公共实例的性能与稳定性
提升公共实例的性能与稳定性
本教程由 yewtu.be 的所有者 unixfox 撰写。如需相关帮助,找他更合适。
概述
本教程将讲解如何提升你服务器上公共实例的性能,并降低其内存和 CPU 消耗。
本教程推荐使用 Docker,因为操作更简单。你也可以在 Docker 之外参考本教程的思路尝试,但重点还是放在 Docker 上。
操作步骤
1) 调整 config.yml
为减轻 PostgreSQL 数据库的负载:
enable_user_notifications: false
用户通知将被禁用。
对于大型实例,建议将此选项设为 false,以减少写入数据库的数据量,从而提升实例的整体性能。 2) 运行多个 Invidious 进程 Invidious 是单线程的,因此运行多个进程可以更好地利用服务器的多核性能。
此外,当请求量达到一定程度时,Invidious 会变得迟缓,多个进程可以缓解这一问题。 本教程将讲解如何运行多个 Invidious 容器,并使用 Docker 中的 NGINX 作为负载均衡器。
如果你的反向代理本身已经在 Docker 中,也可以改造它,让请求直接发送到各个 Invidious 容器。 以下假设你没有修改默认安装的 3000 端口。如果改过端口,则需要在后面的配置文件和命令中相应调整。 在 docker-compose.yml 中,删除以下绑定 Invidious 端口的配置: ports: - "127.0.0.1:3000:3000" 复制整个 "invidious service" 配置块,粘贴为一个新的服务,命名为 invidious-refresh。
对,就是复制 "invidious service" 的整个配置块,从 invidious: 到 - invidious-db。 只在 invidious 服务的 INVIDIOUS_CONFIG 参数中加入以下两行: channel_threads: 0 feed_threads: 0 jobs: clear_expired_items: enabled: false refresh_channels: enabled: false refresh_feeds: enabled: false 这样做是为了确保只有一个 Invidious 进程负责刷新用户的订阅。
使用多进程运行该服务可能会引发冲突。 编辑 docker-compose.yml 文件,填入以下内容: version: "3" services: invidious: image: quay.io/invidious/invidious:latest deploy: replicas: 6 说明:deploy.replicas 参数用于指定基于同一 Docker 镜像运行的容器数量。
注意:进程数量可按需调整,例如本示例中设置为 6。
先别急着重启 Invidious! 创建一个名为 `nginx.conf` 的文件,并填入以下内容: user www-data; events { worker_connections 1000; } http { server { listen 3000; listen [::]:3000; access_log off; location / { resolver 127.0.0.11; set $backend "invidious"; proxy_pass http://$backend:3000; proxy_http_version 1.1; # 保持连接 proxy_set_header Connection ""; # 保持连接 } } } 在你的 `docker-compose.yml` 文件中添加一个新的服务: nginx: image: nginx:latest restart: unless-stopped volumes: - ./nginx.conf:/etc/nginx/nginx.conf:ro depends_on: - invidious ports: - "127.0.0.1:3000:3000" 更新你的定时任务以重启 Invidious(如果你使用 cron)。 现在你需要重启 6 个容器,而不是单个 Docker 容器(如果你增减了容器数量,请相应调整)。 使用以下 CRON 行替换原有配置: 0 */1 * * * docker restart invidious-invidious-1 1 */1 * * * docker restart invidious-invidious-2 2 */1 * * * docker restart invidious-invidious-3 3 */1 * * * docker restart invidious-invidious-4 4 */1 * * * docker restart invidious-invidious-5 5 */1 * * * docker restart invidious-invidious-6 每条 CRON 任务设置了不同的调度时间,以避免因单次重启导致整个 Invidious 实例服务中断。 应用新的配置: docker compose down docker compose up 点击此处查看最终的 docker-compose 文件示例。(请勿盲目复制) version: "3" services: invidious: image: quay.io/invidious/invidious:latest deploy: replicas: 6 restart: unless-stopped environment: INVIDIOUS_CONFIG: | channel_threads: 0 feed_threads: 0 db: dbname: invidious user: kemal password: kemal host: invidious-db port: 5432 check_tables: true use_pubsub_feeds: true use_innertube_for_captions: true external_port: 443 domain: mydomain.com https_only: true statistics_enabled: true hmac_key: "CHANGE_ME!!" jobs: clear_expired_items: enabled: false refresh_channels: enabled: false refresh_feeds: enabled: false healthcheck: test: wget -nv --tries=1 --spider http://127.0.0.1:3000/api/v1/stats || exit 1 interval: 30s timeout: 5s retries: 2 logging: options: max-size: "1G" max-file: "4" depends_on: - invidious-db invidious-refresh: image: quay.io/invidious/invidious:latest restart: unless-stopped environment: INVIDIOUS_CONFIG: | db: dbname: invidious user: kemal password: kemal host: invidious-db port: 5432 check_tables: true use_pubsub_feeds: true use_innertube_for_captions: true external_port: 443 domain: mydomain.com https_only: true statistics_enabled: true hmac_key: "CHANGE_ME!!" healthcheck: test: wget -nv --tries=1 --spider http://127.0.0.1:3000/api/v1/stats || exit 1 interval: 30s timeout: 5s retries: 2 logging: options: max-size: "1G" max-file: "4" depends_on: - invidious-db nginx: image: nginx:latest restart: unless-stopped volumes: - ./nginx.conf:/etc/nginx/nginx.conf:ro depends_on: - invidious ports: - "127.0.0.1:3000:3000" invidious-db: image: docker.io/library/postgres:14 restart: unless-stopped volumes: - postgresdata:/var/lib/postgresql/data - ./config/sql:/config/sql - ./docker/init-invidious-db.sh:/docker-entrypoint-initdb.d/init-invidious-db.sh environment: POSTGRES_DB: invidious POSTGRES_USER: kemal POSTGRES_PASSWORD: kemal healthcheck: test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"] volumes: postgresdata: 3) 使用 http3-ytproxy 加速视频播放 来自 Piped 团队的 Kavin 开发了一个工具,其代理视频播放和图片加载流量的速度比 Invidious 快得多:https://github.com/TeamPiped/http3-ytproxy 本教程将使用 NGINX 配置,强烈建议在你的主反向代理上设置此配置。 如果你的主反向代理不是 NGINX,你可以尝试将规则适配到你的反向代理上。或者,你也可以使用第二节中创建的独立 NGINX 容器。 查找 NGINX 进程的 username,你可以在 `/etc/nginx/nginx.conf` 文件的顶部找到它。
使用 id 命令获取其 uid 和 gid,例如:id www-data。
通常是 uid 33 和 gid 33。 为这个新服务创建一个目录,并为 NGINX 用户设置正确的权限: mkdir /opt/http3-ytproxy chown 33:33 -R /opt/http3-ytproxy 如果你的 uid 和 gid 不是 33:33,请替换为你自己的值。 在 docker-compose.yml 中添加这个新服务: http3-ytproxy: image: 1337kavin/ytproxy:latest restart: unless-stopped user: 33:33 network_mode: "host" environment: - DISABLE_WEBP=1 volumes: - /opt/http3-ytproxy:/app/socket 如果你的 uid 和 gid 不是 33:33,请替换为你自己的值。 在 NGINX 配置中,在 Invidious 的 "server" 部分、location / 块的后面,添加以下几行: location ~ (^/videoplayback|^/vi/|^/ggpht/) { proxy_buffering on; proxy_buffers 1024 16k; proxy_set_header X-Forwarded-For ""; proxy_set_header CF-Connecting-IP ""; proxy_hide_header "alt-svc"; sendfile on; sendfile_max_chunk 512k; tcp_nopush on; aio threads=default; aio_write on; directio 16m; proxy_hide_header Cache-Control; proxy_hide_header etag; proxy_http_version 1.1; proxy_set_header Connection keep-alive; proxy_max_temp_file_size 32m; access_log off; proxy_pass http://unix:/opt/http3-ytproxy/http-proxy.sock; add_header Cache-Control private always; } 注意:proxy_buffering on 可能会增加磁盘占用,如果不想这样,可以删除 proxy_buffering 和 proxy_buffers 这两行。
如果你使用的是第二部分中的 NGINX,需要添加这个新的卷: ```yaml volumes: - /opt/http3-ytproxy:/opt/http3-ytproxy ``` 重新加载 docker compose。(如果你遵循了第二部分的步骤) 重新加载 NGINX:`systemctl reload nginx`。 点击此处查看 docker-compose 文件的最终示例。(请勿盲目照抄) ```yaml version: "3" services: invidious: image: quay.io/invidious/invidious:latest deploy: replicas: 6 restart: unless-stopped environment: INVIDIOUS_CONFIG: | channel_threads: 0 feed_threads: 0 db: dbname: invidious user: kemal password: kemal host: invidious-db port: 5432 check_tables: true # external_port: # domain: # https_only: false # statistics_enabled: false hmac_key: "CHANGE_ME!!" healthcheck: test: wget -nv --tries=1 --spider http://127.0.0.1:3000/api/v1/stats || exit 1 interval: 30s timeout: 5s retries: 2 logging: options: max-size: "1G" max-file: "4" depends_on: - invidious-db invidious-refresh: image: quay.io/invidious/invidious:latest restart: unless-stopped environment: INVIDIOUS_CONFIG: | db: dbname: invidious user: kemal password: kemal host: invidious-db port: 5432 check_tables: true # external_port: # domain: # https_only: false # statistics_enabled: false hmac_key: "CHANGE_ME!!" healthcheck: test: wget -nv --tries=1 --spider http://127.0.0.1:3000/api/v1/stats || exit 1 interval: 30s timeout: 5s retries: 2 logging: options: max-size: "1G" max-file: "4" depends_on: - invidious-db nginx: image: nginx:latest restart: unless-stopped volumes: - ./nginx.conf:/etc/nginx/nginx.conf:ro depends_on: - invidious ports: - "127.0.0.1:3000:3000" http3-ytproxy: image: 1337kavin/ytproxy:latest restart: unless-stopped user: 33:33 network_mode: "host" environment: DISABLE_WEBP: 1 volumes: - /opt/http3-ytproxy:/app/socket invidious-db: image: docker.io/library/postgres:14 restart: unless-stopped volumes: - postgresdata:/var/lib/postgresql/data - ./config/sql:/config/sql - ./docker/init-invidious-db.sh:/docker-entrypoint-initdb.d/init-invidious-db.sh environment: POSTGRES_DB: invidious POSTGRES_USER: kemal POSTGRES_PASSWORD: kemal healthcheck: test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"] volumes: postgresdata: ```
用户通知将被禁用。
对于大型实例,建议将此选项设为 false,以减少写入数据库的数据量,从而提升实例的整体性能。 2) 运行多个 Invidious 进程 Invidious 是单线程的,因此运行多个进程可以更好地利用服务器的多核性能。
此外,当请求量达到一定程度时,Invidious 会变得迟缓,多个进程可以缓解这一问题。 本教程将讲解如何运行多个 Invidious 容器,并使用 Docker 中的 NGINX 作为负载均衡器。
如果你的反向代理本身已经在 Docker 中,也可以改造它,让请求直接发送到各个 Invidious 容器。 以下假设你没有修改默认安装的 3000 端口。如果改过端口,则需要在后面的配置文件和命令中相应调整。 在 docker-compose.yml 中,删除以下绑定 Invidious 端口的配置: ports: - "127.0.0.1:3000:3000" 复制整个 "invidious service" 配置块,粘贴为一个新的服务,命名为 invidious-refresh。
对,就是复制 "invidious service" 的整个配置块,从 invidious: 到 - invidious-db。 只在 invidious 服务的 INVIDIOUS_CONFIG 参数中加入以下两行: channel_threads: 0 feed_threads: 0 jobs: clear_expired_items: enabled: false refresh_channels: enabled: false refresh_feeds: enabled: false 这样做是为了确保只有一个 Invidious 进程负责刷新用户的订阅。
使用多进程运行该服务可能会引发冲突。 编辑 docker-compose.yml 文件,填入以下内容: version: "3" services: invidious: image: quay.io/invidious/invidious:latest deploy: replicas: 6 说明:deploy.replicas 参数用于指定基于同一 Docker 镜像运行的容器数量。
注意:进程数量可按需调整,例如本示例中设置为 6。
先别急着重启 Invidious! 创建一个名为 `nginx.conf` 的文件,并填入以下内容: user www-data; events { worker_connections 1000; } http { server { listen 3000; listen [::]:3000; access_log off; location / { resolver 127.0.0.11; set $backend "invidious"; proxy_pass http://$backend:3000; proxy_http_version 1.1; # 保持连接 proxy_set_header Connection ""; # 保持连接 } } } 在你的 `docker-compose.yml` 文件中添加一个新的服务: nginx: image: nginx:latest restart: unless-stopped volumes: - ./nginx.conf:/etc/nginx/nginx.conf:ro depends_on: - invidious ports: - "127.0.0.1:3000:3000" 更新你的定时任务以重启 Invidious(如果你使用 cron)。 现在你需要重启 6 个容器,而不是单个 Docker 容器(如果你增减了容器数量,请相应调整)。 使用以下 CRON 行替换原有配置: 0 */1 * * * docker restart invidious-invidious-1 1 */1 * * * docker restart invidious-invidious-2 2 */1 * * * docker restart invidious-invidious-3 3 */1 * * * docker restart invidious-invidious-4 4 */1 * * * docker restart invidious-invidious-5 5 */1 * * * docker restart invidious-invidious-6 每条 CRON 任务设置了不同的调度时间,以避免因单次重启导致整个 Invidious 实例服务中断。 应用新的配置: docker compose down docker compose up 点击此处查看最终的 docker-compose 文件示例。(请勿盲目复制) version: "3" services: invidious: image: quay.io/invidious/invidious:latest deploy: replicas: 6 restart: unless-stopped environment: INVIDIOUS_CONFIG: | channel_threads: 0 feed_threads: 0 db: dbname: invidious user: kemal password: kemal host: invidious-db port: 5432 check_tables: true use_pubsub_feeds: true use_innertube_for_captions: true external_port: 443 domain: mydomain.com https_only: true statistics_enabled: true hmac_key: "CHANGE_ME!!" jobs: clear_expired_items: enabled: false refresh_channels: enabled: false refresh_feeds: enabled: false healthcheck: test: wget -nv --tries=1 --spider http://127.0.0.1:3000/api/v1/stats || exit 1 interval: 30s timeout: 5s retries: 2 logging: options: max-size: "1G" max-file: "4" depends_on: - invidious-db invidious-refresh: image: quay.io/invidious/invidious:latest restart: unless-stopped environment: INVIDIOUS_CONFIG: | db: dbname: invidious user: kemal password: kemal host: invidious-db port: 5432 check_tables: true use_pubsub_feeds: true use_innertube_for_captions: true external_port: 443 domain: mydomain.com https_only: true statistics_enabled: true hmac_key: "CHANGE_ME!!" healthcheck: test: wget -nv --tries=1 --spider http://127.0.0.1:3000/api/v1/stats || exit 1 interval: 30s timeout: 5s retries: 2 logging: options: max-size: "1G" max-file: "4" depends_on: - invidious-db nginx: image: nginx:latest restart: unless-stopped volumes: - ./nginx.conf:/etc/nginx/nginx.conf:ro depends_on: - invidious ports: - "127.0.0.1:3000:3000" invidious-db: image: docker.io/library/postgres:14 restart: unless-stopped volumes: - postgresdata:/var/lib/postgresql/data - ./config/sql:/config/sql - ./docker/init-invidious-db.sh:/docker-entrypoint-initdb.d/init-invidious-db.sh environment: POSTGRES_DB: invidious POSTGRES_USER: kemal POSTGRES_PASSWORD: kemal healthcheck: test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"] volumes: postgresdata: 3) 使用 http3-ytproxy 加速视频播放 来自 Piped 团队的 Kavin 开发了一个工具,其代理视频播放和图片加载流量的速度比 Invidious 快得多:https://github.com/TeamPiped/http3-ytproxy 本教程将使用 NGINX 配置,强烈建议在你的主反向代理上设置此配置。 如果你的主反向代理不是 NGINX,你可以尝试将规则适配到你的反向代理上。或者,你也可以使用第二节中创建的独立 NGINX 容器。 查找 NGINX 进程的 username,你可以在 `/etc/nginx/nginx.conf` 文件的顶部找到它。
使用 id 命令获取其 uid 和 gid,例如:id www-data。
通常是 uid 33 和 gid 33。 为这个新服务创建一个目录,并为 NGINX 用户设置正确的权限: mkdir /opt/http3-ytproxy chown 33:33 -R /opt/http3-ytproxy 如果你的 uid 和 gid 不是 33:33,请替换为你自己的值。 在 docker-compose.yml 中添加这个新服务: http3-ytproxy: image: 1337kavin/ytproxy:latest restart: unless-stopped user: 33:33 network_mode: "host" environment: - DISABLE_WEBP=1 volumes: - /opt/http3-ytproxy:/app/socket 如果你的 uid 和 gid 不是 33:33,请替换为你自己的值。 在 NGINX 配置中,在 Invidious 的 "server" 部分、location / 块的后面,添加以下几行: location ~ (^/videoplayback|^/vi/|^/ggpht/) { proxy_buffering on; proxy_buffers 1024 16k; proxy_set_header X-Forwarded-For ""; proxy_set_header CF-Connecting-IP ""; proxy_hide_header "alt-svc"; sendfile on; sendfile_max_chunk 512k; tcp_nopush on; aio threads=default; aio_write on; directio 16m; proxy_hide_header Cache-Control; proxy_hide_header etag; proxy_http_version 1.1; proxy_set_header Connection keep-alive; proxy_max_temp_file_size 32m; access_log off; proxy_pass http://unix:/opt/http3-ytproxy/http-proxy.sock; add_header Cache-Control private always; } 注意:proxy_buffering on 可能会增加磁盘占用,如果不想这样,可以删除 proxy_buffering 和 proxy_buffers 这两行。
如果你使用的是第二部分中的 NGINX,需要添加这个新的卷: ```yaml volumes: - /opt/http3-ytproxy:/opt/http3-ytproxy ``` 重新加载 docker compose。(如果你遵循了第二部分的步骤) 重新加载 NGINX:`systemctl reload nginx`。 点击此处查看 docker-compose 文件的最终示例。(请勿盲目照抄) ```yaml version: "3" services: invidious: image: quay.io/invidious/invidious:latest deploy: replicas: 6 restart: unless-stopped environment: INVIDIOUS_CONFIG: | channel_threads: 0 feed_threads: 0 db: dbname: invidious user: kemal password: kemal host: invidious-db port: 5432 check_tables: true # external_port: # domain: # https_only: false # statistics_enabled: false hmac_key: "CHANGE_ME!!" healthcheck: test: wget -nv --tries=1 --spider http://127.0.0.1:3000/api/v1/stats || exit 1 interval: 30s timeout: 5s retries: 2 logging: options: max-size: "1G" max-file: "4" depends_on: - invidious-db invidious-refresh: image: quay.io/invidious/invidious:latest restart: unless-stopped environment: INVIDIOUS_CONFIG: | db: dbname: invidious user: kemal password: kemal host: invidious-db port: 5432 check_tables: true # external_port: # domain: # https_only: false # statistics_enabled: false hmac_key: "CHANGE_ME!!" healthcheck: test: wget -nv --tries=1 --spider http://127.0.0.1:3000/api/v1/stats || exit 1 interval: 30s timeout: 5s retries: 2 logging: options: max-size: "1G" max-file: "4" depends_on: - invidious-db nginx: image: nginx:latest restart: unless-stopped volumes: - ./nginx.conf:/etc/nginx/nginx.conf:ro depends_on: - invidious ports: - "127.0.0.1:3000:3000" http3-ytproxy: image: 1337kavin/ytproxy:latest restart: unless-stopped user: 33:33 network_mode: "host" environment: DISABLE_WEBP: 1 volumes: - /opt/http3-ytproxy:/app/socket invidious-db: image: docker.io/library/postgres:14 restart: unless-stopped volumes: - postgresdata:/var/lib/postgresql/data - ./config/sql:/config/sql - ./docker/init-invidious-db.sh:/docker-entrypoint-initdb.d/init-invidious-db.sh environment: POSTGRES_DB: invidious POSTGRES_USER: kemal POSTGRES_PASSWORD: kemal healthcheck: test: ["CMD-SHELL", "pg_isready -U $$POSTGRES_USER -d $$POSTGRES_DB"] volumes: postgresdata: ```