Nginx与Tomcat集群实施
实施环境:Ubuntu13.04 Tomcat7.0 Nginx1.4.3
Nginx编译安装
在Ubuntu系统下,可以通过apt-get的方式来安装nginx,也可以直接通过下载安装包自行编译安装. 这里采用自行编译安装的方式实施, 安装包的版本为nginx1.4.3
Nginx编译
$ ./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,对它进行编译安装
$ ./configure --prefix=/usr/local/pcre $ sudo make $ sudo make install
然后再次进行nginx的编译
$ ./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)
编译完成之后,输出的摘要信息如下:
+ 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
$ sudo make $ sudo make install
通过编译的输出结果可以看到,nginx安装之后位于/usr/local/nginx目录,它的核心配置文件是conf/nginx.conf
启动nginx
命令行进入/usr/local/nginx目录,执行下面的命令启动
$ sudo sbin/nginx
关闭和重启nginx的分别命令为
$ sudo sbin/nginx -s stop $ sudo sbin/nginx -s reload
nginx: [emerg] getgrnam("nobody") failed in **的报错处理:
出现该问题是因为缺少nobody用户,或者该用户未添加到组中,检查之后发现本机没有nobody组,所以新增nobody组,并将用户nobody加入组中.
$ sudo addgroup nobody $ sudo adduser nobody nobody
启动之后,可以通过http://localhost检查一下是否启动成功.
Tomcat集群配置
首先准备两个Tomcat,并设置其访问端口分别为6080和7080,下面将通过Nginx实现这两个Tomcat的集群和负载均衡功能.
Nginx核心的配置文件位于$NGINX_HOME/conf/nginx.conf,为了测试方便,我们把该配置文件复制一份,并在新的配置文件中进行集群相关的配置. 打开刚复制出来的配置文件进行参数的修改
$ sudo cp conf/nginx.conf conf/nginx-cluster.conf $ sudo vi conf/nginx-cluster.conf
配置文件中默认的参数及含义见参考资料.
对http节点的配置信息如下:
http { include mime.types; default_type application/octet-stream; access_log logs/access.log ; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream cluster { server localhost:6080; server localhost:7080; } server { listen 80; server_name localhost; location ~ ^/NginxStatus/ { stub_status on; access_log off; } location ~ ^/(WEB-INF)/ { deny all; } location / { proxy_pass http://cluster; proxy_set_header Host $host; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } } }
集群测试
分别在两个tomcat/webapps中新增应用web,并在其中放入一个测试页面test.jsp.内容如下:
<% String msg = "JVM1:"+ new SimpleDateFormat("yyyyMMdd HH:mm:ss").format(new Date()) + " Served."; out.println(msg); System.out.println(msg); %>
在第2个tomcat中,将上面的JVM1改为JVM2.
重启nginx,进行集群的测试.由于这里使用的配置文件是自定义的nginx-cluster.conf,所以在启动时需要增加参数,如下:
$ sudo sbin/nginx -c conf/nginx-cluster.conf
待nginx启动之后,可以通过http://localhost/web/test.jsp查看页面上输出的结果,在多次刷新页面时, 将会是依次输出JVM1和JVM2的信息.
参考资料:
http://www.ibm.com/developerworks/cn/web/wa-lo-nginx/