Lesson 46 of 60 · Go
Building a HTTP/2 Server with TLS
Duration: 20 minutes
HTTP/2 + TLS
This lesson explains the main concept clearly with beginner-friendly examples and background so you can learn why the code works.
Go enables HTTP/2 automatically when serving over TLS.
cert, _ := tls.LoadX509KeyPair("server.crt", "server.key")
server := &http.Server{
Addr: ":443",
Handler: mux,
TLSConfig: &tls.Config{MinVersion: tls.VersionTLS12},
}
log.Fatal(server.ListenAndServeTLS("", "")) // cert/key already in TLSConfig
ALPN negotiation
The client and server negotiate HTTP/2 via ALPN during the TLS handshake.
Testing locally
$ mkcert -install
$ mkcert localhost
Then run the server with the generated localhost+2.pem and localhost+2-key.pem.