Proxy in front of django app

50 views
Skip to first unread message

Maks Materkov

unread,
May 16, 2016, 3:35:25 PM5/16/16
to Django users
Hi everyone!

i' ve got an unusual question, that is probably not very specific to django. We have a large multi-tenant django app, and we want to split all traffic between two tenants (tenant 1 and tenant 2, for now). This tenant will be connected to different databases. Our django app is 100% API, with token authentication. Every request has auth_token parameter (as GET or POST parameter). The token looks like: tenant1.xxxx, tenant-2.yyyy, tenant-3.xxx.

So, we want to route all traffic with tokens tenant1.xxx to app1 and tokens tenant2.yyyy to app2. to do this, we need to setup something like "proxy router" in front of out existing django app. We already using nginx, and we can route to different backends only if token is GET parameter. There is no way to route based on POST params, as far as I know. 

We are discussing several alternatives:

1) Add LUA module to nginx (this module can read POST body data) and perform routing here
2) Write another app (for example, based on Tornado or python3 asyncio) that will be reading POST and GET params and route traffic to appropriate django app. (or another languages? NodeJS (oh, no :) )? Go lang?)

What is the best option here for us? Or some completely different approach?

Thank you!

Erik Cederstrand

unread,
May 16, 2016, 4:16:55 PM5/16/16
to Django Users
The answer to this depends in part on your performance requirements. Do you have *any* control of the client side? If so, you could simply send all API requests to tenant1.api.example.com, tenant2.api.example.com etc.

If you have no control over clients, the most simple solution would be to write a simple Django view that handles every request and passes it on to the correct backend. This keeps your code within Django. But this also requires two trips all the way down your stack and may not yield performance. I've had great success in the past with Nginx scripting, so that would be my next suggestion if the above are not viable. It may cost you a bit more in maintainability, though.

Erik

Maks Materkov

unread,
May 17, 2016, 12:00:33 AM5/17/16
to Django users
Thank you for answer.
Unfortunately, I don't have any control on the client side. All requests are coming to something like "api.example.com" and I need route requests on the server side.

Recently I've found another option. Some "api managment tools" like
and others.

But I think this is an overkill for out task. For now, I think the best option would be to write some lua scripts at the Nginx level.

вторник, 17 мая 2016 г., 1:16:55 UTC+5 пользователь Erik Cederstrand написал:
Reply all
Reply to author
Forward
0 new messages