How to add a prefix to a Phoenix app's /assets/ path at build+deployment time?

I am using nginx, serving two Phoenix apps:

  upstream phoenix1 {
    server 127.0.0.1:4001 max_fails=5 fail_timeout=60s;
    keepalive 32;
  }

  upstream phoenix2 {
    server 127.0.0.1:4002 max_fails=5 fail_timeout=60s;
    keepalive 32;
  }

  server {

     ...

    location /p1 {
      proxy_pass http://phoenix1/;
      proxy_http_version 1.1;
      proxy_set_header X-Cluster-Client-Ip $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      access_log /var/log/p1.access.log;
      error_log /var/log/p1.error.log;
    }

    location /p2 {
      proxy_pass http://phoenix2/;
      proxy_http_version 1.1;
      proxy_set_header X-Cluster-Client-Ip $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header Host $http_host;
      proxy_redirect off;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      access_log /var/log/p2.access.log;
      error_log /var/log/p2.error.log;
    }
    ...
}

I then launch the app and browse to /p1 and /p2 successfully; the gist of your typical Phoenix app is served. Assets however are not; they are expected at, in both cases, /assets/.

Is there a way at build+deployment time to prefix each project’s asset path by, say /resources1 and /resources2, respectively ?

Thanks.

Take a look at the :url (and :static_url) settings for your endpoint. Likely it’s not just your assets, but also generated links, which are wrong.

https://hexdocs.pm/phoenix/1.7.7/Phoenix.Endpoint.html#module-endpoint-configuration

2 Likes

Thanks. I ended up editing config/runtime.exs , which IIUC is one good approach for such things:

import Config
...
case config_env() do
:dev ->
  config :hello_phoenix_2, HelloWeb.Endpoint, url: [path: "/p2"]

:prod ->
  ...

This, coupled to the project specific’s assets location in nginx.conf, works nicely:

    ...
    location /p2/assets {
      alias $abs_path_to_projects/hello_phoenix_2/priv/static/assets;
    }
    ...