How to forward an HTTP/S request (i.e. use Phoenix as proxy)

963 views
Skip to first unread message

Brad O'Hearne

unread,
May 26, 2016, 3:11:36 PM5/26/16
to phoenix-talk
Hello, 

I need to build a Phoenix web app which acts as a proxy to a number of other Phoenix web apps on different servers. I'm looking for standard proxy behavior: 
  • Receives an HTTP (or HTTPS) request. 
  • Does some processing on the request. 
  • Either forwards (not redirects) the request to another web app, or rejects the HTTP request.
  • Returns the HTTP response back to the sender. 
I'd like to address this question primarily as true proxy behavior (one server to a separate server), but I'd also be interested in hearing about interception / filtering of web requests within the same server (though that is not going to solve my problem, but it is related). Most web servers I've seen have a means to inject / intercept / filter the request call chain within the server (I'm thinking Plug), so if anyone wants to comment on that, that would be great. But the primary question is proxying and specifically forwarding a request to another web app, and then returning the response back.

If anyone can give guidance, it would be greatly appreciated. 

Thanks, 

Brad 

benwil...@gmail.com

unread,
May 26, 2016, 5:08:00 PM5/26/16
to phoenix-talk
I suppose my first question here is why? There are plenty of dedicated proxies that operate at the appropriate layer of abstraction. Why use phoenix for the proxy?

Brad O'Hearne

unread,
May 26, 2016, 5:13:49 PM5/26/16
to phoenix-talk, benwil...@gmail.com
On Thursday, May 26, 2016 at 2:08:00 PM UTC-7, benwil...@gmail.com wrote:
I suppose my first question here is why? There are plenty of dedicated proxies that operate at the appropriate layer of abstraction. Why use phoenix for the proxy?

Thanks for the reply. The answer is that I need to add custom behavior to the proxy, which perform a number of actions on every request, and also determines based on custom logic whether the request is forwarded on, or bounced back to the requester. 

Brad 

Jordan Day

unread,
May 27, 2016, 11:47:20 AM5/27/16
to phoenix-talk
Around 18 months or so I wrote a mostly-working reverse proxy in Elixir, but I relied directly on Cowboy (the underlying webserver in phoenix) -- I'm not sure that adding phoenix to the mix actually makes things substantially easier?

My advice would actually be to use one of the existing systems like nginx or haproxy, and learn their scripting techniques for customizing behavior (for example, you can write lua scripts to create custom behavior for nginx). The reason for this is that you'll find yourself spending days and days making all the basic HTTP stuff work, and then suddenly find you need to support websockets, too! And then someone will tell you they need SSE support. Repeat ad infinitum! Maintaining a proxy ends up being a full time job, so unless you're prepared for that (or you can definitively nail down the scope of what your proxy needs to support), it might be more work than it's worth.

The stuff I wrote a while back is open source, available here: https://github.com/openaperture/router. I wrote it while still a relative newbie with elixir/erlang, so a lot of the general design isn't great, and it seems to run afoul of the dreaded erlang binary leak problem (http://dieswaytoofast.blogspot.com/2012/12/erlang-binaries-and-garbage-collection.html) on occasion, so you might view that code more from a how-not-to-do-it perspective. Good luck!

Brad O'Hearne

unread,
May 27, 2016, 5:51:37 PM5/27/16
to phoenix-talk
On Friday, May 27, 2016 at 8:47:20 AM UTC-7, Jordan Day wrote:
I'm not sure that adding phoenix to the mix actually makes things substantially easier?

I'd love to use something out of the box. But I need a UI / management console for proxy / auth / custom configuration and management....so I was looking at having to create a web app anyway.  

B

Ben Wilson

unread,
May 27, 2016, 6:15:33 PM5/27/16
to phoenix-talk
You could build an application that generates haproxy / nginx configs and then restarts those applications. So long as your routes aren't changing on the fly crazy rapidly (many times a second) I would imagine that approach could work.

--
You received this message because you are subscribed to a topic in the Google Groups "phoenix-talk" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/phoenix-talk/5xZw4PVReOY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to phoenix-talk...@googlegroups.com.
To post to this group, send email to phoeni...@googlegroups.com.
Visit this group at https://groups.google.com/group/phoenix-talk.
To view this discussion on the web visit https://groups.google.com/d/msgid/phoenix-talk/8963eb08-a68a-4299-ae60-ca2023e4ecaf%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Scott Ribe

unread,
May 28, 2016, 10:44:51 AM5/28/16
to phoeni...@googlegroups.com
On May 27, 2016, at 4:15 PM, Ben Wilson <benwil...@gmail.com> wrote:
>
> You could build an application that generates haproxy / nginx configs and then restarts those applications. So long as your routes aren't changing on the fly crazy rapidly (many times a second) I would imagine that approach could work.

Perhaps the word "proxy" has thrown this conversation off-track. I think you're not really understanding how much processing might be done by a request that some of us would still refer to as "proxying" to one or more backend services. There's no way I'd want to cobble together nginx configs and a batch of Lua scripts to process headers, query params, form data, make decisions, post-process & reformat returned data, etc.

--
Scott Ribe
scott...@elevated-dev.com
http://www.elevated-dev.com/
https://www.linkedin.com/in/scottribe/
(303) 722-0567 voice





Reply all
Reply to author
Forward
0 new messages