nginx+tomcat 负载均衡
Tomcat+Nginx负载均衡服务器构建======================================================================
Tomcat服务器是一个免费的开源代码的Web应用服务器,属于轻量级的
网站应用服务器,应用在中小型系统和并发访问用户不是很多的场合下。
它和Aapache或nginx这些web服务器是一样的。都能处理html页面。由于
tomcat处理html的能力远不及Aapache或nginx,所以通常是作为一个servlet
和JSP容器使用。
----------------------------------------------------------------------
案例环境
服务器名称:tomcat服务器
操作系统:centos 6.5 x86_64
服务器IP:192.168.200.111
所需软件:1、apache-tomcat-7.0.54.tar.gz 2、jdk-7u65-linux-x64.tar.gz
=======================================================================
1、试验准备
1)关闭防火墙,selinux机制。
# iptables -F
# service iptables stop
iptables:将链设置为政策 ACCEPT:filter [确定]
iptables:清除防火墙规则: [确定]
iptables:正在卸载模块: [确定]
# chkconfig iptables off
# setenforce 0
2)在安装tomcat之前,先安装JDK(Java Development Kit)
# tar xf jdk-7u65-linux-x64.tar.gz
# mv jdk1.7.0_65/ /usr/local/java
# vi /etc/profile.d/java.sh
exportJAVA_HOME=/usr/local/java//设置Java根目录
export PATH=$PATH:$JAVA_HOME/bin //在PATH环境变量中添加JAVA根目录下的子目录
------------------------------------------
# source/etc/profile.d/java.sh
# java -version
java version "1.7.0_45"
OpenJDK Runtime Environment (rhel-2.4.3.3.el6-x86_64 u45-b15)
OpenJDK 64-Bit Server VM (build 24.45-b08, mixed mode)
3)安装配置tomcat
# tar xf apache-tomcat-7.0.54.tar.gz
# mv apache-tomcat-7.0.54 /usr/local/tomcat7
# /usr/local/tomcat7/bin/startup.sh
Using CATALINA_BASE: /usr/local/tomcat7
Using CATALINA_HOME: /usr/local/tomcat7
Using CATALINA_TMPDIR: /usr/local/tomcat7/temp
Using JRE_HOME: /usr/local/java
Using CLASSPATH: /usr/local/tomcat7/bin/bootstrap.jar:/usr/local/tomcat7/bin/tomcat-juli.jar
Tomcat started.
tomcat 默认运行在8080端口。
# netstat -anpt |grep 8080
tcp 0 0 :::8080 :::* LISTEN 3060/java
随后,我们可以打开浏览器测试: http://192.168.200.111:8080/
如果关闭tomcat的话.可以执行下面命令:
# /usr/local/tomcat7/bin/shutdown.sh
=================================================================
4)、建立java的web站点
4.1 首先创建网站发布路径
# mkdir -pv /web/webapp1
mkdir: 已创建目录 "/web"
mkdir: 已创建目录 "/web/webapp1"
4.2 在/web/webapp1下建立测试页index.jsp
# vi /web/webapp1/index.jsp
JSP test1 page
-------------------------------------------------------------
4.3修改tomcat的server.xml文件(定义一个虚拟主机)
# vi /usr/local/tomcat7/conf/server.xml
注释:
docBase: web应用的文档基准目录
reloadable:设置监视类是否变化。
4.4: 重启tomcat.
# /usr/local/tomcat7/bin/shutdown.sh
# /usr/local/tomcat7/bin/startup.sh
4.5: 测试http://192.168.200.111:8080/
出现"Welcom to test site,http://www.test1.com "表示成功。
=======================================================
案例: Nginx+Tomcat负载均衡集群
=================================
案例环境
服务器名称:tomcat服务器1
操作系统:centos 6.5 x86_64
服务器IP:192.168.200.111
所需软件:1、apache-tomcat-7.0.54.tar.gz 2、jdk-7u65-linux-x64.tar.gz
服务器名称:tomcat服务器2
操作系统:centos 6.5 x86_64
服务器IP:192.168.200.112
所需软件:1、apache-tomcat-7.0.54.tar.gz 2、jdk-7u65-linux-x64.tar.gz
服务器名称:Nginx服务器
操作系统:centos 6.5 x86_64
服务器IP:192.168.200.113
所需软件:nginx-1.6.0.tar.gz
=======================================================================
参照tomcat1来搭建tomcat2
唯一不同之处:
# vi /web/webapp1/index.jsp
JSP test2 page
============================================================================
1)Nginx服务器配置
1.1 关闭Linux iptables防火墙及selinux
同上
1.2 安装nginx相关软件包
# yum -y install pcre-devel zlib-devel openssl-devel
# yum -y install gcc gcc-c++ make
1.3 解压并安装nginx
# ./configure --prefix=/usr/local/nginx --user=www ---group=www --with-file-aio --with-
http_stub_status_module --with-http_gzip_static_module --with-http_flv_module --with-http_ssl_module && make && make
install
1.4 配置nginx.conf
# cd /usr/local/nginx/conf/
# cp nginx.conf nginx.conf.bak
# vi nginx.conf
# cat nginx.conf
userwww www;
worker_processes1;
error_loglogs/error.log;
#error_loglogs/error.lognotice;
#error_loglogs/error.loginfo;
pid logs/nginx.pid;
events {
use epoll;
worker_connections1024;
}
http {
include mime.types;
default_typeapplication/octet-stream;
log_formatmain'$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_loglogs/access.logmain;
sendfile on;
#tcp_nopush on;
#keepalive_timeout0;
keepalive_timeout65;
#gzipon;
upstream tomcat_server {
server 192.168.200.111:8080 weight=1;
server 192.168.200.112:8080 weight=1;
}
server {
listen 80;
server_namelocalhost;
charset utf-8;
#access_loglogs/host.access.logmain;
location / {
root html;
indexindex.html index.htm;
proxy_pass http://tomcat_server;
}
#error_page404 /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;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_indexindex.php;
# fastcgi_paramSCRIPT_FILENAME/scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# denyall;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_namesomenamealiasanother.alias;
# location / {
# root html;
# indexindex.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_namelocalhost;
# ssl_certificate cert.pem;
# ssl_certificate_keycert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout5m;
# ssl_ciphersHIGH:!aNULL:!MD5;
# ssl_prefer_server_cipherson;
# location / {
# root html;
# indexindex.html index.htm;
# }
#}
}
-------------------------------------------------------------
1.5 测试nginx配置文件是否正常:
# /usr/local/nginx/sbin/nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
1.6 启动nginx服务器
# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf
# ps aux|grep nginx
root 51180.00.1450001140 ? Ss 22:38 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c
/usr/local/nginx/conf/nginx.conf
www 51190.00.1454521736 ? S 22:38 0:00 nginx: worker process
root 51220.00.0 103248 880 pts/0 S+ 22:38 0:00 grep nginx
# netstat -anpt |grep nginx
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 5118/nginx
2) 测试负载均衡器的效果
http://192.168.200.113/
======================================================
nginx日志分割脚本:
===================
# cat logs/nginx.pid
5118
# kill -QUIT 5118
-QUIT : 关闭进程
-HUP : 重载服务, 平滑重启
-USR1: 日志切换,(通过结合crontab,每天生成一个新的日志)
-USR2: 平滑升级可执行程序
=# cat /opt/fenge-nginx.sh
#!/bin/bash
#fenge nginx logs
savepath_log='/home/nginx/logs'
nglogs='/usr/local/nginx/logs'
[ -d $savepath_log ] || mkdir -p $savepath_log
[ -d $savepath_log/$(date +%Y)/$(date +%m) ] || mkdir -p$savepath_log/$(date +%Y)/$(date +%m)
mv $nglogs/access.log $savepath_log/$(date +%Y)/$(date +%m)/access.$(date +%Y%m%d).log
mv $nglogs/error.log $savepath_log/$(date +%Y)/$(date +%m)/error.$(date +%Y%m%d).log
kill -USR1 $(cat /usr/local/nginx/logs/nginx.pid)
---------------------------------------------------------------------------
# chmod +x /opt/fenge-nginx.sh
# /opt/fenge-nginx.sh
# tree /home/nginx/
/home/nginx/
└── logs
└── 2014
└── 12
├── access.20141220.log
└── error.20141220.log
3 directories, 2 files
-----------------------------------------------------
# crontab-l
0 0 * * * /opt/fenge-nginx.sh
页:
[1]