在群晖中自定义Jellyfin的Nginx反向代理规则
背景
群晖DSM系统自带的反向代理只提供简单的代理规则,不能对Nginx的配置做更多定制化处理。
导致用DSM Nginx反向Jellyfin时,没办法使用syncplay(多人同步播放的功能)
顺便吐槽一下DSM反向代理的位置不好找。
踩坑
进入群晖的命令行,使用 nginx -T
命令,发现配置反向代理文件的位置在:
/etc/nginx/sites-enabled/server.ReverseProxy.conf
满怀期待修改完配置文件,发现实际的配置是用 /usr/local/etc/nginx/conf.d/
里面的模板生成。每次重载Nginx,/etc/nginx/sites-enabled
里的修改酒会被覆盖掉。
https://www.simaek.com/archives/298/
实战
琢磨再三,决定直接在 /etc/nginx/sites-enabled/jellyfin.conf
创建反向代理文件,绕过群晖DSM提供的反向代理。SSL规则就抄一下群晖原有的。Jellyfin的Nginx反向代理配置在官方文档里有,结合一下。
mkdir /var/cache/nginx/jellyfin-videos
vim /etc/nginx/sites-enabled/jellyfin.conf
# configuration file /etc/nginx/sites-enabled/jellyfin.conf:
proxy_cache_path /var/cache/nginx/jellyfin-videos levels=1:2 keys_zone=jellyfin-videos:1024m inactive=90d max_size=35000m;
map $request_uri $h264Level { ~(h264-level=)(.+?)& $2; }
map $request_uri $h264Profile { ~(h264-profile=)(.+?)& $2; }
server {
listen 端口 ssl;
listen [::]:端口 ssl;
server_name 域名 ;
if ( $host !~ "(^域名$)" ) { return 404; }
include /usr/syno/etc/www/certificate/ReverseProxy_9ccxxxxx09/cert.conf*;
include /usr/syno/etc/security-profile/tls-profile/config/ReverseProxy_9cc38b30xxx09.conf*; add_header Strict-Transport-Security "max-age=15768000; includeSubdomains; preload" always;
proxy_ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3;
# Set in Server block
location ~* ^/Videos/(.*)/(?!live)
{
# Set size of a slice (this amount will be always requested from the backend by nginx)
# Higher value means more latency, lower more overhead
# This size is independent of the size clients/browsers can request
slice 2m;
proxy_cache jellyfin-videos;
proxy_cache_valid 200 206 301 302 30d;
proxy_ignore_headers Expires Cache-Control Set-Cookie X-Accel-Expires;
proxy_cache_use_stale error timeout invalid_header updating http_500 http_502 http_503 http_504;
proxy_connect_timeout 15s;
proxy_http_version 1.1;
proxy_set_header Connection "";
# Transmit slice range to the backend
proxy_set_header Range $slice_range;
# This saves bandwidth between the proxy and jellyfin, as a file is only downloaded one time instead of multiple times when multiple clients want to at the same time
# The first client will trigger the download, the other clients will have to wait until the slice is cached
# Esp. practical during SyncPlay
proxy_cache_lock on;
proxy_cache_lock_age 60s;
proxy_pass http://ip:port;
proxy_cache_key "jellyvideo$uri?MediaSourceId=$arg_MediaSourceId&VideoCodec=$arg_VideoCodec&AudioCodec=$arg_AudioCodec&AudioStreamIndex=$arg_AudioStreamIndex&VideoBitrate=$arg_VideoBitrate&AudioBitrate=$arg_AudioBitrate&SubtitleMethod=$arg_SubtitleMethod&TranscodingMaxAudioChannels=$arg_TranscodingMaxAudioChannels&RequireAvc=$arg_RequireAvc&SegmentContainer=$arg_SegmentContainer&MinSegments=$arg_MinSegments&BreakOnNonKeyFrames=$arg_BreakOnNonKeyFrames&h264-profile=$h264Profile&h264-level=$h264Level&slicerange=$slice_range";
# add_header X-Cache-Status $upstream_cache_status; # This is only for debugging cache
}
location / {
proxy_connect_timeout 60;
proxy_read_timeout 60;
proxy_send_timeout 60;
proxy_intercept_errors off;
proxy_http_version 1.1;
proxy_set_header Host $http_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_pass http://ip:port;
proxy_set_header X-Forwarded-Host $http_host;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $http_connection;
}
error_page 403 404 500 502 503 504 /dsm_error_page;
location /dsm_error_page {
internal;
root /usr/syno/share/nginx;
rewrite (.*) /error.html break;
allow all;
}
location /socket {
# Proxy Jellyfin Websockets traffic
proxy_pass http://ip:port;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $http_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-Protocol $scheme;
proxy_set_header X-Forwarded-Host $http_host;
}
}