LoginSignup
0
0

More than 5 years have passed since last update.

Clojure: Herokuで強制的にHTTPS通信をさせようとしたらページが表示できない場合

Last updated at Posted at 2018-02-08

症状

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の設定が上手く作動していなかった旨を更新しました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0