Revel doesn't forward to port 443 when SSL ena

2019-04-10 23:52发布

问题:

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:

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.



标签: ssl go revel