Run same phoenix app in multiple locations on the same server


I have one phoenix app that I need to run in multiple locations on the same server.
For ex lets say the server is
I want to run it on, and so on.
I have an nginx server in front of the phoenix app and have nginx blocks like so -
location /instance1 {
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto https;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection “Upgrade”;
proxy_redirect off;
Similar for instance2, 3 etc.

For the phoenix app I need to have all routes begin with /instance1 and all static assets served from /instance1
This works well when I have only one instance. If i build a release using mix release and want to copy the instances, it doesn’t work.
I am trying to put something like below in the config/releases.exs file and use the same in routes

config :app_name,
  app_prefix: "/instance1"

for routes

scope "#{Application.get_env(:app_name, :app_prefix)}/admin/", AppNameWeb do

But it seams that the routes are only generated at compile time and not at runtime.
Is there a possibility of generating the routes at runtime using a config setting?

Sorry for the long post, but did not understand how else to put it.
Help would be deeply appreciated.


You probably want to configure your endpoint correctly using the :url option of the endpoint configuration:

:url - configuration for generating URLs throughout the app. Accepts the :host , :scheme , :path and :port options. All keys except :path can be changed at runtime. Defaults to:

[host: "localhost", path: "/"]

The :port option requires either an integer, string, or {:system, "ENV_VAR"} . When given a tuple like {:system, "PORT"} , the port will be referenced from System.get_env("PORT") at runtime as a workaround for releases where environment specific information is loaded only at compile-time.

The :host option requires a string or {:system, "ENV_VAR"} . Similar to :port , when given a tuple like {:system, "HOST"} , the host will be referenced from System.get_env("HOST") at runtime.

The :scheme option accepts "http" and "https" values. Default value is infered from top level :http or :https option. It is useful when hosting Phoenix behind a load balancer or reverse proxy and terminating SSL there.

The :path option can be used to override root path. Useful when hosting Phoenix behind a reverse proxy with URL rewrite rules

1 Like

I am trying permutations and combinations with the url.
I am able to change the port no etc at runtime so if I copy over a release I am able to get multiple running apps on different ports.

The problem comes during configuration with nginx proxy. I think to work in a subfolder phoenix routes need to being with the subfolder and all static assets need to be served from the subfolder also.

At the moment I can get that to work by creating multiple releases with different subfolders, but I want to automate this process so that I don’t need to create multiple releases. Just copying the release and changing the runtime config to get the app rolling.

Will post the updates here.
Thanks for the help.

Well, :path is explicitely named as not beeing runtime configurable.

Also you can not change routes via a runtime parameter, as they need to be compiled into literal binary pattern matches.

Currently I do see no possibility to do what you want.

1 Like

Seems like compiling multiple times is the only way forward for now. Thank you for your time and help.

Would Multiple Endpoints in Phoenix help you to achieve what you want?

Take also a look into this thread What is benefit of having multiple phoenix endpoints.

Seems the link is dead, but waybackmachine as a copy of it: