How to create a cohttp server that accepts both HTTP and HTTPS requests?

Would someone have an example of a cohttp server accepting both HTTP and HTTPS requests? Having a look at the API, it seems to me that I have to call Cohttp_lwt_unix.Server.create twice, with a different argument for ~mode, is that right?

Yes you can add a command-line argument to your cohttp server so that it can run in both modes. Then start two instances of the server, one in http mode and one in https mode. You may also want to redirect all connections of the http server to the https server (e,g, using Cohttp_lwt_unix.Server.respond_redirect)

That being said, if you are serious about running a web server that talks to the Internet, a much better option would probably be to put the cohttp server behind a reverse proxy such as Apache/Nginx/HAProxy, which can handle https redirection (and a lot other things) for you.

2 Likes

Thanks for the answer and the suggestion, which I’ll probably follow!