Rectangle 27 0

php How to direct the response from a call to an external API back to the requesting web server in a load balanced (nginx) environment?

The downside: sometimes one of the servers might get very busy but you need to benchmark the solution and find where the problem might be.

We already use ip_hash so that clients get directed to the same web server each time. This does not help when our servers need to fetch data from an external API however. The API provider's IP might be directed to the same web server each time but any of our web servers could have made that request initially. e.g. all API returns might get directed to web1 based on the API provider's IP being the same, but the request might have come from web1, web2 or web3

can you please explain why do you need the API answer to be redirected to the same backend server? I assume you may actually receive the answer on any other server and fetch the response internal. But this is more an architecture issue and it's hard to provide a solution without actually knowing what happens in your application.

now I understand! You don't know what web-server initiated the API call, so how can you redirect the API return to the proper one without knowing which one is actually? One dirty solution: use different upstreams but I need to think about how to actually implement it.