S
S
sokol12016-05-20 11:38:07
Nginx
sokol1, 2016-05-20 11:38:07

How to set up nginx proxying according to the scheme user id=resource?

The task is as follows:
There are several servers, each of which has its own users. It is required to make one entry point (one host) for all users. URLs like http://mycloud.com/{user-id}/resource are used. Depending on the user-id, the request should be routed to a specific backend server. The list of users and their corresponding servers is stored in the database.
I want to use nginx for routing, but I don't know how to configure it. After all, if you specify location / proxy_pass for each user separately, then this will be a sooooo big config! Is there another way?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton B, 2016-05-20
@bigton

You can use map to define the backend.
I use the cookie to determine which server to send requests to:

upstream backend {
    least_conn;
    server 1.1.1.1;
    server 2.2.2.2;
    server 3.3.3.3;
}

map $cookie_server $upstream {
    default backend;
    1 1.1.1.1;
    2 2.2.2.2;
    3 3.3.3.3;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question