Nginx+Tomcat搭建高性能负载均衡集群

本实验是将两套Tomcat 和一套nginx服务搭建在同一台服务器上。

二、    目标


  实现高性能负载均衡的Tomcat集群:


  Nginx+Tomcat搭建高性能负载均衡集群



1、安装nginx

编译源码需要的组件


1.zlib   ngix 需要的lib库


2.pcre  nginx rewrite重写使用,支持正则表达式


3.openssl  支持安全协议的站点使用


1)介质下载
库下载地址:


源码下载地址 zlib 


http://www.zlib.net/


PCRE --支持正则表达式 


http://www.pcre.org/


opensll安装(可选), 支持安全协议的站点


http://www.openssl.org/


nginx 下载


http://nginx.org/en/download.html




2)上传介质到linux平台、并且解压
3)编译安装
4)修改配置文件




1.编译zlib


tar xvf zlib-1.2.8.tar.gz
./configure --static --prefix=/usr/local/libs/zlib
make
make install
2.编译openssl


tar xvf openssl-1.0.le.tar.gz
./config --prefix=/usr/local/openssl 
make
make install
3.编译pcre


tar xvf pcre-8.33.tar.gz
./configure --prefix=/usr/local/pcre 
make 
make install
4.编译ngnix


useradd nginx 


tar xvf nginx-1.5.4.tar.gz
./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-debug --with-openssl=/soft/openssl-1.0.le --with-zlib=/soft/zlib-1.2.8 --with-pcre=/soft/pcre-8.33 --with-http_stub_status_module --with-http_gzip_static_module  --with-http_ssl_module
make 
make install


备注:--with-openssl --with-zlib --with-pcre这3个路径是他们对应的源码路径.




启动nginx服务
cd /usr/local/nginx
./nginx
查看nginx进程是否正常
ps -ef|grep nginx



2、安装tomcat

在tomcat官网下载tomcat介质

上传Linux服务器上解压并修改配置文件
[[email protected] tomcat]# pwd
/usr/local/tomcat

[[email protected] tomcat1]# pwd
/usr/local/tomcat1


修改tomcat配置文件


第一套tomcat

Nginx+Tomcat搭建高性能负载均衡集群


第二套tomcat修改:

Nginx+Tomcat搭建高性能负载均衡集群

启动两套tomcat

修改nginx配置文件

配置如下(这里只进行了简单的配置,实际生产环境可以进行更详细完善配置):

vi tomcat_fzjh.conf

[html] view plain copy
  1. worker_processes  1;#工作进程的个数,一般与计算机的cpu核数一致  
  2.   
  3. events {  
  4.     worker_connections  1024;#单个进程最大连接数(最大连接数=连接数*进程数)  
  5. }  
  6.   
  7. http {  
  8.     include       mime.types; #文件扩展名与文件类型映射表  
  9.     default_type  application/octet-stream;#默认文件类型  
  10.   
  11.     sendfile        on;#开启高效文件传输模式,sendfile指令指定nginx是否调用sendfile函数来输出文件,对于普通应用设为 on,如果用来进行下载等应用磁盘IO重负载应用,可设置为off,以平衡磁盘与网络I/O处理速度,降低系统的负载。注意:如果图片显示不正常把这个改成off。  
  12.       
  13.     keepalive_timeout  65; #长连接超时时间,单位是秒  
  14.   
  15.     gzip  on;#启用Gizp压缩  
  16.       
  17.     #服务器的集群  
  18.     upstream  netitcast.com {  #服务器集群名字   
  19.         server    127.0.0.1:18080  weight=1;#服务器配置   weight是权重的意思,权重越大,分配的概率越大。  
  20.         server    127.0.0.1:28080  weight=2;  
  21.     }     
  22.   
  23.     #当前的Nginx的配置  
  24.     server {  
  25.         listen       80;#监听80端口,可以改成其他端口  
  26.         server_name  localhost;##############   当前服务的域名  
  27.   
  28.     location / {  
  29.             proxy_pass http://netitcast.com;  
  30.             proxy_redirect default;  
  31.         }  
  32.           
  33.   
  34.         error_page   500 502 503 504  /50x.html;  
  35.         location = /50x.html {  
  36.             root   html;  
  37.         }  
  38.     }  
  39. }  



启动nginx服务

/usr/local/nginx/sbin/nginx -c /usr/local/nginx/config/tomcat_fzjh.conf



来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30345407/viewspace-2135602/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/30345407/viewspace-2135602/