I have two nginx servers which are serving images: server Foo and Bar. I want to set up load balancing in a way that every other request server Foo receives is redirected to server Bar. I read up on load balancing in nginx documentation and it seems I should define upstream section on server Foo like this:

upstream imgserver {
    server localhost;
    server server-bar.com;

location / {
    proxy_pass http://imgserver;

Now, I suspect that this configuration will yield that only server Bar serves the images, since whenever server Foo receives a requests it will try to proxy it again. Is that correct? If so, how do I set this up correctly?

Do I need to use another port for Foo redirection? Or maybe add a custom header on redirection?

  • 1
    Why do you put server localhost then? that is not the server you are trying to proxy to! Mar 7 '19 at 22:02
  • @MichaelHampton Because I want 50% of requests to be served by localhost and 50% by server-bar
    – Marko
    Mar 7 '19 at 22:04
  • But this system will serve 100% of requests! and pass on 50% of them. You could make a new server block but it doesn't seem to have any point. Mar 7 '19 at 22:11
  • @MichaelHampton It will serve 100% of HTTP requests, but for only 50% of them it will actually fetch the image from disk and return it to the client. The idea is to reduce disk load, maybe I should have clarified it further.
    – Marko
    Mar 7 '19 at 22:23
  • OK, then it kind of makes sense. But of course you need a copy of all the images on the other server, and a diferent nginx server block to actually serve the files locally. Mar 7 '19 at 22:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.