nginxload-balancingmirror

Nginx: Call all upstreams at once


How I could call all upstreams at once and return a result from first that respond and the response will not be a 404?

Example: Call to load balancer at "serverX.org/some-resource.png" creates two requests to:

srv1.serverX.org/some-resource.png srv2.serverX.org/some-resource.png

srv2 responds faster and the response is shown to the user. Is this possible at all? :)

Thanks!


Solution

  • Short answer, NO. You can't do exactly what you described with nginx. Come to think of it a bit, this operation can't be called load balancing since the whole back-end gets the total amount of traffic.


    A good question is what do you think that you could accomplish with that? Better performance?

    You can be sure that you will have better results with simple load balancing between your servers since the will have to handle the half of the traffic.

    In case that you have a more complex architecture i.e. different loads from different paths to your backend servers we could discuss a more sophisticated load balancing method.


    So if your purpose is sth else than performance there are some things that you can do:

    1) After you sent the request to first server you can send it using the post_action to another one.

    location ~ ^/*.png {
        proxy_pass http://srv1.serverX.org;
        ...
    
        post_action @mirror_to_srv2;
        ...
    }
    
    location @mirror_to_srv2 {
        proxy_ignore_client_abort on;
        ...
        proxy_pass http://srv2.serverX.org;
    }
    

    2) The request is available to you in nginx as a variable so with some lua scripting you can send it where ever you want.

    Note that the above methods are not useful to tackle performance issues but to enable you to do things like mirroring live traffic to dev servers for test/debug purposes.

    Last this one seems to provide the functionality you want but remember that isn't built for the use that you seem to have in mind.