One of the simplest load-balancing tests, which does not involve session replication, simply assigns the request to a different server.
1. Create a simple Web application. There is only one index.jsp page, the content is as follows.
<%@ page language= "java" pageencoding= "UTF-8"%><! DOCTYPE html> hello,nginx! <% System.out.println ("*****************nginx do load Balancing to assign requests to this server *****************"); %> </body>
Print a word on the OK, can be printed on different servers, indicating that the request was assigned to a different server.
2, Configuration nginx.conf
1) Add the following to the HTTP node
Upstream localhost {server localhost:18080;//tomcat server-1th server LOCALHOST:28080;//TOMCAT Servers-# 2nd }
2) Add the following content to the server node content in the HTTP node
Location/{ proxy_pass http://localhost;}
3. Deploy the Web application to two Tomcat servers, launch two Tomcat, and start Nginx. Then visit http://localhost/index.jsp to see what the two Tomcat servers are printing
4, the above is the default use of polling to achieve load balancing, there are two other ways
Least connected (assigning requests to the most idle server)
Upstream localhost { least_conn; Server localhost:18080; Server localhost:28080;}
Ip_hash (the request is assigned to some fixed server based on the client IP)
Upstream localhost { ip_hash; Server localhost:18080; Server localhost:28080;}
5, in the way of polling, you can also specify the server to accept the weight of the request
Upstream localhost { server localhost:18080 weight=2; Server localhost:28080 weight=1; }
Nginx as a Load Balancer server (Windows environment)