Revel在启用SSL时不会转发到端口443。

huangapple go评论79阅读模式
英文:

Revel doesn't forward to port 443 when SSL enabled

问题

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

# 监听的端口号。
http.port = 443

# 是否使用SSL。
http.ssl = true

# SSL证书文件的路径,如果使用SSL。
http.sslcert = /root/go/src/saml/AlphaCerts/cert.crt

# SSL证书密钥文件的路径,如果使用SSL。
http.sslkey = /root/go/src/saml/star_home_com.key
英文:

I'm using Revel for a small app and added SSL. When I update the config to point to http.port = 443, requests to port 80 are rejected instead of being forwarded. Is there a way to fix this on the Revel Framework? Thank you.

# 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 重定向代码。

英文:

You could add a simple redirect handler yourself.

Try putting this in your app/init.go file:

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()

Note that in Revel's dev mode, you'll first have to access your app on port 443 to have Revel start up properly before your port 80 redirect code will be run.

huangapple
  • 本文由 发表于 2015年11月6日 03:53:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/33553583.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定