From cb4c29f1d70b0463e9af7808c83064ac3164c6c1 Mon Sep 17 00:00:00 2001 From: Moritz 'e1mo' Fromm Date: Thu, 13 Apr 2023 13:47:05 +0200 Subject: [PATCH] Deliver images in alternative formats If the browser supports webp/avif images, nginx checks if any file with the same name but the other formats extension is available. --- services/website.nix | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) diff --git a/services/website.nix b/services/website.nix index 9c69cc7..16c8a69 100644 --- a/services/website.nix +++ b/services/website.nix @@ -23,14 +23,33 @@ in { recommendedProxySettings = true; recommendedTlsSettings = true; + commonHttpConfig = '' + map $http_accept $webp_suffix { + default ""; + "~*webp" ".webp"; + } + map $http_accept $avif_suffix { + default ""; + "~*avif" ".avif"; + } + ''; + virtualHosts.${baseDomain} = { default = true; enableACME = true; forceSSL = true; serverAliases = [ "www.${baseDomain}" ]; - # TODO: Change this to be deployed by some sort of CI + rsync so we don't need to always update the package version - locations."/".root = webroot; - locations."/.well-known/matrix/".alias = matrixWellKnownDir + "/"; + root = webroot; + locations = { + "~* ^(/images/.+)\\.(png|jpe?g)$".extraConfig = '' + set $base $1; + add_header Vary Accept; + expires 7d; + add_header Cache-Control "must-revalidate, s-maxage=86400"; + try_files $request_uri$avif_suffix $base$avif_suffix $request_uri$webp_suffix $base$webp_suffix $request_uri =404; + ''; + "/.well-known/matrix/".alias = matrixWellKnownDir + "/"; + }; }; };