about summary refs log tree commit diff
path: root/nixos/modules/services/web-apps/lemmy.nix
diff options
context:
space:
mode:
authorColin <colin@uninsane.org>2023-05-17 20:20:13 +0000
committerYt <happysalada@tuta.io>2023-06-12 09:59:11 -0400
commitbd77d4ae46a32fda5d28c02b0f2cb33c7f722c8e (patch)
treea7467690ed668257cb2f49e0bca8e9eda111c04e /nixos/modules/services/web-apps/lemmy.nix
parent328bcf4d57f14f35f2e55d6ec3283c0bc8fd6892 (diff)
downloadnixlib-bd77d4ae46a32fda5d28c02b0f2cb33c7f722c8e.tar
nixlib-bd77d4ae46a32fda5d28c02b0f2cb33c7f722c8e.tar.gz
nixlib-bd77d4ae46a32fda5d28c02b0f2cb33c7f722c8e.tar.bz2
nixlib-bd77d4ae46a32fda5d28c02b0f2cb33c7f722c8e.tar.lz
nixlib-bd77d4ae46a32fda5d28c02b0f2cb33c7f722c8e.tar.xz
nixlib-bd77d4ae46a32fda5d28c02b0f2cb33c7f722c8e.tar.zst
nixlib-bd77d4ae46a32fda5d28c02b0f2cb33c7f722c8e.zip
nixos/lemmy: support nginx
Diffstat (limited to 'nixos/modules/services/web-apps/lemmy.nix')
-rw-r--r--nixos/modules/services/web-apps/lemmy.nix36
1 files changed, 36 insertions, 0 deletions
diff --git a/nixos/modules/services/web-apps/lemmy.nix b/nixos/modules/services/web-apps/lemmy.nix
index f48afd98a6c9..97be4a96f219 100644
--- a/nixos/modules/services/web-apps/lemmy.nix
+++ b/nixos/modules/services/web-apps/lemmy.nix
@@ -25,6 +25,7 @@ in
     };
 
     caddy.enable = mkEnableOption (lib.mdDoc "exposing lemmy with the caddy reverse proxy");
+    nginx.enable = mkEnableOption (lib.mdDoc "exposing lemmy with the nginx reverse proxy");
 
     database.createLocally = mkEnableOption (lib.mdDoc "creation of database on the instance");
 
@@ -140,6 +141,41 @@ in
         };
       };
 
+      services.nginx = mkIf cfg.nginx.enable {
+        enable = mkDefault true;
+        virtualHosts."${cfg.settings.hostname}".locations = let
+          ui = "http://127.0.0.1:${toString cfg.ui.port}";
+          backend = "http://127.0.0.1:${toString cfg.settings.port}";
+        in {
+          "~ ^/(api|pictrs|feeds|nodeinfo|.well-known)" = {
+            # backend requests
+            proxyPass = backend;
+            proxyWebsockets = true;
+            recommendedProxySettings = true;
+          };
+          "/" = {
+            # mixed frontend and backend requests, based on the request headers
+            proxyPass = "$proxpass";
+            recommendedProxySettings = true;
+            extraConfig = ''
+              set $proxpass "${ui}";
+              if ($http_accept = "application/activity+json") {
+                set $proxpass "${backend}";
+              }
+              if ($http_accept = "application/ld+json; profile=\"https://www.w3.org/ns/activitystreams\"") {
+                set $proxpass "${backend}";
+              }
+              if ($request_method = POST) {
+                set $proxpass "${backend}";
+              }
+
+              # Cuts off the trailing slash on URLs to make them valid
+              rewrite ^(.+)/+$ $1 permanent;
+            '';
+          };
+        };
+      };
+
       assertions = [{
         assertion = cfg.database.createLocally -> cfg.settings.database.host == "localhost" || cfg.settings.database.host == "/run/postgresql";
         message = "if you want to create the database locally, you need to use a local database";