Welcome! Log In Create A New Profile

Advanced

Upstream question

Posted by chrisyxg 
Upstream question
January 09, 2013 09:03PM
For test purpose, I have 2 backend tomcat running on one box, the nginx is running on separate box on CentOS.
I have a nginx conf like this:


upstream backend {
#ip_hash;
server 10.10.10.120:8081; # this has a jsp page say "tomcat 1"
server 10.10.10.120:8080; # this has a jsp page say "tomcat 2"
}


While I stop the 1st tomcat and keep 2nd one alive, I go to my nginx server url, http://10.10.10.121/HelloWorld/HelloWorld.jsp and refresh manually, I can see the page content "tomcat 2", so far so good, then I refresh again, the page is loading...and loading... instead of show content "tomcat 2" (since 1st tomcat is down), it should show content "tomcat 2" straight away, after I refresh the page, right ?

Then I keep refresh the page, what happen is:
...
show "tomcat 2"
loading ......
show "tomcat 2"
loading ......
show "tomcat 2"
loading ......
show "tomcat 2"
loading ......
show "tomcat 2"
loading ......
...

I wonder why it is loading ? is my ip_hash config wrong ??
Re: Upstream question
March 19, 2013 07:57AM
I understand it works like this:

By chance, the ip_hash module hashed your IP to be forwarded to the first tomcat. Now, since "tomcat 1" is down, you're being forwarded to "tomcat 2". But since nginx tries to check if "tomcat 1" is back up and your IP matched to it, it tries to connect to "tomcat 1" again on your next request. Then it registers that it's still down so you get forwarded to "tomcat 2" again... and so on.

I've read some enlightening comments by Maxim Dounin in this thread: http://forum.nginx.org/read.php?2,232912,232912.
Re: Upstream question
March 22, 2013 06:06AM
Hi Chris,

Nginx is using a round robin algorithm to decide which host should get the request, since you are (probably) the only user tomcat 1 and tomcat 2 are getting each 50% of the trafic.
Now you want all trafic to go to tomcat 2 because tomcat 1 is down, and you can achieve this by telling Nginx to mark a server as down if it cannot reach it:

upstream backend {
#ip_hash;
server 10.10.10.120:8081 max_fails=3 fail_timeout=30s; # this has a jsp page say "tomcat 1"
server 10.10.10.120:8080 max_fails=3 fail_timeout=30s; # this has a jsp page say "tomcat 2"
}

also ip_hash is telling the Nginx to route all request form a single host to the same backend server.

More info you can find here: http://wiki.nginx.org/HttpUpstreamModule
Sorry, only registered users may post in this forum.

Click here to login

Online Users

Guests: 262
Record Number of Users: 8 on April 13, 2023
Record Number of Guests: 421 on December 02, 2018
Powered by nginx      Powered by FreeBSD      PHP Powered      Powered by MariaDB      ipv6 ready