症状
ring.middleware.ssl/wrap-ssl-redirectを使って強制的に接続をhttpsにしようとしたら、Heroku上のアプリでページが表示されなくなった。
解決
Clojure: wrap-ssl-redirect on heroku?
のコメントに対処法が共有されていた。
(defn wrap-force-ssl
"Almost like in lib-noir.
If the request's scheme is not https [and is for 'secure.'], redirect with https.
Also checks the X-Forwarded-Proto header."
[app]
(fn [req]
(let [headers (:headers req)
host (headers "host")]
(if (or (= :https (:scheme req))
(= "https" (headers "x-forwarded-proto"))
(not= "secure.mydomain.com" host)) ;you might not need this!
(app req)
(redirect (str "https://" host (:uri req)) :permanent)))))
※更新
自身の環境で上手く作動していないことに気づいたのでringのソースを確認したら、permanentの指定方法が違っていました。
なので、上記の:permanent
は :moved-permanently
にするか :permanent-redirect
にするとよいです。
status codeの違いはstackoverflowの説明がわかりやすいかったので、参考にして下さい。
更新ここまで。
lib-noirを使っている人はこのような関数があるようですのでそちらを使えばいいそうです(使ったことがないのでよく知らない)
あとはwrap-ssl-redirect
と同じようにhandlerに渡してやればいい
(-> handler
(wrap-defaults site-defaults)
wrap-force-ssl)
※ ringの設定の変更(?)のためかredirectの設定が上手く作動していなかった旨を更新しました。