Hi,
I’m running a Phoenix application behind a AWS ALB (Application Load Balancer), which routes requests to different applications based on the path in the requested URL.
Requests that start with /myapp
are routed to my application.
The problem is that ALB doesn’t support stripping this /myapp
prefix from the actual URL that goes to Phoenix (differently from nginx, haproxy, etc) and I wouldn’t like to add this prefix (a deployment configuration) to all my routes as it can only be done in compilation time (at least without having to create a router myself which I think would be overkill; I might be wrong though).
In an attempt to overcome this issue, I’ve created a simple plug that runs before everything and removes the prefix from connection’s path_info
. For instance, it turns
["myapp", "assets", "app-978a7188b69b3752fe7c58e50c7e5571.js"]
into
["assets", "app-978a7188b69b3752fe7c58e50c7e5571.js"]
Also, I configure my Endpoint
to use this prefix as the path so URLs generated by the application include the prefix back and are properly routed by ALB.
# runtime.exs
config :myapp, MyAppWeb.Endpoint,
url: [
host: get_env("PHX_HOST", "localhost"),
port: get_env("PHX_PORT", 4000, :int),
path: get_env("PHX_PATH", "/") # get_env is just a helper func
]
It works fine for assets and regular routes, but not for sockets and anything that uses sockets like live reloading and the dashboard. Example:
17:39:09.218 [info] GET /myapp/socket/websocket
17:39:09.280 [debug] ** (Phoenix.Router.NoRouteError) no route found for GET /socket/websocket (MyAppWeb.Router)
(myapp 1.0.0-alpha.19) lib/phoenix/router.ex:405: MyAppWeb.Router.call/2
(myapp 1.0.0-alpha.19) lib/myapp_web/endpoint.ex:1: MyAppWeb.Endpoint.plug_builder_call/2
The plug feels like a hack and it didn’t solve my problem.
Have you guys had to deal with a similar deployment? How did you manage?
Cheers!