Strayer
I have a Phoenix application that should be accessible in the local network and remotely via a reverse proxy. This means I can’t configure a URL in the Endpoint config, since the client did either use something like hostname.local or reverseproxy.example.tld.
I can easily override the Router URL with a simple Plug that calls Phoenix.Controller.set_router_url/2:
def set_router_url_by_host_header(conn, _opts) do
allowed_hosts = Application.get_env(:my_app, :allowed_hosts, [])
requested_host = conn.host
if requested_host in allowed_hosts do
Phoenix.Controller.put_router_url(conn, requested_host)
else
conn
end
end
This can’t be done for URLs generated with Routes.static_path though, since they always use the url or static_url configured in the Application env.
As far as I can see there is nothing to override this behavior with a plug, or am I missing something here?
Related StackOverflow question: https://stackoverflow.com/questions/52091185/how-can-i-configure-phoenix-to-use-the-clients-host-header-as-a-base-url-for-sta
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
easco
A Phoenix application can have more than one Endpoint. Can you use one for each of the domains you are supporting? You will have to run the endpoints on different ports, but you should be able to use the reverse proxy on the remote network to hide that.
LostKobrakai
I’d still be curious about a non static option to have the router helpers just use the incoming host (if used with a conn not just an endpoint). Sometimes there’s just the need for a site work under whatever address I can be reached, no matter what IP or domain it might be.
josevalim
The first argument to static_url is a struct, that can either be a connection struct or a URI struct. So the trick is to build a URI struct with the information of the domain and pass it to
static_urlinstead of theconn.LostKobrakai
https://github.com/phoenixframework/phoenix/blob/bb0d91fa9abf0b7a90007e12ac72d8933c04870d/lib/phoenix/router/helpers.ex#L155-L168
I knew this is the case for the path helpers, but it seems to be missing for the static path helpers. Probably because it needs to know where to mount static assets from the endpoint itself.
But I just notices if the domain is really unknown just using a plain path should really be the solution.
josevalim
Oh, I see. Good point.
Strayer
Something like
Phoenix.Controller.put_static_urlwould solve my problem easily. Then I can simply override the URL just like the router URL.rmoorman
Why not use
static_pathinstead ofstatic_urlin this case @Strayer? What would be your use-case of complete urls instead of just a path? (not that I would be opposed to that kind of flexibility)Strayer
Because static_path generates full URLs with the configured domain name, that is the problem. See the linked StackOverflow question in my original post for a detailed example.
rmoorman
I am wondering because when I run a vanilla phoenix installation using
mix phx.newI get an application which is usingstatic_pathand even though it is configured for prod with a url pointing toexample.com, the HTML output still contains an absolute path without a domain name.When I add
static_urlto the templates though, a full url is generated instead including the configured domain.Have a look at the example code over here.
It basically is the phoenix app generated by default which has two additions within
lib/dingen_web/templates/layout/app.html.eex. I added two image tags there (with the logo). One withstatic_pathand one withstatic_url.When in development mode, this gives me the following html (in dev no url is configured in
config/dev.exsso the html points to the inferred localhost):When running in production mode (after a quick
mix phx.digest), the url configured inconfig/prod.exsis used:If you are experiencing different results in your project, would you mind sharing some example code?