本地主机默认不启用 HTTP2

标签 http go localhost webserver http2

我可能对此一无所知,但由于某些奇怪的原因,我的基本本地主机服务器没有启用 HTTP2,我通常在 Caddy 后面代理,但由于我不想将我的域用于这个副项目,我创建了Go 中的一个基本服务器,并运行它,它工作正常,但 header 显示 HTTP/1.1 而不是 2.0,有什么问题吗?

package main

import (
  "fmt"
  "net/http"
  "html/template"
  "os"
)

func IfError(err error, Quit bool) {
  if err != nil {
    fmt.Println(err.Error())
    if(Quit) {
      os.Exit(1);
    }
  }
}

func ServeHome(w http.ResponseWriter, r *http.Request) {
  t, err := template.ParseFiles("html/home")
  IfError(err, false)
  err = t.Execute(w, nil)
  IfError(err, false)
}

func RedirectRoot(fs http.Handler, home http.Handler) http.Handler {
  return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    if r.URL.Path == "/" {
      home.ServeHTTP(w, r)
    } else {
      fs.ServeHTTP(w, r)
    }
  })
}

func main()  {
  proto := ":8081"
  ServeFiles := http.FileServer(http.Dir("static/"))
  http.Handle("/", RedirectRoot(ServeFiles, http.HandlerFunc(ServeHome)))
  fmt.Printf("Listening on ... %s", proto)
  IfError(http.ListenAndServe(proto, nil), true)
}

非常基本的东西,但即使文档说它在默认情况下可以工作,也无法工作。还有,我的go版本是1.8.3

最佳答案

是的,当您使用 SSL 证书时它默认启用。

Doc Reference: Starting with Go 1.6, the http package has transparent support for the HTTP/2 protocol when using HTTPS.

err := http.ListenAndServeTLS(":8081", "server.crt", "server.key", handler)
if err != nil && err != http.ErrServerClosed {
    log.Fatal("ListenAndServe: ", err)
}

然后,通过

访问它
https://localhost:8081/

关于本地主机默认不启用 HTTP2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44866396/

相关文章:

http - HTTP 中的内容编码与传输编码

loops - Go中没有分配的循环

go - 使用具有外部功能的 sync.WaitGroup 的最佳方式

c# - 如何使用 WebClient.DownloadData(到本地 DummyPage.aspx)

ios - 即使在 IBM Worklight 中添加异常域,HTTP 也无法正常工作?

apache - 将文件下载重定向到 CDN

http - 关于Access-Control-Allow-Origin和CORS的问题

go - 如何为 GRPC 服务设置 Strict-Transport-Header

Mongodb 无法连接到 localhost 但可以连接到 localhost 的 IP 地址

oauth - 当我的本地域未在 Yahoo 注册时,我如何在本地开发时让 Yahoo OAuth 工作?