打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Nginx与Tomcat集群实施

实施环境:Ubuntu13.04 Tomcat7.0 Nginx1.4.3

 

Nginx编译安装

在Ubuntu系统下,可以通过apt-get的方式来安装nginx,也可以直接通过下载安装包自行编译安装. 这里采用自行编译安装的方式实施, 安装包的版本为nginx1.4.3

Nginx编译

Java代码  
  1. $ ./configure  

 

在未安装PCRE模块的情况下,会报错:

./configure: error: the HTTP rewrite module requires the PCRE library.

You can either disable the module by using --without-http_rewrite_module

option, or install the PCRE library into the system, or build the PCRE library

statically from the source with nginx by using --with-pcre=<path> option.

这里的错误信息应该是HTTP rewrite模块需要PCRE库的支持,如果不需要HTTP rewrite,可以选择通过增加参数--without-http_rewrite_module禁用该模块.

当然也可以通过ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/下载并安装PCRE依赖包,强烈建议安装此依赖包.

这里下载的PCRE包是pcre8.33.tar.gz,对它进行编译安装

Java代码  
  1. $ ./configure --prefix=/usr/local/pcre  
  2. $ sudo make  
  3. $ sudo make install  

 

然后再次进行nginx的编译

Java代码  
  1. $ ./configure --user=nobody --group=nobody --prefix=/usr/local/nginx --with-http_stub_status_module --with-pcre=/home/dingguangxian/software/pcre-8.33  

 

(更多的编译时参数设置可以参考http://wiki.nginx.org/InstallOptions)

编译完成之后,输出的摘要信息如下:

写道
Configuration summary
+ using PCRE library: /home/dingguangxian/software/pcre-8.33
+ OpenSSL library is not used
+ md5: using system crypto library
+ sha1: using system crypto library
+ using system zlib library

nginx path prefix: "/usr/local/nginx"
nginx binary file: "/usr/local/nginx/sbin/nginx"
nginx configuration prefix: "/usr/local/nginx/conf"
nginx configuration file: "/usr/local/nginx/conf/nginx.conf"
nginx pid file: "/usr/local/nginx/logs/nginx.pid"
nginx error log file: "/usr/local/nginx/logs/error.log"
nginx http access log file: "/usr/local/nginx/logs/access.log"
nginx http client request body temporary files: "client_body_temp"
nginx http proxy temporary files: "proxy_temp"
nginx http fastcgi temporary files: "fastcgi_temp"
nginx http uwsgi temporary files: "uwsgi_temp"
nginx http scgi temporary files: "scgi_temp"

 

 

 

安装Nginx

Java代码  
  1. $ sudo make  
  2. $ sudo make install  

 

通过编译的输出结果可以看到,nginx安装之后位于/usr/local/nginx目录,它的核心配置文件是conf/nginx.conf

 

启动nginx

命令行进入/usr/local/nginx目录,执行下面的命令启动

Java代码  
  1. $ sudo sbin/nginx  

 

关闭和重启nginx的分别命令为

Java代码  
  1. $ sudo sbin/nginx -s stop  
  2. $ sudo sbin/nginx -s reload  

 

nginx: [emerg] getgrnam("nobody") failed in **的报错处理:

出现该问题是因为缺少nobody用户,或者该用户未添加到组中,检查之后发现本机没有nobody组,所以新增nobody组,并将用户nobody加入组中.

Java代码  
  1. $ sudo addgroup nobody  
  2. $ sudo adduser nobody nobody  

 

启动之后,可以通过http://localhost检查一下是否启动成功.

 

Tomcat集群配置

首先准备两个Tomcat,并设置其访问端口分别为6080和7080,下面将通过Nginx实现这两个Tomcat的集群和负载均衡功能.

Nginx核心的配置文件位于$NGINX_HOME/conf/nginx.conf,为了测试方便,我们把该配置文件复制一份,并在新的配置文件中进行集群相关的配置. 打开刚复制出来的配置文件进行参数的修改

Java代码  
  1. $ sudo cp conf/nginx.conf conf/nginx-cluster.conf  
  2. $ sudo vi conf/nginx-cluster.conf  

 

配置文件中默认的参数及含义见参考资料.

对http节点的配置信息如下:

 

Java代码  
  1. http {  
  2.      include       mime.types;  
  3.      default_type  application/octet-stream;  
  4.    
  5.        access_log  logs/access.log  ;  
  6.    
  7.      sendfile        on;  
  8.      #tcp_nopush     on;  
  9.    
  10.      #keepalive_timeout  0;  
  11.      keepalive_timeout  65;  
  12.    
  13.      #gzip  on;  
  14.      upstream cluster {  
  15.          server localhost:6080;  
  16.          server localhost:7080;  
  17.      }  
  18.      server {  
  19.          listen       80;  
  20.          server_name  localhost;  
  21.          location ~ ^/NginxStatus/ {  
  22.             stub_status on;  
  23.             access_log off;  
  24.          }  
  25.         
  26.          location ~ ^/(WEB-INF)/ {  
  27.             deny all;  
  28.          }  
  29.     
  30.          location / {  
  31.              proxy_pass http://cluster;  
  32.             proxy_set_header Host $host;  
  33.          }  
  34.    
  35.          #error_page  404              /404.html;  
  36.    
  37.          # redirect server error pages to the static page /50x.html  
  38.          #  
  39.          error_page   500 502 503 504  /50x.html;  
  40.          location = /50x.html {  
  41.              root   html;  
  42.          }  
  43.      }  
  44.    
  45.  }  
  46.    

 

 

集群测试

分别在两个tomcat/webapps中新增应用web,并在其中放入一个测试页面test.jsp.内容如下:

Java代码  
  1. <%  
  2. String msg = "JVM1:"+ new SimpleDateFormat("yyyyMMdd HH:mm:ss").format(new Date()) + " Served.";  
  3.     out.println(msg);  
  4.     System.out.println(msg);  
  5. %>  

 

在第2个tomcat中,将上面的JVM1改为JVM2.

重启nginx,进行集群的测试.由于这里使用的配置文件是自定义的nginx-cluster.conf,所以在启动时需要增加参数,如下:

Java代码  
  1. $ sudo sbin/nginx -c conf/nginx-cluster.conf  

 

待nginx启动之后,可以通过http://localhost/web/test.jsp查看页面上输出的结果,在多次刷新页面时, 将会是依次输出JVM1和JVM2的信息.

 

参考资料:

http://wiki.nginx.org/Main

http://talangniao.iteye.com/blog/341512

http://www.ibm.com/developerworks/cn/web/wa-lo-nginx/

http://developer.51cto.com/art/201004/194472.htm

http://blog.s135.com/

http://zhuzhichao.com/ubuntu-lnmp-make-install/

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Nginx+Tomcat+Memcached集群
图文解说:Nginx+tomcat配置负载均衡
分享小白Nginx反向代理实现Tomcat分布式集群
Linux上Nginx的安装
Nginx环境搭建与使用
Nginx 安装配置 | 菜鸟教程
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服