1.修改nginx.conf配置文件,在http大括号中添加如下配置

 upstream nginx {
     server 192.168.2.140:8080;
     server 192.168.2.136:8080;
 }


2.修改server的location中的配置

location / {
    proxy_pass  #就是这一行,这里的nginx就是上面upstream 后面的nginx,这是一个名称,请保存两个
    #地方是一致的
    auth_basic off;
    auth_basic_user_file /var/user;
    root   html;
    index  index.html index.htm;
}


3.准备两台服务器地址分别为192.168.2.140和192.168.2.136,并安装apache服务器,端口使用8080


4.分别修改apache webapps/ROOT中的index.jsp,修改为简单的内容

<!-- tomcat1中的index.jsp -->
<html>
    <h1>tomcat1</h1>
</html>

<!-- tomcat2中的index.jsp -->
<html>
    <h1>tomcat2</h1>
</html>


5.在浏览器访问http://www.nginx1.com/即可看到配置后的结果,每一次刷新请求不同的apache服务

nginx实现负载均衡_负载均衡