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

Is it possible to have nginx retry a second backend before returning a 502 to the client?

Would something like this work?

Front end:

# haproxy:85 => [a few app servers]:8000
# more specifically:
# haproxy => [nginx => unicorn (502 when busy)]

# Will this try a second app server when the first returns 502?

upstream haproxy {
  server 127.0.0.1:85;
  server 127.0.0.1:85 backup;
}

server {
  listen 80;
  proxy_pass http://haproxy;
  proxy_next_upstream http_502;
}

Back end:

upstream unicorn {
  server unix:/tmp/unicorn.sock fail_timeout=0;
}

server {
  listen 8000;
  proxy_pass http://unicorn;
}

Anyway, I'm just curious. This is actually probably pretty stupid because the retry could end up hitting the same overloaded server and would end up returning a 502 anyway...

share|improve this question
1  
As far as I know, Nginx automatically tries every available server - and if all return an error, it will return the error code of the last response. proxy_next_upstream just seems to offer some finer control (but the default is already any 'error'). (You could test it with a script that logs the time to a file and returns 502 - 2 times should be saved). As per the documentation (the wording could use some work): "If with an attempt at the work with the server error occurred, then the request will be transmitted to the following server and then until all workers of server not are tested." –  cyberx86 Dec 26 '11 at 16:09
    
@cyberx86 ah, thank you! Why didn't you submit that as an answer? –  Aaron Gibralter Dec 26 '11 at 22:30
    
Well, I wasn't able to test the answer out - which made it more speculation supported by documentation (I saw this question last night, but thought that answer was 'too obvious'). Since there don't seem to be any better responses, I will post it as an answer. –  cyberx86 Dec 27 '11 at 4:36
    
Yeah... it doesn't really make sense to retry a unicorn master that has already returned a 502... –  Aaron Gibralter Nov 5 '12 at 6:44
add comment

1 Answer

up vote 2 down vote accepted

As far as I know, Nginx automatically tries every available server - and if all return an error, it will return the error code of the last response.

proxy_next_upstream just seems to offer some finer control as to what errors will be processed and which will be ignored (but the default is already any 'error' in connecting, sending, or receiving - with, I believe, a 60s timeout).

As per the documentation (the wording could use some work):

"If with an attempt at the work with the server error occurred, then the request will be transmitted to the following server and then until all workers of server not are tested. If successful answer is not succeeded in obtaining from all servers, then to client will be returned the result of work with the last server."

You could verify this behaviour with a script that logs the time (or any data) to a file and returns 502. If you find only 1 line logged, then nginx didn't try multiple servers, if you find two lines logged, it did.

share|improve this answer
    
I don't know if I'll have a chance to actually test this out right now, but the logic seems sound. I'll accept this as the answer given that reasoning. –  Aaron Gibralter Dec 27 '11 at 20:39
add comment

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.