Jump to content

Caddy: Difference between revisions

→‎Reverse proxy: Add example on forward real client ip
imported>Malteneuss
m (Show caddy config as part of whole NixOS module)
(→‎Reverse proxy: Add example on forward real client ip)
 
(10 intermediate revisions by 6 users not shown)
Line 4: Line 4:
== Get started ==
== Get started ==


To try out Caddy add the following minimal example to your [https://nixos.wiki/wiki/NixOS_modules NixOS module]:
To try out Caddy add the following minimal example to your [[NixOS modules | NixOS module]]:


<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{
{
   # ...
   # ...
   config.services.caddy = {
   services.caddy = {
     enable = true;
     enable = true;
     virtualHosts."localhost".extraConfig = ''
     virtualHosts."localhost".extraConfig = ''
Line 50: Line 50:
</syntaxhighlight>
</syntaxhighlight>


Curl will set <code>Host</code> header and TLS <code>SNI</> in the request to <code><virtualhost></code> as desired by Caddy, but will make the actual request against the <code><realhost></code>, e.g. a load-balancer or ingress-controller.
Curl will set <code>Host</code> header and TLS <code>SNI</code> in the request to <code><virtualhost></code> as desired by Caddy, but will make the actual request against the <code><realhost></code>, e.g. a load-balancer or ingress-controller.


Alternatively with http and automatic redirects to https you can extend that call:
Alternatively with http and automatic redirects to https you can extend that call:
Line 95: Line 95:
   virtualHosts."example.org".extraConfig = ''
   virtualHosts."example.org".extraConfig = ''
     reverse_proxy http://10.25.40.6
     reverse_proxy http://10.25.40.6
  '';
  virtualHosts."another.example.org".extraConfig = ''
    reverse_proxy unix//run/gunicorn.sock
  '';
};
</syntaxhighlight>In case you would like to forward the real client IP of the request to the backend, add following headers<syntaxhighlight lang="nix">
services.caddy = {
  virtualHosts."example.org".extraConfig = ''
    reverse_proxy http://10.25.40.6 {
      header_down X-Real-IP {http.request.remote}
      header_down X-Forwarded-For {http.request.remote}
    }
   '';
   '';
};
};
</syntaxhighlight>
</syntaxhighlight>Fur further reverse proxy configuration, see [https://caddyserver.com/docs/quick-starts/reverse-proxy upstream documentation].
 
* [https://caddyserver.com/docs/quick-starts/reverse-proxy Caddy reverse proxy documentation]


=== Redirect ===
=== Redirect ===
Line 110: Line 120:
   virtualHosts."example.org" = {
   virtualHosts."example.org" = {
     extraConfig = ''
     extraConfig = ''
       redir https://www.example.org
       redir https://www.example.org{uri}
   '';
   '';
     serverAlias = [ "old.example.org" ];
     serverAliases = [ "old.example.org" ];
};
};
</syntaxhighlight>
</syntaxhighlight>
Line 203: Line 213:


[[Category:Applications]]
[[Category:Applications]]
[[Category:Web Servers]]
[[Category:Server]]
[[Category:Networking]]
63

edits