实施环境: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://talangniao.iteye.com/blog/341512
http://www.ibm.com/developerworks/cn/web/wa-lo-nginx/
http://developer.51cto.com/art/201004/194472.htm
http://zhuzhichao.com/ubuntu-lnmp-make-install/
相关推荐
Nginx+tomcat配置集群负载均衡实现动静分离实例
nginx,tomcat集群,session共享,基于memcache 包含nginx配置,tomcat配置,以及tomcat所需要的jar 不包含nginx,tomcat,jdk,memcache的安装
nginx+tomcat集群配置文件
关于nginx和tomcat在window下的下载安装和使用等。仅供参考。
nginx+tomcat集群的jar包,实现session共享
linux下nginx-tomcat集群及session共享,亲测成功,下载的文件包含了所有需要的jar包及nginx,memcached安装,配置,由于试了很多种方法,下了很多包,有些也需要积分,所以这里下载要些积分留着用,不好意思了,...
windowXP下搭建nginx-1.7.10和apache-tomcat-8.0.18的集群 已经配置好,下载后直接启动三个tomcat和nginx,然后访问http://localhost/即可。
基于ssm+shiro+redis+nginx tomcat服务器集群管理项目 基于ssm+shiro+redis+nginx tomcat服务器集群管理项目 基于ssm+shiro+redis+nginx tomcat服务器集群管理项目 基于ssm+shiro+redis+nginx tomcat服务器集群管理...
Docker-Compose部署nginx代理Tomcat集群,实现负载均衡!
nginx 配置tomcat8集群
nginx+tomcat集群demo,已测试,可放心下载使用!包含 nainx最新包。 温馨提示此demo是windows环境测试。 启动nginx:start nginx或nginx.exe。 关闭nginx:nginx.exe -s stop或nginx.exe -s quit。 访问方式:...
目前只支持tomcat8.5,其它版本的tomcat还没测试,自己配置过很多nginx+tomcat集群+redis实现session共享的系统,经过仔细总结得出这些,可以说是知识的结晶,欢迎大家来下载,不好的地方可以多提,有问题看到的话会...
nginx 和tomcat 集群,内含nginx 和tomcat 8 的集群配置,有例子,可测试: http://127.0.0.1:8080/test1/ 和 http://127.0.0.1:8080/test2/
nginx + tomcat 集群配置 负载 nginx 下载地址: http://nginx.org/en/download.html 联合tomcat 实现集群 windows: Nginx.conf 配置 //见文件夹下面的nginx.conf 更改tomcat的配置: 第一处端口...
nginx+tomcat集群部署与负载均衡。。。。。。。。。。。。。。。。。。。。。。。。。
NULL 博文链接:https://8324.iteye.com/blog/672945
Nginx+Tomcat+Memcached实现tomcat集群和session共享 nginx配置
nginx+tomcat集群实例 #服务器的集群,配置服务列表,权重,更改应用端口。启动nginx
nginx+tomcat集群简单参考