Kest

Search

Search the documentation

FAQ

HTTPS & multiple servers

TLS, and several servers in one application.

The questions: How do I serve HTTPS? Can I also listen on a plain port? Can one app run several servers?

HTTPS in the application

val tls = SslContextBuilder.forServer(File("fullchain.pem"), File("privkey.pem")).build()
app.http(NettyHttpAdapter(8443, sslContext = tls))                      // HTTPS

fullchain.pem is your certificate (with the chain) and privkey.pem its private key, for example from Let's Encrypt. For HTTP/2 over TLS build the context with http2TlsContext(cert, key) and pass http2 = true.

Multiple servers

app.http(...) can be called more than once: each call starts another server on the same container, sharing every provider:

app.http(NettyHttpAdapter(8443, sslContext = tls))                       // the real API
app.http(NettyHttpAdapter(8080)) { useMiddleware(redirectToHttps) }      // a plain port whose only job is to redirect

The second server needs a middleware of your own that answers every request with a redirect to the https:// address. Another use: an admin or metrics server on a private port, away from the public one:

app.http(NettyHttpAdapter(9090)) { useMetrics(); useHealth("/health") { } }       // internal only

Or terminate TLS outside (usual in production)

Most deployments put a load balancer, ingress or reverse proxy in front (nginx, Caddy, a cloud load balancer): it holds the certificates, renews them and speaks HTTPS to the world, while Kest listens on plain HTTP inside the private network. This is simpler to operate (one place for certificates) and is the common choice. Make sure the proxy forwards the original host and scheme (X-Forwarded-Host, X-Forwarded-Proto) and that Kest's idle timeout is longer than the proxy's (Keep-Alive connections).

Checklist

  • Certificates expire: automate renewal (and a reload or restart on renewal).
  • Send security headers including HSTS.
  • Never serve cookies with secure = false on a public site.

Read more