簡體   English   中英

默認情況下,本地未啟用HTTP2

[英]HTTP2 not enabled by default on localhost

我可能對此一無所知,但是由於某些奇怪的原因,我的基本本地主機服務器沒有啟用HTTP2,我通常在Caddy之后進行代理,但是由於我不想將我的域用於此輔助項目,因此我創建了一個基本服務器在Go中運行它可以正常運行,但是標題顯示的是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證書一起使用時,默認情況下啟用此功能。

文檔參考 :從Go 1.6開始,使用HTTPS時,http軟件包對HTTP / 2協議具有透明支持。

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

然后,通過訪問

https://localhost:8081/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM