1

I have a setup where my website content is being served by mirrors also, but because my own traffic is strictly limited, I want to direct request (not proxy) to mirrors whenever they are online.

How can I do this with nginx? I already found this which seems quite close, but it seems to decide randomly for one of the servers and does not offer to serve the data from the host when the mirrors are offline.

bonanza
  • 77
  • 5

1 Answers1

1

Nginx also provides least_conn and weighted connections (there is also ip_hash for session persistence) so your backends can be configured like this:

upstream mybackend {
    least_conn;
    server host1.domain.com;
    server host2.domain.com;
    server host3.domain.com;
}

or with weighting:

upstream mybackend {
    server host1.domain.com weight=5;
    server host2.domain.com;
    server host3.domain.com;
}

In recent versions of nginx, weighting can also be used with least_conn and ip_hash.

Simon Greenwood
  • 1,363
  • 9
  • 12
  • Thanks a lot! How can I combine this with the `return` command to actually perform the redirection to the mirrors? – bonanza Dec 07 '17 at 09:08
  • 1
    The nginx upstream module returns `$upstream_addr` so in theory (and I haven't tested this so I won't update the answer just yet) you could have `location / { return 302 $upstream_addr; }` However, I would also suggest that a transparent proxy is a better solution as even returning a 302 rather than a 301 will almost invariably end up with users (or browsers) caching a response leading to a loss of control of your incoming traffic. – Simon Greenwood Dec 07 '17 at 09:46
  • Thanks for your comment. But how to ensure that the `return` only happens for the case when the host address is e.g. localhost (serving from local directory)? – bonanza Dec 07 '17 at 15:43
  • You could try setting a high weight for localhost and lower weights for other mirrors. However, with a requirement like this you are going to end up using `map` to define actions, or some kind of dynamic load balancing. However, if your site and mirrors are identical, why not just spread load? If your primary is restricted by bandwidth, true load balancing will help. – Simon Greenwood Dec 07 '17 at 16:27