Enabling HTTPS in Dream is very easy: just pass ~tls:true to
Dream.run:
let () =
Dream.run ~tls:true
@@ Dream.logger
@@ fun _ -> Dream.html "Good morning, world!"$ cd example/l-https
$ opam install --deps-only --yes .
$ dune exec --root . ./https.exeHowever, when you visit https://localhost:8080, you will have to click through a bunch of certificate errors. That's because, by default, Dream uses a compiled-in localhost certificate, which is suitable only for development. The certificate is technically valid, but it is self-signed, and the browser rightly recognizes it as dubious.
For production, be sure to obtain a real certificate, for example, from
Let's Encrypt. Pass the certificate to
Dream.run with
~certificate_file and ~key_file. Dream will remind you to do this in the
logs if you try to use the built-in self-signed certificate when not running on
localhost.
Enabling HTTPS also enables upgrading of connections to HTTP/2, if the client
requests it. Whether HTTP/1.1 or HTTP/2 was used is completely transparent to
the Web app, though it can examine the protocol version by calling
Dream.version on any given
request.
That's all for the tutorial!