Multiple backend servers accessible from a Flask server

Question:

I want to have a front-end server where my clients can connect, and depending on the client, be redirected (transparently) to another Flask application that will handle the specific client needs (eg. there can be different applications).

I also want to be able to add / remove / restart those backend clients whenever I want without killing the main server for the other clients.

I’d like the clients to:

  • not detect that there are other servers in the backend (the URL should be the same host)
  • not have to reenter their credentials when they are redirected to the other process

What would be the best approach?

Asked By: oulipo

||

Answers:

Sounds like you want a single sign-on setup for a collection of service endpoints with a single entry point.

I would consider deploying all my services as Flask applications with no knowledge of how they are to be architected. All they know is all requests for resources need some kind of credentials associated with them. The manner you pass those credentials can vary. You can use something like the FAS Flask Auth Plugin to handle authentication. Or you can do something simpler like package the credentials provided to your entry service in the HTTP headers of the subsequent requests to other services. Flask.request.headers in your subsequent services will give you access to the right headers to pass to your authentication service.

There are a lot of ways you can go when it comes to details, but I think this general architecture should work for you.

Answered By: Vidya

The front-end server that you describe is essentially what is known as a reverse proxy.

The reverse proxy receives requests from clients and forwards them to a second line of internal servers that clients cannot reach directly. Typically the decision of which internal server to forward a request to is made based on some aspect of the request URL. For example, you can assign a different sub-domain to each internal application.

After the reverse proxy receives a response from the internal server it forwards it on to the client as if it was its own response. The existence of internal servers is not revealed to the client.

Solving authentication is simple, as long as all your internal servers share the same authentication mechanism and user database. Each request will come with authentication information. This could for example be a session cookie that was set by the login request, direct user credentials or some type of authentication token. In all cases you can validate logins in the same way in all your applications.

Nginx is a popular web server that works well as a reverse proxy.

Answered By: Miguel Grinberg
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.