0

I have set up some upstream server in nginx. I want to route request to backend server by url. e.g. first user choose: /a go to server1 /b go to server2

if other user use: /a, he will route to server1 too.

so I guess we need to store a mapping table on nginx.

how to do it in nginx ?

1

1 Answer 1

0

If you want to route /a to serverA and /b to serverB you may need to configure to separate upstreams. So your configuration may be like:

upstream upstreamA {
 server serverA;
}
upstream upstreamB {
server serverB ;
}
server {
location /a {proxy_pass http://upstreamA ;}
location /b {proxy_pass http://upstreamB ;}
}
5
  • but we may have lots of URL /a /b /c /d /e ... etc I want do is: when a user enter a URL for the first time. the nginx will choose a backend server for this URL. Then Nignx will store a lookup table for the mapping. next time when another user will this URL. it will route to the same backend server
    – lixiran
    Commented Sep 6, 2019 at 14:26
  • if all of them should point to different servers you will need to use a separate upstream. upstream allow you to combine servers that will expose same application. E.g. in case you want to have 2 servers for HA purposes for /a Commented Sep 6, 2019 at 14:28
  • but the URL will be infinity. in our case, the URL is determined by Users. we have a pool of server. each run the same application. we will randomly select a server for a incoming URL. just need to make sure when the URL come again. it route to the same server
    – lixiran
    Commented Sep 6, 2019 at 14:33
  • In my understanding it is not common case if you want this to be persistent. Nginx may be extended with lua and you may have your own balancer script. If after user relogin it may be routed to another server you may configure sticky sessions (for example with ip_hash). But server may be changed from time to time. It will depends on nginx restart or backend failure. Commented Sep 6, 2019 at 14:50
  • I do a simple test today. I just hash the request URL. then use it for load balancing the upstream. the good news is that, the same URL has the same hash code so it will route the same server. the bad news is the balancing is not distributed equally. 70% on 1 server, 30% on the other 2 servers
    – lixiran
    Commented Sep 6, 2019 at 15:18

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .