How to serve Tandoor Recipes from a sub-path?

I’m running into issues setting up a machine to serve Tandoor Recipes from a sub-path (same as all my other services, which is why I’d rather not change it to a subdomain or root path):

  • I tried following the official instructions for sub-path nginx configuration, but just ended up with a HTTP 400 error and no debugging output at all in journalctl -f. The relevant configuration:

    extraConfig = ''
      proxy_set_header Host $host;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Script-Name ${config.services.tandoor-recipes.extraConfig.SCRIPT_NAME};
      proxy_cookie_path / ${config.services.tandoor-recipes.extraConfig.SCRIPT_NAME};
    '';
    
  • Even if I specify SCRIPT_NAME the static content is still served from /static, not from ${config.services.tandoor-recipes.extraConfig.SCRIPT_NAME}/static/. This seems like a bug, but since this seems to be how the upstream package works, I guess I have to work around it.

  • If I also set STATIC_URL = "${config.services.tandoor-recipes.extraConfig.SCRIPT_NAME}/static/" it fixes the URLs to static content, but the server seems to reject them, returning HTTP 404 for all of them.

  • Setting DEBUG = "1" breaks because of a missing Python dependency.

  • I had a look at several similar issues (like this one), and couldn’t work out what was wrong based on those.

The relevant parts of /etc/nixos/configuration.nix:

{
  services = {
    nginx = {
      enable = true;
      recommendedGzipSettings = true;
      recommendedOptimisation = true;
      recommendedProxySettings = true;
      recommendedTlsSettings = true;
      virtualHosts."${config.networking.fqdn}" = {
        forceSSL = true;
        locations = {
          "${config.services.tandoor-recipes.extraConfig.SCRIPT_NAME}/" = {
            proxyPass = "http://${toString config.services.tandoor-recipes.address}:${toString config.services.tandoor-recipes.port}";
          };
        };
        useACMEHost = config.networking.domain;
      };
    };
    tandoor-recipes = {
      enable = true;
      extraConfig = {
        SCRIPT_NAME = "/tandoor";
        STATIC_URL = "${config.services.tandoor-recipes.extraConfig.SCRIPT_NAME}/static/";
        TIMEZONE = "Pacific/Auckland";
      };
    };
  };
}

Working on enabling this slowly in nixos/tandoor-recipes: Test setup of script name setting by l0b0 · Pull Request #262892 · NixOS/nixpkgs · GitHub and tandoor-recipes: Add runtime dependencies by l0b0 · Pull Request #287978 · NixOS/nixpkgs · GitHub.