sreyansjain
Run same phoenix app in multiple locations on the same server
Hi,
I have one phoenix app that I need to run in multiple locations on the same server.
For ex lets say the server is app.example.com
I want to run it on app.example.com/instance1, app.example.com/instance2 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_pass https://app.example.com:4013;
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.
Thanks
Most Liked
NobbZ
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,:pathand:portoptions. All keys except:pathcan be changed at runtime. Defaults to:[host: "localhost", path: "/"]The
:portoption requires either an integer, string, or{:system, "ENV_VAR"}. When given a tuple like{:system, "PORT"}, the port will be referenced fromSystem.get_env("PORT")at runtime as a workaround for releases where environment specific information is loaded only at compile-time.The
:hostoption requires a string or{:system, "ENV_VAR"}. Similar to:port, when given a tuple like{:system, "HOST"}, the host will be referenced fromSystem.get_env("HOST")at runtime.The
:schemeoption accepts"http"and"https"values. Default value is infered from top level:httpor:httpsoption. It is useful when hosting Phoenix behind a load balancer or reverse proxy and terminating SSL there.The
:pathoption can be used to override root path. Useful when hosting Phoenix behind a reverse proxy with URL rewrite rules
NobbZ
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.







