fix(youko): reuse upstream TLS connections in the nix cache proxy

Assisted-by: DeepSeek Harness (claude-opus-5-5)
This commit is contained in:
Ryan Yin committed 2026-11-17 23:25:56 +08:00
1 parent 6ca74dcd86
commit 480c486ea2
3 files changed
+34 -6

No files matched your search

@@ -1,4 +1,4 @@
{ myvars, ... }:
{ lib, myvars, ... }:
let
cacheDir = "/data/apps/nix-cache-proxy";
inherit (myvars.ports) youkoNixCacheProxy;
@@ -25,10 +25,23 @@ let
add_header X-Cache-Status $upstream_cache_status always;
'';
upstreamHosts = [
"cache.nixos.org"
"cache.numtide.com"
"ryan4yin.cachix.org"
"mirrors.ustc.edu.cn"
];
upstreamName = host: builtins.replaceStrings [ "." ] [ "_" ] host;
# Reuse upstream TLS connections: Nix fetches hundreds of narinfos at once,
# and a fresh handshake per request gets reset under that burst.
cacheLocation = upstream: subPath: {
proxyPass = "https://${upstream}${subPath}/";
proxyPass = "https://${upstreamName upstream}${subPath}/";
extraConfig = ''
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_set_header Host ${upstream};
proxy_ssl_name ${upstream};
${cacheConfig}
'';
};
@@ -46,6 +59,14 @@ in
use_temp_path=off;
'';
upstreams = lib.genAttrs' upstreamHosts (
host:
lib.nameValuePair (upstreamName host) {
servers."${host}:443" = { };
extraConfig = "keepalive 16;";
}
);
virtualHosts."nix-cache-proxy" = {
# Caddy owns the public listener and terminates HTTPS; nginx is internal.
listen = [
@@ -8,8 +8,14 @@
cachixProxyConfigured = true;
directFallbacks = true;
cacheLivesOnDataVolume = true;
nixosUpstream = "https://cache.nixos.org/";
numtideUpstream = "https://cache.numtide.com/";
cachixUpstream = "https://ryan4yin.cachix.org/";
ustcUpstream = "https://mirrors.ustc.edu.cn/nix-channels/store/";
nixosUpstream = "https://cache_nixos_org/";
numtideUpstream = "https://cache_numtide_com/";
cachixUpstream = "https://ryan4yin_cachix_org/";
ustcUpstream = "https://mirrors_ustc_edu_cn/nix-channels/store/";
upstreamServers = {
cache_nixos_org = [ "cache.nixos.org:443" ];
cache_numtide_com = [ "cache.numtide.com:443" ];
mirrors_ustc_edu_cn = [ "mirrors.ustc.edu.cn:443" ];
ryan4yin_cachix_org = [ "ryan4yin.cachix.org:443" ];
};
}
@@ -26,4 +26,5 @@ in
cachixUpstream = nginx.virtualHosts."nix-cache-proxy".locations."/ryan4yin.cachix.org/".proxyPass;
ustcUpstream =
nginx.virtualHosts."nix-cache-proxy".locations."/mirrors.ustc.edu.cn/nix-channels/store/".proxyPass;
upstreamServers = lib.mapAttrs (_: upstream: builtins.attrNames upstream.servers) nginx.upstreams;
}