2 回答

TA貢獻(xiàn)1810條經(jīng)驗(yàn) 獲得超4個(gè)贊
使用 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)
}

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超10個(gè)贊
感謝約翰·漢利 (John Hanley) 的支持,導(dǎo)致了這個(gè)答案。首先,我確實(shí)通過編輯 /etc/apache2/ports.conf 為 https 設(shè)置了端口 8443:
Listen 80
<IfModule ssl_module>
Listen 443
Listen 8443
</IfModule>
然后我在 example.com 域的配置中添加了一個(gè) VirtualHost,以便端口 8443 充當(dāng)代理:
<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和加載模塊 proxy 和 proxy_http e2enmod proxy_http。重新加載 apache 后,可以在https://www.example.com:8443/hello調(diào)用 API 。
- 2 回答
- 0 關(guān)注
- 103 瀏覽
添加回答
舉報(bào)