Caddy: Difference between revisions

From NixOS Wiki
imported>Onny
mNo edit summary
imported>Onny
mNo edit summary
Line 20: Line 20:
=== Redirect ===
=== Redirect ===


Redirecting <code>example.org</code> to <code>www.example.org</code>
Redirecting <code>example.org</code> and <code>old.example.org</code> to <code>www.example.org</code>
<syntaxhighlight lang="nix>
<syntaxhighlight lang="nix>
caddy.virtualHosts."example.org".extraConfig = ''
caddy.virtualHosts."example.org" = {
  redir https://www.example.org
  extraConfig = ''
'';
    redir https://www.example.org
  '';
  serverAlias = [ "old.example.org" ];
};
</syntaxhighlight>
</syntaxhighlight>
== See also ==
== See also ==
* [https://caddyserver.com/docs/ Official Caddy documentation]
* [https://caddyserver.com/docs/ Official Caddy documentation]
[[Category:Applications]]
[[Category:Applications]]
[[Category:Web Servers]]
[[Category:Web Servers]]

Revision as of 14:40, 31 July 2022

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

Installation

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
  '';
};

Caddy will automatically try to acquire SSL certificates for the domain. 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.

Configuration examples

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