0
0
Fork 0
mirror of https://github.com/NixOS/nixpkgs.git synced 2025-07-12 05:16:25 +03:00

nixos/peertube: update nginx configuration

This commit is contained in:
Izorkin 2023-12-12 17:47:47 +03:00
parent b0b1f73473
commit 937220442c
No known key found for this signature in database
GPG key ID: 1436C1B3F3679F09

View file

@ -61,11 +61,13 @@ let
eval -- "\$@"
'';
nginxCommonHeaders = lib.optionalString cfg.enableWebHttps ''
add_header Strict-Transport-Security 'max-age=63072000; includeSubDomains';
'' + lib.optionalString config.services.nginx.virtualHosts.${cfg.localDomain}.http3 ''
add_header Alt-Svc 'h3=":443"; ma=86400';
'' + ''
nginxCommonHeaders = lib.optionalString config.services.nginx.virtualHosts.${cfg.localDomain}.forceSSL ''
add_header Strict-Transport-Security 'max-age=31536000';
'' + lib.optionalString (config.services.nginx.virtualHosts.${cfg.localDomain}.quic && config.services.nginx.virtualHosts.${cfg.localDomain}.http3) ''
add_header Alt-Svc 'h3=":$server_port"; ma=604800';
'';
nginxCommonHeadersExtra = ''
add_header Access-Control-Allow-Origin '*';
add_header Access-Control-Allow-Methods 'GET, OPTIONS';
add_header Access-Control-Allow-Headers 'Range,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
@ -487,6 +489,9 @@ in {
services.nginx = lib.mkIf cfg.configureNginx {
enable = true;
upstreams."peertube".servers = {
"127.0.0.1:${toString cfg.listenHttp}".fail_timeout = "0";
};
virtualHosts."${cfg.localDomain}" = {
root = "/var/lib/peertube/www";
@ -496,14 +501,14 @@ in {
priority = 1110;
};
locations."= /api/v1/videos/upload-resumable" = {
locations."~ ^/api/v1/videos/(upload-resumable|([^/]+/source/replace-resumable))$" = {
tryFiles = "/dev/null @api";
priority = 1120;
extraConfig = ''
client_max_body_size 0;
proxy_request_buffering off;
'';
'' + nginxCommonHeaders;
};
locations."~ ^/api/v1/videos/(upload|([^/]+/studio/edit))$" = {
@ -512,13 +517,11 @@ in {
priority = 1130;
extraConfig = ''
limit_except POST HEAD { deny all; }
client_max_body_size 12G;
add_header X-File-Maximum-Size 8G always;
'' + lib.optionalString cfg.enableWebHttps ''
add_header Strict-Transport-Security 'max-age=63072000; includeSubDomains';
'' + lib.optionalString config.services.nginx.virtualHosts.${cfg.localDomain}.http3 ''
add_header Alt-Svc 'h3=":443"; ma=86400';
'';
'' + nginxCommonHeaders;
};
locations."~ ^/api/v1/runners/jobs/[^/]+/(update|success)$" = {
@ -529,11 +532,7 @@ in {
extraConfig = ''
client_max_body_size 12G;
add_header X-File-Maximum-Size 8G always;
'' + lib.optionalString cfg.enableWebHttps ''
add_header Strict-Transport-Security 'max-age=63072000; includeSubDomains';
'' + lib.optionalString config.services.nginx.virtualHosts.${cfg.localDomain}.http3 ''
add_header Alt-Svc 'h3=":443"; ma=86400';
'';
'' + nginxCommonHeaders;
};
locations."~ ^/api/v1/(videos|video-playlists|video-channels|users/me)" = {
@ -543,21 +542,17 @@ in {
extraConfig = ''
client_max_body_size 6M;
add_header X-File-Maximum-Size 4M always;
'' + lib.optionalString cfg.enableWebHttps ''
add_header Strict-Transport-Security 'max-age=63072000; includeSubDomains';
'' + lib.optionalString config.services.nginx.virtualHosts.${cfg.localDomain}.http3 ''
add_header Alt-Svc 'h3=":443"; ma=86400';
'';
'' + nginxCommonHeaders;
};
locations."@api" = {
proxyPass = "http://127.0.0.1:${toString cfg.listenHttp}";
proxyPass = "http://peertube";
priority = 1150;
extraConfig = ''
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
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_connect_timeout 10m;
@ -566,7 +561,7 @@ in {
client_max_body_size 100k;
send_timeout 10m;
'';
''+ nginxCommonHeaders;
};
# Websocket
@ -590,24 +585,26 @@ in {
};
locations."@api_websocket" = {
proxyPass = "http://127.0.0.1:${toString cfg.listenHttp}";
proxyPass = "http://peertube";
priority = 1240;
extraConfig = ''
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
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_http_version 1.1;
'';
'' + nginxCommonHeaders;
};
# Bypass PeerTube for performance reasons.
locations."~ ^/client/(assets/images/(icons/icon-36x36\.png|icons/icon-48x48\.png|icons/icon-72x72\.png|icons/icon-96x96\.png|icons/icon-144x144\.png|icons/icon-192x192\.png|icons/icon-512x512\.png|logo\.svg|favicon\.png|default-playlist\.jpg|default-avatar-account\.png|default-avatar-account-48x48\.png|default-avatar-video-channel\.png|default-avatar-video-channel-48x48\.png))$" = {
tryFiles = "/client-overrides/$1 /client/$1 $1";
priority = 1310;
extraConfig = nginxCommonHeaders;
};
locations."~ ^/client/(.*\.(js|css|png|svg|woff2|otf|ttf|woff|eot))$" = {
@ -615,59 +612,55 @@ in {
priority = 1320;
extraConfig = ''
add_header Cache-Control 'public, max-age=604800, immutable';
'' + lib.optionalString cfg.enableWebHttps ''
add_header Strict-Transport-Security 'max-age=63072000; includeSubDomains';
'' + lib.optionalString config.services.nginx.virtualHosts.${cfg.localDomain}.http3 ''
add_header Alt-Svc 'h3=":443"; ma=86400';
'';
'' + nginxCommonHeaders;
};
locations."^~ /download/" = {
proxyPass = "http://127.0.0.1:${toString cfg.listenHttp}";
proxyPass = "http://peertube";
priority = 1410;
extraConfig = ''
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
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_limit_rate 5M;
'';
'' + nginxCommonHeaders;
};
locations."^~ /static/streaming-playlists/private/" = {
proxyPass = "http://127.0.0.1:${toString cfg.listenHttp}";
locations."^~ /static/streaming-playlists/hls/private/" = {
proxyPass = "http://peertube";
priority = 1420;
extraConfig = ''
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
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_limit_rate 5M;
'';
'' + nginxCommonHeaders;
};
locations."^~ /static/web-videos/private/" = {
proxyPass = "http://127.0.0.1:${toString cfg.listenHttp}";
proxyPass = "http://peertube";
priority = 1430;
extraConfig = ''
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
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_limit_rate 5M;
'';
'' + nginxCommonHeaders;
};
locations."^~ /static/webseed/private/" = {
proxyPass = "http://127.0.0.1:${toString cfg.listenHttp}";
proxyPass = "http://peertube";
priority = 1440;
extraConfig = ''
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
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_limit_rate 5M;
'';
'' + nginxCommonHeaders;
};
locations."^~ /static/redundancy/" = {
@ -683,6 +676,7 @@ in {
if ($request_method = 'OPTIONS') {
${nginxCommonHeaders}
${nginxCommonHeadersExtra}
add_header Access-Control-Max-Age 1728000;
add_header Content-Type 'text/plain charset=UTF-8';
add_header Content-Length 0;
@ -690,6 +684,7 @@ in {
}
if ($request_method = 'GET') {
${nginxCommonHeaders}
${nginxCommonHeadersExtra}
access_log off;
}
@ -718,6 +713,7 @@ in {
if ($request_method = 'OPTIONS') {
${nginxCommonHeaders}
${nginxCommonHeadersExtra}
add_header Access-Control-Max-Age 1728000;
add_header Content-Type 'text/plain charset=UTF-8';
add_header Content-Length 0;
@ -725,6 +721,7 @@ in {
}
if ($request_method = 'GET') {
${nginxCommonHeaders}
${nginxCommonHeadersExtra}
access_log off;
}
@ -742,7 +739,7 @@ in {
locations."^~ /static/web-videos/" = {
tryFiles = "$uri @api";
root = cfg.settings.storage.streaming_playlists;
root = cfg.settings.storage.web_videos;
priority = 1470;
extraConfig = ''
set $peertube_limit_rate 800k;
@ -753,6 +750,7 @@ in {
if ($request_method = 'OPTIONS') {
${nginxCommonHeaders}
${nginxCommonHeadersExtra}
add_header Access-Control-Max-Age 1728000;
add_header Content-Type 'text/plain charset=UTF-8';
add_header Content-Length 0;
@ -760,6 +758,7 @@ in {
}
if ($request_method = 'GET') {
${nginxCommonHeaders}
${nginxCommonHeadersExtra}
access_log off;
}
@ -771,7 +770,7 @@ in {
limit_rate $peertube_limit_rate;
limit_rate_after 5M;
rewrite ^/static/streaming-playlists/(.*)$ /$1 break;
rewrite ^/static/web-videos/(.*)$ /$1 break;
'';
};
@ -788,6 +787,7 @@ in {
if ($request_method = 'OPTIONS') {
${nginxCommonHeaders}
${nginxCommonHeadersExtra}
add_header Access-Control-Max-Age 1728000;
add_header Content-Type 'text/plain charset=UTF-8';
add_header Content-Length 0;
@ -795,6 +795,7 @@ in {
}
if ($request_method = 'GET') {
${nginxCommonHeaders}
${nginxCommonHeadersExtra}
access_log off;
}
@ -809,10 +810,6 @@ in {
rewrite ^/static/webseed/(.*)$ /web-videos/$1 break;
'';
};
extraConfig = lib.optionalString cfg.enableWebHttps ''
add_header Strict-Transport-Security 'max-age=63072000; includeSubDomains';
'';
};
};