···6767</section>
68686969<section><title>Using ACME certificates in Nginx</title>
7070-<para>In practice ACME is mostly used for retrieval and renewal of
7171- certificates that will be used in a webserver like Nginx. A configuration for
7272- Nginx that uses the certificates from ACME for
7373- <literal>foo.example.com</literal> will look similar to:
7070+<para>NixOS supports fetching ACME certificates for you by setting
7171+<literal>enableACME = true;</literal> in a virtualHost config. We
7272+first create self-signed placeholder certificates in place of the
7373+real ACME certs. The placeholder certs are overwritten when the ACME
7474+certs arrive. For <literal>foo.example.com</literal> the config would
7575+look like.
7476</para>
75777678<programlisting>
7777-security.acme.certs."foo.example.com" = {
7878- webroot = config.security.acme.directory + "/acme-challenge";
7979- email = "foo@example.com";
8080- user = "nginx";
8181- group = "nginx";
8282- postRun = "systemctl restart nginx.service";
8383-};
8484-services.nginx.httpConfig = ''
8585- server {
8686- server_name foo.example.com;
8787- listen 80;
8888- listen [::]:80;
8989-9090- location /.well-known/acme-challenge {
9191- root /var/www/challenges;
9292- }
9393-9494- location / {
9595- return 301 https://$host$request_uri;
9696- }
9797- }
9898-9999- server {
100100- server_name foo.example.com;
101101- listen 443 ssl;
102102- ssl_certificate ${config.security.acme.directory}/foo.example.com/fullchain.pem;
103103- ssl_certificate_key ${config.security.acme.directory}/foo.example.com/key.pem;
104104- root /var/www/foo.example.com/;
105105- }
106106-'';
7979+services.nginx = {
8080+ enable = true;
8181+ virtualHosts = {
8282+ "foo.example.com" = {
8383+ forceSSL = true;
8484+ enableACME = true;
8585+ locations."/" = {
8686+ root = "/var/www";
8787+ };
8888+ };
8989+ };
9090+}
10791</programlisting>
10892109109-<para>Now Nginx will try to use the certificates that will be retrieved by ACME.
110110- ACME needs Nginx (or any other webserver) to function and Nginx needs
111111- the certificates to actually start. For this reason the ACME module
112112- automatically generates self-signed certificates that will be used by Nginx to
113113- start. After that Nginx is used by ACME to retrieve the actual ACME
114114- certificates. <literal>security.acme.preliminarySelfsigned</literal> can be
115115- used to control whether to generate the self-signed certificates.
116116-</para>
9393+<para>At the moment you still have to restart Nginx after the ACME
9494+certs arrive.</para>
11795</section>
11896</chapter>