polypush135

polypush135

Forcing https not working on localhost, what am I missing?

I’m trying to force all http request to redirect to https.

In short I have added the below to my endpoint config.

  url: [host: "localhost", port: 80],
  force_ssl: [hsts: true, rewrite_on: [:x_forwarded_proto], host: "localhost"]

I’ve tried just simply force_ssl: [hsts: true] with no luck as well.

Apps:

:phoenix, "1.4.10"
:plug, "1.8.3"

Browsers:

Chrome: Ubuntu Version 78.0.3904.97 (Official Build) (64-bit)

When making a http request the request is successful but it never redirects to https.

Heres the whole config.

config :morphic_pro, MorphicProWeb.Endpoint,
  http: [port: 4000],
  https: [
    port: 4001,
    cipher_suite: :strong,
    certfile: "priv/cert/localhost.crt",
    keyfile: "priv/cert/localhost.key"
  ],
  debug_errors: true,
  code_reloader: true,
  check_origin: false,
  watchers: [
    node: [
      "node_modules/webpack/bin/webpack.js",
      "--mode",
      "development",
      "--watch-stdin",
      cd: Path.expand("../assets", __DIR__)
    ]
  ],
  url: [host: "localhost", port: 80],
  force_ssl: [hsts: true, rewrite_on: [:x_forwarded_proto], host: "localhost"]

I have read over https://hexdocs.pm/phoenix/1.4.10/endpoint.html#using-ssl and was a little confused about the note about making https requests on 4000. I assume that maybe a typo that the protocol was https not http

EDIT: I now understand the point of the comment around being redirected from http to https but still keeping the port as 4000 but I’ve yet to see that since I don’t seem to be redirecting any of my requests.

Marked As Solved

polypush135

polypush135

whoot! finally!. Ok so it looks like I have to use the host option too.

  force_ssl: [hsts: false, rewrite_on: [:x_forwarded_proto], exclude: [], host: "localhost:4001"]

Also Liked

NobbZ

NobbZ

The example config just has force_ssl: :hsts, not even wrapped in a list.

PS: having HSTS for localhost is nasty… You shouldn’t activate it before you know how to remove it from your browser after wards…

NobbZ

NobbZ

Also please read the full section over HSTS I linked above, HSTS will only work for port 80 according to that page.

antew

antew

I believe localhost is excluded from the SSL plug by default, check out https://github.com/elixir-plug/plug/blob/master/lib/plug/ssl.ex#L46

NobbZ

NobbZ

HSTS is a bad idea for localhost, as it will require you to provide HTTPS for all applications that you happen to develop on that computer, also for some browsers you won’t be able to use self signed or outdated certificates along with HSTS.

They become very strict once HSTS was activated for a domain.

Where Next?

Popular in Questions Top

bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
LegitStack
I’m hoping you guys can give me some general advice and perhaps code examples if you’re feeling up to it. I’m very interested in Elixir,...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
gonzofish
I’m currently trying to understand how to join three tables using Ecto. All the examples I’ve seen use 2, so maybe I’m just missing somet...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New

Other popular topics Top

sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement