Caddy: Difference between revisions

imported>Onny
mNo edit summary
imported>Onny
(Add config examples for serving static files and SSL config)
Line 3: Line 3:
== Installation ==
== Installation ==


The following snippet creates a reverse proxy for the domain <code>example.org</code>, redirecting all requests to <code><nowiki>http://10.25.40.6</nowiki></code>.
The example snippet below will run Caddy on http://localhost and serving an [http://localhost/example.html example.html] file.
 
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
services.caddy = {
  enable = true;
  extraConfig = ''
    http://localhost {
      encode gzip
      file_server
      root * ${
        pkgs.runCommand "testdir" {} ''
          mkdir "$out"
          echo hello world > "$out/example.html"
        ''
      }
    }
  '';
};
</nowiki>}}
 
== Configuration examples ==
 
=== SSL ===
 
Caddy will automatically try to acquire SSL certificates for the specified domain, in this example <code>example.org</code>. 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 <code>80</code> and <code>443</code> needs to be opened.


{{file|/etc/nixos/configuration.nix|nix|<nowiki>
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
Line 9: Line 33:
   enable = true;
   enable = true;
   virtualHosts."example.org".extraConfig = ''
   virtualHosts."example.org".extraConfig = ''
     reverse_proxy http://10.25.40.6
     http://localhost {
      encode gzip
      file_server
      root * ${
        pkgs.runCommand "testdir" {} ''
          mkdir "$out"
          echo hello world > "$out/example.html"
        ''
      }
    }
   '';
   '';
};   
};   
</nowiki>}}
</nowiki>}}


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 <code>80</code> and <code>443</code> needs to be opened.
=== Reverse proxy ===


== Configuration examples ==
The following snippet creates a reverse proxy for the domain <code>example.org</code>, redirecting all requests to <code><nowiki>http://10.25.40.6</nowiki></code>
 
{{file|/etc/nixos/configuration.nix|nix|<nowiki>
caddy = {
  enable = true;
  virtualHosts."example.org".extraConfig = ''
    reverse_proxy http://10.25.40.6
  '';
}; 
</nowiki>}}


=== Redirect ===
=== Redirect ===


Redirecting <code>example.org</code> and <code>old.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" = {
caddy.virtualHosts."example.org" = {
Line 29: Line 72:
};
};
</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]]