Strayer

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

Showing Posts 1 to 9

easco

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

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

josevalim

Creator of Elixir

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_url instead of the conn.

LostKobrakai

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

josevalim

Creator of Elixir

Oh, I see. Good point. :thinking:

Strayer

Strayer OP

Something like Phoenix.Controller.put_static_url would solve my problem easily. Then I can simply override the URL just like the router URL.

rmoorman

rmoorman

Why not use static_path instead of static_url in 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

Strayer OP

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

rmoorman

I am wondering because when I run a vanilla phoenix installation using mix phx.new I get an application which is using static_path and even though it is configured for prod with a url pointing to example.com, the HTML output still contains an absolute path without a domain name.
When I add static_url to 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 with static_path and one with static_url.

When in development mode, this gives me the following html (in dev no url is configured in config/dev.exs so the html points to the inferred localhost):

      <p>
        <img src="/images/phoenix.png" alt="Phoenix Framework Logo"/>
      </p>
      <p>
        <img src="http://localhost:4000/images/phoenix.png" alt="Phoenix Framework Logo"/>
      </p>

When running in production mode (after a quick mix phx.digest), the url configured in config/prod.exs is used:

      <p>
        <img src="/images/phoenix-5bd99a0d17dd41bc9d9bf6840abcc089.png?vsn=d" alt="Phoenix Framework Logo"/>
      </p>
      <p>
        <img src="http://example.com/images/phoenix-5bd99a0d17dd41bc9d9bf6840abcc089.png?vsn=d" alt="Phoenix Framework Logo"/>
      </p>

If you are experiencing different results in your project, would you mind sharing some example code?

— All posts loaded —

Where Next? Top

Trending in Questions Top

katta
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
achenet
Hello, I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind. However, when I launch mix phx.server, I get an error...
New
kpanic
Hi everyone, I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding. I sta...
New
asweet-confluent
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
Cxx-mlr
I’m working on a small exercise involving update_in/3, and I came up with this solution: data = %{ name: "Periodic Table", category:...
New
ChrisAmelia
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication): toke...
New

Other Trending Topics Top

GenericJam
Edit: 2026 May 15 - This post is archived. Mob is alive!! Main docs: mob v0.7.11 — Documentation A bit of explanation for the slightly c...
New
JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
budgie
A little off-topic, but I feel like people here have a good head on their shoulders. I used to be quite good at making software. Was luc...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews