Overview
I deployed a Phoenix app and I’m using the Phoenix path_helpers in situations when I want to write the full URL like this.
<%= Routes.page_url(MyappWeb.Endpoint, :index) %>
Problem
It’s returning the generated fly app name http://foo-acme-700.fly.dev:443 as the url with port 443 but I would prefer it would return my domain name www.example.com.
Question
Where do I make the changes so that my path_helpers is using the domain name I own and not the fly generated app name?
import Config
if System.get_env("PHX_SERVER") && System.get_env("RELEASE_NAME") do
config :myapp, MyappWeb.Endpoint, server: true
end
if config_env() == :prod do
secret_key_base =
System.get_env("SECRET_KEY_BASE") ||
raise """
environment variable SECRET_KEY_BASE is missing.
You can generate one by calling: mix phx.gen.secret
"""
host = System.get_env("PHX_HOST") || "example.com"
port = String.to_integer(System.get_env("PORT") || "4000")
config :myapp, MyappWeb.Endpoint,
url: [host: host, port: 443],
http: [
# Enable IPv6 and bind on all interfaces.
# Set it to {0, 0, 0, 0, 0, 0, 0, 1} for local network only access.
# See the documentation on https://hexdocs.pm/plug_cowboy/Plug.Cowboy.html
# for details about using IPv6 vs IPv4 and loopback vs public addresses.
ip: {0, 0, 0, 0, 0, 0, 0, 0},
port: port
],
secret_key_base: secret_key_base
end
and now is this what the AFTERruntime.exs should look like:
import Config
if System.get_env("PHX_SERVER") && System.get_env("RELEASE_NAME") do
config :myapp, MyappWeb.Endpoint, server: true
end
if config_env() == :prod do
secret_key_base =
System.get_env("SECRET_KEY_BASE") ||
raise """
environment variable SECRET_KEY_BASE is missing.
You can generate one by calling: mix phx.gen.secret
"""
config :myapp, MyappWeb.Endpoint,
http: [port: {:system, "PORT"}],
force_ssl: [rewrite_on: [:x_forwarded_host, :x_forwarded_port, :x_forwarded_proto]],
check_origin: :conn,
secret_key_base: secret_key_base
end
I removed/deleted a lot of things from BEFOREruntime.exs and I just want to make sure I have all the necessary information.
I put the code you suggested in my runtime.exs and I’m getting this error:
Reaped child process with pid: 544, exit code: 0
[info] 00:35:14.011 [error] :force_ssl mismatch for MyAppWeb.Endpoint.
[info] Compile time configuration: nil
[info] Runtime configuration : [rewrite_on: [:x_forwarded_host, :x_forwarded_port, :x_forwarded_proto]]
[info] :force_ssl is a compile-time configuration, so setting
[info] it at runtime has no effect. Therefore you must set it in your
[info] config/prod.exs or similar (not in your config/releases.exs)
Does this mean I need to put your suggestion in both prod.exs AND runtime.exs
I’m not deploying my with Elixir releases. I don’t know if this matter I just followed the tutorial from flyio documentation
I’m running into the same issue of having my route helpers generate urls of the form http://foo-acme-700.fly.dev:443. To me there are two problems here:
The internal app host name is being used
http and :443 are included in the url
Solutions
The internal app host name is being used
Set your PHX_HOST environment variable in fly.toml to the host you want say www.example.com.
http and :443 are included in the url
I still haven’t figured out the proper solution to the second problem. I’m using a phoenix project that was recently generated. I made the changes and saw that the app didn’t work. There were a handful of errors being thrown.