PHX_HOST with port - how to get a simple endpoint like ‘http://localhost:4000’?

for development purposes in a docker container
I want to access my app like

http://localhost:4000

If I set the env var to

PHX_HOST=localhost:4000

then I get the info on startup like (see the brackets)

[info] Access SampleWeb.Endpoint at https://[localhost:4000]

Is there a way to get the brackets away ?

I only found a solution to set in ‘runtime.exc’

url: [host: host, port: 4000, scheme: "http"]

to get it all clean I would have to introduce a new env var ‘PHX_SCHEME’ and read it into a ‘scheme’ variable and then use it like:

url: [host: host, port: port, scheme: scheme]

Question: what is the best way to get a simple endpoint like ‘http://localhost:4000’ ?

1 Like

PHX_HOST is only for the host part of the endpoint (e.g. localhost), not the port. You should be able to set the port with the PORT env variable.

1 Like

Thanks, I thought ‘PHX_HOST’ can also contain a port.

The ‘PORT’ is for starting the server on this port but not the port for the URL generation.
But in a docker container or behind a reverse proxy sometimes the port the server opens is different to the port in the generated URL.

I made it now a bit more explicit so that all my ‘PHX_’ env vars are only used for URL generation and the old ‘PORT’ is still the opened TCP Port.

  phx_host = System.get_env("PHX_HOST") || "example.com"
  phx_port = System.get_env("PHX_PORT") || "443"
  phx_scheme = System.get_env("PHX_SCHEME") || "https"
  port = String.to_integer(System.get_env("PORT") || "4000")

  config :hiverec, HiverecWeb.Endpoint,
    url: [host: phx_host, port: phx_port, scheme: phx_scheme],