Caddy: Difference between revisions
imported>Onny mNo edit summary |
imported>Onny Add example for PHP Fastcgi |
||
Line 55: | Line 55: | ||
}; | }; | ||
</nowiki>}} | </nowiki>}} | ||
=== Redirect === | === Redirect === | ||
Line 67: | Line 68: | ||
}; | }; | ||
</syntaxhighlight> | </syntaxhighlight> | ||
=== PHP FastCGI === | |||
Serving a PHP application in <code>/var/www</code> on <code>localhost</code>. | |||
<syntaxhighlight lang="nix> | |||
services.caddy = { | |||
enable = true; | |||
virtualHosts."http://localhost" = { | |||
extraConfig = '' | |||
root * /var/www | |||
file_server | |||
php_fastcgi unix/var/run/phpfpm/localhost.sock | |||
''; | |||
}; | |||
}; | |||
</syntaxhighlight> | |||
You'll need a [[Phpfpm|PHP-FPM]] socket listening on Unix socket path <code>/var/run/phpfpm/localhost.sock</code>. | |||
== 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 12:59, 30 January 2023
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 > "$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 = ''
encode gzip
file_server
root * ${
pkgs.runCommand "testdir" {} ''
mkdir "$out"
echo hello world > "$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" ];
};
PHP FastCGI
Serving a PHP application in /var/www
on localhost
.
services.caddy = {
enable = true;
virtualHosts."http://localhost" = {
extraConfig = ''
root * /var/www
file_server
php_fastcgi unix/var/run/phpfpm/localhost.sock
'';
};
};
You'll need a PHP-FPM socket listening on Unix socket path /var/run/phpfpm/localhost.sock
.