PhoenixReverseProxy (PhoenixReverseProxy v1.0.3)

This Library provides everything you need to route requests and websockets from one proxy app into your umbrella. This allows you to have multiple phoenix applications in the same umbrella sharing the same port number.

Start by creating a Phoenix application in your umbrella apps folder:

(cd apps && mix phx.new --no-webpack --no-ecto --no-html --no-gettext --no-dashboard reverse_proxy)
# Optionally you can delete unused files
rm -rf apps/reverse_proxy/lib/reverse_proxy_web/{channels,controllers,views}

In apps/reverse_proxy/lib/reverse_proxy_web/endpoint.ex replace the contents with this and replace the default endpoint and add appropriate endpoints:

defmodule ReverseProxyWeb.Endpoint do
  use PhoenixReverseProxy, otp_app: :reverse_proxy

  # IMPORTANT: All of these macros except for proxy_default/1
  #            can take a path prefix so they all have an arity
  #            of 2 and 3.

  # Maps to http(s)://api.example.com/v1
  # proxy("api.example.com", "v1", ExampleApiV1.Endpoint)

  # Maps to http(s)://api.example.com/v2
  # proxy("api.example.com", "v2", ExampleApiV2.Endpoint)

  # Matches the domain only and no subdomains
  # proxy("example.com", ExampleWeb.Endpoint)
  # Matched any subdomain such as http(s)://images.example.com/
  # but not the domain itself http(s)://example.com/
  # proxy_subdomains("example.com", ExampleSubs.Endpoint)

  # Matches all subdomains and the domain itself.
  # This is equivalent to combining these rules:
  #   proxy("foofoovalve.com", FoofooValve.Endpoint)
  #   proxy_subdomains("foofoovalve.com", FoofooValve.Endpoint)
  # proxy_all("foofoovalve.com", FoofooValve.Endpoint)

  # Matches anything not matched above
  proxy_default(ExampleWeb.Endpoint)
end

In your config you must disable your other Phoenix applications from listening on TCP ports. In your current endpoint configuration make the following modifications.

# Configures the endpoint
config :example_web, ExampleWeb.Endpoint,
  # Add this
  server: false,
  # Remove this and add it to the proxy endpoint configuration
  http: [port: System.get_env("PHX_PORT") || 4000],
  ...

Move contents of the files apps/reverse_proxy/config/*.exs to the corresponding config/*.exs config files.

Link to this section Summary

Functions

Sets the Phoenix endpoint for a specific hostname. Note that more specific matches are matched first regardless of the order in which they are specified.

Sets the Phoenix endpoint for a specific hostname and first component of the path. Note that more specific matches are matched first regardless of the order in which they are specified.

Sets the Phoenix endpoint for a set of subdomains including the domain itself. Note that more specific matches are matched first regardless of the order in which they are specified.

Sets the Phoenix endpoint for a set of subdomains including the domain itself as well as the first component of the path. Note that more specific matches are matched first regardless of the order in which they are specified.

Sets the default Phoenix endpoint to send requests to if we cannot match against anything.

Sets the Phoenix endpoint for a specific first component of a path. Note that more specific matches are matched first regardless of the order in which they are specified.

Sets the Phoenix endpoint for a set of subdomains excluding the domain itself. Note that more specific matches are matched first regardless of the order in which they are specified.

Sets the Phoenix endpoint for a set of subdomains excluding the domain itself as well as the first component of the path. Note that more specific matches are matched first regardless of the order in which they are specified.

Reverse a domain name string (ASCII). This is used internally for pattern matching of subdomains.

Link to this section Functions

Link to this macro

proxy(hostname, endpoint)

(macro)

Sets the Phoenix endpoint for a specific hostname. Note that more specific matches are matched first regardless of the order in which they are specified.

Examples

proxy("example.com", ExampleWeb.Endpoint)
proxy("example.com", "oauth", OAuthWeb.Endpoint) # This matches first
Link to this macro

proxy(hostname, path_prefix, endpoint)

(macro)

Sets the Phoenix endpoint for a specific hostname and first component of the path. Note that more specific matches are matched first regardless of the order in which they are specified.

Examples

proxy("example.com", ExampleWeb.Endpoint)
# For http(s)://example.com/oauth
proxy("example.com", "oauth", OAuthWeb.Endpoint) # This matches first
Link to this macro

proxy_all(hostname, endpoint)

(macro)

Sets the Phoenix endpoint for a set of subdomains including the domain itself. Note that more specific matches are matched first regardless of the order in which they are specified.

Examples

proxy_all("example.com", OAuthWeb.Endpoint)
# Which is equivalent to:
proxy("example.com", OAuthWeb.Endpoint)
proxy_subdomains("example.com", OAuthWeb.Endpoint)
Link to this macro

proxy_all(hostname, path_prefix, endpoint)

(macro)

Sets the Phoenix endpoint for a set of subdomains including the domain itself as well as the first component of the path. Note that more specific matches are matched first regardless of the order in which they are specified.

Examples

proxy_all("example.com", "oauth", OAuthWeb.Endpoint)
# Which is equivalent to:
proxy("example.com", "oauth", OAuthWeb.Endpoint)
proxy_subdomains("example.com", "oauth", OAuthWeb.Endpoint)
Link to this macro

proxy_default(endpoint)

(macro)

Sets the default Phoenix endpoint to send requests to if we cannot match against anything.

Examples

proxy_default(ExampleWeb.Endpoint)
Link to this macro

proxy_path(path_prefix, endpoint)

(macro)

Sets the Phoenix endpoint for a specific first component of a path. Note that more specific matches are matched first regardless of the order in which they are specified.

Examples

proxy_path("auth", AuthWeb.Endpoint)
Link to this macro

proxy_subdomains(hostname, endpoint)

(macro)

Sets the Phoenix endpoint for a set of subdomains excluding the domain itself. Note that more specific matches are matched first regardless of the order in which they are specified.

Examples

proxy_subdomains("example.com", ExampleWeb.Endpoint)
Link to this macro

proxy_subdomains(hostname, path_prefix, endpoint)

(macro)

Sets the Phoenix endpoint for a set of subdomains excluding the domain itself as well as the first component of the path. Note that more specific matches are matched first regardless of the order in which they are specified.

Examples

proxy_subdomains("example.com", "oauth", OAuthWeb.Endpoint)
Link to this function

reverse_domain(domain)

Reverse a domain name string (ASCII). This is used internally for pattern matching of subdomains.

Examples

iex> PhoenixReverseProxy.reverse_domain("abc.com")
"moc.cba"