Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I am attempting to set up a test Nginx load balanced environment. So far I have sucessfully configured a load balancer nginx-balancer1 and 3 servers to serve webpages nginx1, nginx2 & nginx3.

I want to balance the load by region depending on the visitor's IP. I have configured my Nginx nginx-balancer1 to use the Maxmind GeoIP Country data.

So here is my configuration to the upstream servers:

# Check where the user is coming from
server {
  location / {
    if ($geoip_city_continent_code = "EU") { proxy_pass http://ams1; }
    if ($geoip_city_continent_code = "NA") { proxy_pass http://sfo1; }
    if ($geoip_city_continent_code = "AS") { proxy_pass http://sgp1; }
}
}
# Define upstream servers
upstream ams1 { server server1.example.com; }
upstream sfo1 { server server1.example.com; }
upstream sgp1 { server server1.example.com; }

This seems to work well, however if I shutdown nginx on say ams1 (server1.example.com) and try to go to the main page I receive a 502 Bad Gateway error.

What I want to figure out is if a server is down, how can I get nginx-balancer1 to redirect to another server, either the next closest or the next functioning server.

Can anybody help?

Thanks

share|improve this question

1 Answer 1

It sounds like what you're looking for is proxy_next_upstream and proxy_connect_timeout. So you'd then do something like:

proxy_next_upstream     error timeout invalid_header http_500;
proxy_connect_timeout   2;

Basically if a failure is detected the backend will be marked as down for x seconds and it will try again. Nginx will try the next entry in the upstream block, so yonce the downed upstream comes back up it should automatically be re-enabled in the upstream pool.

share|improve this answer
    
Thanks for your answer sadly it doesn't work with my configuration as I am still seeing 502 errors. These directives should be added inside my server { location / { #HERE }} correct? –  Daniel Pilch Feb 26 at 17:12
    
correct on the location in your nginx conf. What version of nginx are you on? If your upstream is returning a 502 make sure you're using --> http_502 (instead of http_500 from my example earlier). –  xxdesmus Feb 26 at 18:07
    
Yeah I tried with http_502 but it hasn't worked :/ I'm using nginx version: nginx/1.1.19. Here is a pastebin of my 'default' file in sites-available if it helps: pastebin.com/mi45hUXU –  Daniel Pilch Feb 26 at 21:29
    
I don't have this kind of setup in place on my side so it's kinda hard to make a definitive suggestion for a fix, but might be worth reviewing: andrewparisio.com/2011/02/… based on that they seem to show you should specify max_failes and fail_timeout per server such as --> max_fails=3 fail_timeout=31s; –  xxdesmus Feb 26 at 22:36
    
So yeah I had a look at the link and attempted to change my configurations to use max_fails & fail_timeout but I'm still getting the 502 error. Very strange indeed. I've checked the nginx logs but nothing screams out at me failed (111: Connection refused) while connecting to upstream. –  Daniel Pilch Feb 27 at 11:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.