Golang 的 HTTPS API
HTTPS for Golang API
我是 Golang 的新手,我确实设置了一个“hello world!”在我们的 VPS 上测试 Golang API 的消息。它在 http://www.example.com:8080/hello 时工作得很好。不过,我想转到 HTTPS。
有人可以逐步告诉我从 HTTP 到 HTTPS 的 golang API 的正确过程吗?谢谢!
如果 golang 代码有问题:
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World")
})
fmt.Println("Server Started On Port 8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
改用http.ListenAndServeTLS
https://pkg.go.dev/net/http#ListenAndServeTLS
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World")
})
fmt.Println("Server Started On Port 8080")
err := http.ListenAndServeTLS(":8080", "cert.pem", "key.pem", nil)
log.Fatal(err)
}
补充@Dan 的回答。实现稍微复杂一些,但可以让您对其进行更多配置。
如果你想使用一套以上的证书
certs := []tls.Certificate{}
for _, cert := range []{"cert1", "cert2"} {
certSet, err := tls.LoadX509KeyPair(cert+".pem", cert+".key")
if err != nil {
return err
}
certs = append(certs, certSet)
}
创建 tls config
cfg := &tls.Config {
Certificates: certs,
MinVersion: tls.VersionTLS12,
}
cfg.BuildNameToCertificate()
server := &http.Server{
Addr: ":8080",
TLSConfig: cfg,
IdleTimeout: 30 * time.Second,
}
添加处理程序并启动服务器
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World")
})
err := server.ListenAndServeTLS("", "") // Dont give filename here if giving set of certs in tls config above
if err != nil {
return err
}
感谢约翰·汉利 (John Hanley) 的支持,才得出这个答案。
首先,我确实通过编辑 /etc/apache2/ports.conf:
为 https 设置了端口 8443
Listen 80
<IfModule ssl_module>
Listen 443
Listen 8443
</IfModule>
然后我在 example.com 域的配置中添加了一个 VirtualHost,以便端口 8443 充当代理:
<VirtualHost *:8443>
ServerAdmin admin@example.com
ServerName www.example.com
ServerAlias example.com
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
并且您需要使用 e2enmod proxy
和 e2enmod proxy_http
加载模块代理和 proxy_http。
重新加载 apache 后,可以在 https://www.example.com:8443/hello.
调用 API
我是 Golang 的新手,我确实设置了一个“hello world!”在我们的 VPS 上测试 Golang API 的消息。它在 http://www.example.com:8080/hello 时工作得很好。不过,我想转到 HTTPS。
有人可以逐步告诉我从 HTTP 到 HTTPS 的 golang API 的正确过程吗?谢谢!
如果 golang 代码有问题:
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World")
})
fmt.Println("Server Started On Port 8080")
log.Fatal(http.ListenAndServe(":8080", nil))
}
改用http.ListenAndServeTLS
https://pkg.go.dev/net/http#ListenAndServeTLS
package main
import (
"fmt"
"log"
"net/http"
)
func main() {
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World")
})
fmt.Println("Server Started On Port 8080")
err := http.ListenAndServeTLS(":8080", "cert.pem", "key.pem", nil)
log.Fatal(err)
}
补充@Dan 的回答。实现稍微复杂一些,但可以让您对其进行更多配置。
如果你想使用一套以上的证书
certs := []tls.Certificate{}
for _, cert := range []{"cert1", "cert2"} {
certSet, err := tls.LoadX509KeyPair(cert+".pem", cert+".key")
if err != nil {
return err
}
certs = append(certs, certSet)
}
创建 tls config
cfg := &tls.Config {
Certificates: certs,
MinVersion: tls.VersionTLS12,
}
cfg.BuildNameToCertificate()
server := &http.Server{
Addr: ":8080",
TLSConfig: cfg,
IdleTimeout: 30 * time.Second,
}
添加处理程序并启动服务器
http.HandleFunc("/hello", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello, World")
})
err := server.ListenAndServeTLS("", "") // Dont give filename here if giving set of certs in tls config above
if err != nil {
return err
}
感谢约翰·汉利 (John Hanley) 的支持,才得出这个答案。 首先,我确实通过编辑 /etc/apache2/ports.conf:
为 https 设置了端口 8443Listen 80
<IfModule ssl_module>
Listen 443
Listen 8443
</IfModule>
然后我在 example.com 域的配置中添加了一个 VirtualHost,以便端口 8443 充当代理:
<VirtualHost *:8443>
ServerAdmin admin@example.com
ServerName www.example.com
ServerAlias example.com
ProxyRequests Off
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/example.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/example.com/privkey.pem
</VirtualHost>
并且您需要使用 e2enmod proxy
和 e2enmod proxy_http
加载模块代理和 proxy_http。
重新加载 apache 后,可以在 https://www.example.com:8443/hello.