plug_proxy v0.3.1 PlugProxy View Source

A plug for reverse proxy server.

PlugProxy pipeline the request to the upstream, and the response will be sent with ranch transport which is highly efficient.

Options

  • :upstream - Upstream URL string. Additional path and query string will be prefixed.
  • :url - URL string or a function which returns an URL.
  • :transport - Transport module. Default to PlugProxy.Transport.Cowboy.

Additional options will be passed to hackney. You can see :hackney.request/5 for available options.

Examples

Forward requests to a upstream.

forward "/v2", to: PlugProxy, upstream: "http://example.com/"
# http://localhost:4000/v2/test => http://example.com/v2/test

forward "/v2", to: PlugProxy, upstream: "http://example.com/abc/"
# http://localhost:4000/v2/test => http://example.com/abc/v2/test

forward "/v2", to: PlugProxy, upstream: "http://example.com?a=1"
# http://localhost:4000/v2/test?b=2 => http://example.com/v2/test?a=1&b=2

Return URL in a function.

get "/v2/:id", do
  url_fun = fn conn, opts ->
    "http://example.com/a/" <> String.reverse(id)
  end

  opts = PlugProxy.init(url: url_fun)
  PlugProxy.call(conn, opts)
end
# http://localhost:4000/v2/123 => http://example.com/a/321

Link to this section Summary

Functions

Callback implementation for Plug.call/2

Callback implementation for Plug.init/1

Link to this section Functions

Callback implementation for Plug.call/2.

Callback implementation for Plug.init/1.