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.

1 Answer 1

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.

4
  • Thanks a lot! How can I combine this with the return command to actually perform the redirection to the mirrors?
    – bonanza
    Dec 7, 2017 at 9: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. Dec 7, 2017 at 9: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 7, 2017 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. Dec 7, 2017 at 16:27

You must log in to answer this question.

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