# Proper way to enforce https only?

**URL:** <https://forum.elixirforum.com/t/proper-way-to-enforce-https-only/2117>\
**Category:** Questions\
**Tags:** phoenix, https\
**Created:** [October 25, 2016, 8:11pm UTC](https://forum.elixirforum.com/t/proper-way-to-enforce-https-only/2117 "2016-10-25T20:11:08Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![ibgib](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ibgib/32/2176_2.png) [@ibgib](https://forum.elixirforum.com/u/ibgib)\
**Post date:** [October 25, 2016, 8:11pm UTC](https://forum.elixirforum.com/t/proper-way-to-enforce-https-only/2117/1 "2016-10-25T20:11:08Z")

</div>

I’m currently in the process of deploying ibgib, and I just finished configuring my DNS 👀 🏁.

Now I’m trying to set up https redirection so that `(http://)www.ibgib.com` will automatically redirect to `https://www.ibgib.com`. What is the “proper” way to implement this?

I know of the `force_ssl` option in the endpoint configuration, which I’ve tried turning on (with `hsts: true`), however this does not _seem_ to do the redirect. But this could be that I do not have my docker setup properly configured. Is setting this supposed to do the redirect? Or am I supposed to accomplish this another way, perhaps with nginx? And if nginx is the way to go, could I then completely remove the `http` config setting from my endpoint config? 🤔

#### Additional Background

I’m currently using distillery to produce a release and then docker (engine, compose, machine) with a “simple” setup of my release container + a postgres container deployed to aws ec2. My full-ish (foolish? 😕) ongoing deployment notes can be found in my [deploy issue](https://github.com/ibgib/ibgib/issues/74) for anyone else looking to go elixir + docker + aws.

---

<div class="post-metadata">

**Author:** ![josevalim](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/josevalim/32/1787_2.png) [@josevalim](https://forum.elixirforum.com/u/josevalim)\
**Post date:** [October 25, 2016, 9:09pm UTC](https://forum.elixirforum.com/t/proper-way-to-enforce-https-only/2117/2 "2016-10-25T21:09:01Z")

</div>

FWIW, in my experience Plug.SSL, which is what Phoenix uses behind the scenes for `force_ssl: [hsts: true]`, works fine.

---

<div class="post-metadata">

**Author:** ![ibgib](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ibgib/32/2176_2.png) [@ibgib](https://forum.elixirforum.com/u/ibgib)\
**Post date:** [October 25, 2016, 9:46pm UTC](https://forum.elixirforum.com/t/proper-way-to-enforce-https-only/2117/3 "2016-10-25T21:46:01Z")

</div>

> [@josevalim](#):
>
> FWIW, in my experience Plug.SSL, which is what Phoenix uses behind the scenes for force\_ssl: [hsts: true], works fine.

Thanks @josevalim. I think that may be the sanity check that I’m looking for, as reading the [docs](https://hexdocs.pm/plug/Plug.SSL.html) it seems obvious what it should do…but I’m just not seeing it happening! 🙈

It is probably a docker configuration issue that I just haven’t been able to pin down yet. 📌

---

<div class="post-metadata">

**Author:** ![ibgib](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ibgib/32/2176_2.png) [@ibgib](https://forum.elixirforum.com/u/ibgib)\
**Post date:** [October 26, 2016, 5:21pm UTC](https://forum.elixirforum.com/t/proper-way-to-enforce-https-only/2117/4 "2016-10-26T17:21:08Z")

</div>

Well, I couldn’t get the `force_ssl` to work as expected. 🙁 I would hazard a guess that it may be because of the rewrite not working with the docker-machine vm and/or aws configuration. ❔

I was however able to get it (mostly) working with a _very_ basic, customized [`https` + `www` redirect nginx container](https://github.com/bill-mybiz/docker-nginx-https-redirect). I forked this from an [`https`-only redirect container](https://github.com/jamessharp/docker-nginx-https-redirect), and customized it for also forcing `www` as well. 😄 🍪

I say “mostly” because both [ibgib.com](http://ibgib.com) and [www.ibgib.com](http://www.ibgib.com) redirect as expected to `https://www.ibgib.com`. Also if you type in `https://www.ibgib.com` directly it will work as well (\*). However, if you type in `https://ibgib.com`, explicitly stating the `https` and giving a naked URL, it doesn’t properly redirect to the `www` version, but at this point I’m fine with that, as I’ll have to reconfigure this stuff anyway in a later release. 😅

I just wanted to post my results for anyone else going the distillery + docker (engine, compose, machine) route.

Thanks again! 😄

(\*) _This may seem like a given to devops/deployment gurus, but I found plenty of (mis)configurations where this doesn’t work 😉._

---

<div class="post-metadata">

**Author:** ![logicmason](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/logicmason/32/40554_2.png) [@logicmason](https://forum.elixirforum.com/u/logicmason)\
**Post date:** [March 27, 2017, 2:49am UTC](https://forum.elixirforum.com/t/proper-way-to-enforce-https-only/2117/5 "2017-03-27T02:49:34Z")

</div>

It’s not working for me either. I’ve tried both what was in the phoenix guides and `force_ssl: [hsts: true]` and read the docs for Plug.SSL without seeing any clear reason it’s not working.

If I type in [应用宝官网-全网最新最热手机应用游戏下载](https://myapp.com/foo), it loads with https. If I type [应用宝官网-全网最新最热手机应用游戏下载](http://myapp.com/foo), it loads without. What I want is for the https route to load, even if the user doesn’t type https into the location bar of their browser.

I’m not using a proxy. It’s just a normal install of phoenix.

Here’s my prod.exs config:

```elixir
config :myapp, Myapp.Endpoint,
  server: true,
  url: [scheme: "https", host: "myapp.com", port: 443],
  http: [compress: true, port: 80],
  https: [compress: true, port: 443,
  force_ssl: [hsts: true],
    otp_app: :myapp,
    keyfile: "/etc/letsencrypt/live/myapp.com/privkey.pem",
    certfile: "/etc/letsencrypt/live/myapp.com/cert.pem"],
  cache_static_manifest: "priv/static/manifest.json"

```

---

<div class="post-metadata">

**Author:** ![chensan](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/chensan/32/11011_2.png) [@chensan](https://forum.elixirforum.com/u/chensan)\
**Post date:** [March 27, 2017, 4:33am UTC](https://forum.elixirforum.com/t/proper-way-to-enforce-https-only/2117/6 "2017-03-27T04:33:17Z")

</div>

I’m offloading the SSL termination to HAProxy, you can do the redirect easy in haproxy.cfg:

```elixir
frontend http-in-website
  bind 0.0.0.0:80
  redirect scheme https code 301 if !{ ssl_fc }

frontend https-in-website
  bind 0.0.0.0:443 ssl crt /etc/haproxy/ssl
  default_backend servers-website

backend servers-website
  server w1 127.0.0.1:4200 check maxconn 4000

```

Your server just listen on `127.0.0.1:4200`, no need to care about the `force_ssl`.

You still need the `http` config, check [http://www.phoenixframework.org/docs/serving-your-application-behind-a-proxy](http://www.phoenixframework.org/docs/serving-your-application-behind-a-proxy).

---

<div class="post-metadata">

**Author:** ![ibgib](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ibgib/32/2176_2.png) [@ibgib](https://forum.elixirforum.com/u/ibgib)\
**Post date:** [March 27, 2017, 11:57am UTC](https://forum.elixirforum.com/t/proper-way-to-enforce-https-only/2117/7 "2017-03-27T11:57:26Z")

</div>

> [@chensan](#):
>
> I’m offloading the SSL termination to HAProxy, you can do the redirect easy in haproxy.cfg

Yes, FWIW this is what I ended up doing as well with [ibGib using nginx](https://github.com/ibgib/ibgib/blob/master/ib_gib_umb/nginx/default.conf). I ended up terminating the SSL at the proxy, redirect 301, etc.

---

<div class="post-metadata">

**Author:** ![pshoukry](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/pshoukry/32/14596_2.png) [@pshoukry](https://forum.elixirforum.com/u/pshoukry)\
**Post date:** [September 6, 2017, 1:02pm UTC](https://forum.elixirforum.com/t/proper-way-to-enforce-https-only/2117/8 "2017-09-06T13:02:08Z")

</div>

Did anyone ever find a solution for this, I am still seeing the same problem on docker, AWS, distillery.

---

<div class="post-metadata">

**Author:** ![Ankhers](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/a/ce7236/32.png) [@Ankhers](https://forum.elixirforum.com/u/Ankhers)\
**Post date:** [September 6, 2017, 5:11pm UTC](https://forum.elixirforum.com/t/proper-way-to-enforce-https-only/2117/10 "2017-09-06T17:11:16Z")

</div>

I am currently using `force_ssl: [rewrite_on: [:x_forwarded_proto]]` for my application running on AWS (ECS specifically) and it is working fine.

---

<div class="post-metadata">

**Author:** ![pshoukry](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/pshoukry/32/14596_2.png) [@pshoukry](https://forum.elixirforum.com/u/pshoukry)\
**Post date:** [September 6, 2017, 5:16pm UTC](https://forum.elixirforum.com/t/proper-way-to-enforce-https-only/2117/11 "2017-09-06T17:16:06Z")

</div>

> force\_ssl: [hsts: true, host: nil]

Solved it for me.

---

<div class="post-metadata">

**Author:** ![Nagasaki45](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nagasaki45/32/4859_2.png) [@Nagasaki45](https://forum.elixirforum.com/u/Nagasaki45)\
**Post date:** [September 8, 2017, 11:26pm UTC](https://forum.elixirforum.com/t/proper-way-to-enforce-https-only/2117/12 "2017-09-08T23:26:06Z")

</div>

Thanks @pshoukry! It solved the problem for me (EC2, no proxy, just iptables routing).

---

<div class="post-metadata">

**Author:** ![pshoukry](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/pshoukry/32/14596_2.png) [@pshoukry](https://forum.elixirforum.com/u/pshoukry)\
**Post date:** [September 9, 2017, 9:09am UTC](https://forum.elixirforum.com/t/proper-way-to-enforce-https-only/2117/13 "2017-09-09T09:09:04Z")

</div>

@Nagasaki45 most welcome! Great to hear it worked

---

<div class="post-metadata">

**Author:** ![indatawetrust](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/indatawetrust/32/9875_2.png) [@indatawetrust](https://forum.elixirforum.com/u/indatawetrust)\
**Post date:** [December 18, 2017, 1:43pm UTC](https://forum.elixirforum.com/t/proper-way-to-enforce-https-only/2117/14 "2017-12-18T13:43:12Z")

</div>

thank you.

---

<div class="post-metadata">

**Author:** ![arcseldon](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/arcseldon/32/6731_2.png) [@arcseldon](https://forum.elixirforum.com/u/arcseldon)\
**Post date:** [April 8, 2018, 12:55pm UTC](https://forum.elixirforum.com/t/proper-way-to-enforce-https-only/2117/15 "2018-04-08T12:55:06Z")

</div>

@pshoukry - thank you, your solution worked too (after much googling).

---

<div class="post-metadata">

**Author:** ![snewcomer](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/snewcomer/32/9343_2.png) [@snewcomer](https://forum.elixirforum.com/u/snewcomer)\
**Post date:** [July 5, 2018, 7:17pm UTC](https://forum.elixirforum.com/t/proper-way-to-enforce-https-only/2117/16 "2018-07-05T19:17:26Z")

</div>

> [@Nagasaki45](#):
>
> iptable

Great thread! Anybody have a good resource for their iptables config?

---

<div class="post-metadata">

**Author:** ![ijunaidfarooq](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ijunaidfarooq/32/1532_2.png) [@ijunaidfarooq](https://forum.elixirforum.com/u/ijunaidfarooq)\
**Post date:** [December 11, 2018, 3:41pm UTC](https://forum.elixirforum.com/t/proper-way-to-enforce-https-only/2117/17 "2018-12-11T15:41:41Z")

</div>

I am on heroku with Phoenix app. and this didnt work for me. any solution?

> config :edge\_commander, EdgeCommanderWeb.Endpoint,  
> force\_ssl: [hsts: true, host: nil]

---

<div class="post-metadata">

**Author:** ![outlog](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@outlog](https://forum.elixirforum.com/u/outlog)\
**Post date:** [December 11, 2018, 3:54pm UTC](https://forum.elixirforum.com/t/proper-way-to-enforce-https-only/2117/18 "2018-12-11T15:54:15Z")

</div>

> [@ijunaidfarooq](#):
>
> I am on heroku with Phoenix app. and this didnt work for me. any solution?
> 
> > config :edge\_commander, EdgeCommanderWeb.Endpoint,  
> > force\_ssl: [hsts: true, host: nil]

try

`force_ssl: [rewrite_on: [:x_forwarded_proto], hsts: true, host: nil],`

---

<div class="post-metadata">

**Author:** ![ijunaidfarooq](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ijunaidfarooq/32/1532_2.png) [@ijunaidfarooq](https://forum.elixirforum.com/u/ijunaidfarooq)\
**Post date:** [December 11, 2018, 4:00pm UTC](https://forum.elixirforum.com/t/proper-way-to-enforce-https-only/2117/19 "2018-12-11T16:00:17Z")

</div>

> [@outlog](#):
>
> force\_ssl: [rewrite\_on: [:x\_forwarded\_proto], hsts: true, host: nil]

It didnt work.. 🙂 results are the same, nothing get force. both app. and [https://app](https://app). working, any other clues?

---

<div class="post-metadata">

**Author:** ![outlog](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/o/c0e974/32.png) [@outlog](https://forum.elixirforum.com/u/outlog)\
**Post date:** [December 11, 2018, 4:14pm UTC](https://forum.elixirforum.com/t/proper-way-to-enforce-https-only/2117/20 "2018-12-11T16:14:14Z")

</div>

“it works for me”

can you check with curl

eg

`curl -s -D- http://myapp.herokuapp.com`  
should output a 301 to the https

and

`curl -s -D- https://myapp.herokuapp.com | grep -i Strict`

should output  
Strict-Transport-Security: max-age=31536000

---

<div class="post-metadata">

**Author:** ![ijunaidfarooq](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/ijunaidfarooq/32/1532_2.png) [@ijunaidfarooq](https://forum.elixirforum.com/u/ijunaidfarooq)\
**Post date:** [December 11, 2018, 4:21pm UTC](https://forum.elixirforum.com/t/proper-way-to-enforce-https-only/2117/21 "2018-12-11T16:21:25Z")

</div>

> [@outlog](#):
>
> curl -s -D- [http://myapp.herokuapp.com](http://myapp.herokuapp.com)

Thanks it worked. 🙂

[Next page](https://forum.elixirforum.com/t/proper-way-to-enforce-https-only/2117.md?page=2)
