概述
nginx现在十分流行,一直想做个nginx负载均衡,体验一下真实的环境,碰巧最近项目需要正好搭建这个环境:
我是用一个nginx服务器转发请求到两个tomcat上,具体环境:
os:centos5.5 ,?nginx-0.7.44,?pcre-8.10,apache-tomcat-7.0.5
IP地址分配如下:
nginx server:192.168.2.220
tomcat server1 192.168.2.202
tomcat server2 192.168.2.203
一,具体操作步骤:(在192.168.2.220上)
1,安装pcre
#tar zxvf pcre-8.10.tar.gz
#cd pcre-8.10
#make#make install
2,安装??nginx-0.7.44
#tar zxvf ??nginx-0.7.44.tar.gz
#cd ??nginx-0.7.44
#?./configure --with-http_stub_status_module --with-http_ssl_module --prefix=/usr/local/nginx
#make && make install
注:安装过程中可能会有错误,一般就是库文件没有装,建议用yum -y install filename
2,测试nginx安装是否成功:
#/usr/local/nginx/sbin/nginx -t
如果出现如下信息说明安装成功:
# /usr/local/nginx/sbin/nginx -t
2010/12/04 21:36:15 [info] 3620#0: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
2010/12/04 21:36:15 [info] 3620#0: the configuration file /usr/local/nginx/conf/nginx.conf was tested successfully
3,修改nginx配置文件:
?user www www;
worker_processes 10;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
#最大文件描述符
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
http
{
include mime.types;
default_type application/octet-stream;
keepalive_timeout 120;
tcp_nodelay on;
upstream servers {
server 192.168.2.202:8080;
server 192.168.2.203:8080;
}
server
{
listen 80;
server_name 192.168.2.220;
location /
{
proxy_pass http://servers;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
log_format 192.168.2.220 '$remote_addr - $remote_user [$time_local] $request '
'"$status" $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/logs/access.log ;
location /NginxStatus {
stub_status on;
access_log on;
auth_basic "NginxStatus";
# auth_basic_user_file htpasswd;
}
}
}
二,安装tomcat,分别在192.168.2.202/192.168.2.203进行如下操作:
1,安装jdk,
#cd /usr/local
#chmod +x jdk-6u22-linux-i586-rpm.bin
#./?jdk-6u22-linux-i586-rpm.bin
# rm -rf /usr/bin/java (删除系统默认java)
#ln -s /usr/java /usr/local/java
2,设置java环境变量
#vi /etc/profile
在/etc/profile最后面添加如下内容:
?export JAVA_HOME=/usr/java/jdk1.6.0_22
export JAVA_BIN=/usr/java/jdk1.6.0_22/bin
export CALSSPATH=.:$JAVA_HOME/lib.tools.jar
export PAHT=$JAVA_HOME/bin:$PATH
按下ESC输入:x
保存退出,让设置的变量生效输入:
#source /etc/profile
3,检查看java版本看看安装是否正确:
#java -version
?ava version "1.6.0_22"
Java(TM) SE Runtime Environment (build 1.6.0_22-b04)
Java HotSpot(TM) Client VM (build 17.1-b03, mixed mode, sharing)
4,安装tomcat
#cd /usr/src
#tar zxvf ?apache-tomcat-7.0.5.tar.gz
#mv ??apache-tomcat-7.0.5 /usr/local/tomcat
5,启动tomcat。
#/usr/local/tomcat/bin/startup.sh
?Using CATALINA_BASE: /usr/local/tomcat
Using CATALINA_HOME: /usr/local/tomcat
Using CATALINA_TMPDIR: /usr/local/tomcat/temp
Using JRE_HOME: /usr/java/jdk1.6.0_22
Using CLASSPATH: /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
6,查看tomcat服务端口
#netstat -lnt
?Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 0.0.0.0:608 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:25 0.0.0.0:* LISTEN
tcp 0 0 :::8009 :::* LISTEN
tcp 0 0 :::3306 :::* LISTEN
tcp 0 0 :::8080 :::* LISTEN
tcp 0 0 :::22 :::* LISTEN
至此,tomcat安装结束。
7,分别启动tomcat和nginx
#/usr/local/tomcat/bin/startup.sh (在192.168.2.202/192.168.2.203上分别运行)
#/usr/local/nginx/sbin/nginx (在192.168.2.220上运行)
测试结果:http://192.168.2.220
如果出现猫头证明环境搭建完成,这里我建议更改下tomcat的默认页面,便于区分。
让tomcat和nginx在开机启动可以运行下面脚本:
#echo "/usr/local/tomcat/bin/startup.sh" >>/etc/rc.local
#echo "/usr/local/nginx/sbin/nginx" >>/etc/rc.local
最后
以上就是听话航空为你收集整理的nginx+tomcat整合负载均衡的全部内容,希望文章能够帮你解决nginx+tomcat整合负载均衡所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复