Caddy: Difference between revisions

From NixOS Wiki
imported>Onny
No edit summary
imported>Onny
mNo edit summary
Line 22: Line 22:
};
};
</nowiki>}}
</nowiki>}}
== Configuration examples ==
== Configuration examples ==


Line 46: Line 45:
};   
};   
</nowiki>}}
</nowiki>}}
=== Reverse proxy ===
=== Reverse proxy ===


Line 59: Line 57:
};   
};   
</nowiki>}}
</nowiki>}}
=== Redirect ===
=== Redirect ===


Line 72: Line 69:
};
};
</syntaxhighlight>
</syntaxhighlight>
== See also ==
== See also ==
* [https://caddyserver.com/docs/ Official Caddy documentation]
* [https://caddyserver.com/docs/ Official Caddy documentation]

Revision as of 16:52, 6 August 2022

Caddy is a HTTP/2 capable web server with automatic HTTPS.

Installation

The example snippet below will run Caddy on http://localhost and serving an example.html page.

/etc/nixos/configuration.nix
services.caddy = {
  enable = true;
  extraConfig = ''
    http://localhost {
      encode gzip
      file_server
      root * ${
        pkgs.runCommand "testdir" {} ''
          mkdir "$out"
          echo hello world &gt; "$out/example.html"
        ''
      }
    }
  '';
};

Configuration examples

SSL

Caddy will automatically try to acquire SSL certificates for the specified domain, in this example example.org. This requires you to configure the DNS records of your domain correctly, which should point to the address of your Caddy server. The firewall ports 80 and 443 needs to be opened.

/etc/nixos/configuration.nix
caddy = {
  enable = true;
  virtualHosts."example.org".extraConfig = ''
    http://localhost {
      encode gzip
      file_server
      root * ${
        pkgs.runCommand "testdir" {} ''
          mkdir "$out"
          echo hello world &gt; "$out/example.html"
        ''
      }
    }
  '';
};

Reverse proxy

The following snippet creates a reverse proxy for the domain example.org, redirecting all requests to http://10.25.40.6

/etc/nixos/configuration.nix
caddy = {
  enable = true;
  virtualHosts."example.org".extraConfig = ''
    reverse_proxy http://10.25.40.6
  '';
};

Redirect

Redirecting example.org and old.example.org to www.example.org

caddy.virtualHosts."example.org" = {
  extraConfig = ''
    redir https://www.example.org
  '';
  serverAlias = [ "old.example.org" ];
};

See also