首页 > 技术文章 > Nginx+Tomcat负载均衡群集

mangood 2016-11-14 11:18 原文

一、Nginx负载均衡原理

  目前很多大型网站都应用Nginx服务器作为后端网站程序的反向代理及负载均衡器,提升整个站点的负载并发能力

  Nginx负载均衡是通过反向代理实现的

  

二、部署Tomcat 

  本案例以Nginx作为负载均衡器,Tomcat作为应用服务器,具体安装方法请见本人其他文章

  此案例为了测试搭建两个内容不同的网站可以创建/web/webapp1目录,修改server.xml,将网站文件目录更改到/web/webapp1/路径下,并建立测试页index.jsp,进行测试。

三、部署Nginx

 1 安装Nginx
 2 [root@localhost ~]# yum -y install pcre-devel zlib-devel
 3 [root@localhost ~]# useradd -M -s /sbin/nologin nginx
 4 [root@localhost ~]# tar -zxvf nginx-1.6.0.tar.gz -C /usr/src/
 5 [root@localhost ~]# cd /usr/src/nginx-1.6.0/
 6 [root@localhost nginx-1.6.0]# ./configure --prefix=/usr/local/nginx --user=nginx --
 7 group=nginx --with-file-aio --with-http_stub_status_module --with-http_gzip_static_module --
 8 with-http_flv_module --with-http_ssl_module
 9 [root@localhost nginx-1.6.0]# make && make install
10 [root@localhost ~]# ln -s /usr/local/nginx/sbin/* /usr/local/sbin/
11 [root@localhost ~]# nginx -t
12 [root@localhost ~]# nginx
13 [root@localhost ~]# netstat -anpt | grep 80
14 [root@localhost ~]# killall -s HUP nginx     
15 [root@localhost ~]# killall -s QUIT nginx     
16 [root@localhost ~]# nginx
17 验证:
18 [root@localhost ~]# firefox http://localhost/ &
19 
20 设置权重
21 [root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
22 在http{ }中添加:
23 upstream tomcat_server {
24 server 192.168.1.10:8080 weight=1;  #两台服务器权重相同,为轮询算法
25 server 192.168.1.20:8080 weight=1;  #两台服务器权重相同,为轮询算法
26 }
27 location / { }中添加:
28 proxy_pass http://tomcat_server;    #访问请求转发给Tomcat
29 
30 重启Nginx服务
31 [root@localhost ~]# killall -s QUIT nginx
32 [root@localhost ~]# nginx
33 [root@localhost ~]# netstat -anpt | grep nginx
34 
35 验证
36 [root@localhost ~]# firefox http://192.168.1.1/ &
37 验证结果:分别显示Tomcat1和Tomcat2上的网站页面。

 

推荐阅读