当启用SSL时,Revel没有转发到端口443

3

我正在使用Revel框架开发一款小型应用,并添加了SSL。当我更新配置文件中的http.port = 443后,对80端口的请求被拒绝而不是被转发。请问在Revel框架上有没有解决这个问题的方法?谢谢。

# The port on which to listen.
http.port = 443

# Whether to use SSL or not.
http.ssl = true

# Path to an X509 certificate file, if using SSL.
http.sslcert = /root/go/src/saml/AlphaCerts/cert.crt

# Path to an X509 certificate key, if using SSL.
http.sslkey = /root/go/src/saml/star_home_com.key
1个回答

5

您可以自己添加一个简单的重定向处理程序。

尝试将以下内容放入您的app/init.go文件中:

httpRedirectServer := &http.Server{Addr: ":80", Handler: http.HandlerFunc(
    func(w http.ResponseWriter, r *http.Request) {
        http.Redirect(w, r, fmt.Sprintf("https://%s%s", r.Host, r.RequestURI), 
                      http.StatusMovedPermanently)
    })}
go httpRedirectServer.ListenAndServe()

请注意,在Revel的开发模式下,您需要首先通过端口443访问您的应用程序,以便使Revel正确启动,然后才能运行您的80端口重定向代码。

这段代码在不做任何修改的情况下成功运行。对于其他人,在将上述代码放置在func init()中后,请注意导入net/http和fmt。谢谢! - vinniyo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接