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机制。 [root@localhost xue]# iptables -F [root@localhost xue]# service iptables stop iptables:将链设置为政策 ACCEPT:filter [确定] iptables:清除防火墙规则: [确定] iptables:正在卸载模块: [确定] [root@localhost xue]# chkconfig iptables off [root@localhost xue]# setenforce 0 2)在安装tomcat之前,先安装JDK(Java Development Kit) [root@tomcat1 xue]# tar xf jdk-7u65-linux-x64.tar.gz [root@tomcat1 xue]# mv jdk1.7.0_65/ /usr/local/java [root@tomcat1 xue]# vi /etc/profile.d/java.sh export JAVA_HOME=/usr/local/java //设置Java根目录 export PATH=$PATH:$JAVA_HOME/bin //在PATH环境变量中添加JAVA根目录下的子目录 ------------------------------------------ [root@tomcat1 xue]# source /etc/profile.d/java.sh [root@tomcat1 xue]# 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 [root@tomcat1 xue]# tar xf apache-tomcat-7.0.54.tar.gz [root@tomcat1 xue]# mv apache-tomcat-7.0.54 /usr/local/tomcat7 [root@tomcat1 xue]# /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端口。 [root@tomcat1 xue]# netstat -anpt |grep 8080 tcp 0 0 :::8080 :::* LISTEN 3060/java 随后,我们可以打开浏览器测试: http://192.168.200.111:8080/ 如果关闭tomcat的话.可以执行下面命令: [root@tomcat1 xue]# /usr/local/tomcat7/bin/shutdown.sh ================================================================= 4)、建立java的web站点 4.1 首先创建网站发布路径 [root@tomcat1 ~]# mkdir -pv /web/webapp1 mkdir: 已创建目录 "/web" mkdir: 已创建目录 "/web/webapp1" 4.2 在/web/webapp1下建立测试页index.jsp [root@tomcat1 ~]# vi /web/webapp1/index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>JSP test1 page</title> </head> <body> <% out.println("Welcom to test site,http://www.test1.com");%> </body> </html> ------------------------------------------------------------- 4.3修改tomcat的server.xml文件(定义一个虚拟主机) [root@tomcat1 ~]# vi /usr/local/tomcat7/conf/server.xml <Host name="localhost" appBase="webapps" unpackWARs="true" autoDeploy="true"> <Context docBase="/web/webapp1" path="" reloadable="false"> </Context> 注释: docBase: web应用的文档基准目录 reloadable:设置监视类是否变化。 4.4: 重启tomcat. [root@tomcat1 ~]# /usr/local/tomcat7/bin/shutdown.sh [root@tomcat1 ~]# /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 唯一不同之处: [root@tomcat2 ~]# vi /web/webapp1/index.jsp <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <html> <head> <title>JSP test2 page</title> </head> <body> <% out.println("Welcom to test site,http://www.test2.com");%> </body> </html> ============================================================================ 1)Nginx服务器配置 1.1 关闭Linux iptables防火墙及selinux 同上 1.2 安装nginx相关软件包 [root@localhost ~]# yum -y install pcre-devel zlib-devel openssl-devel [root@localhost ~]# yum -y install gcc gcc-c++ make 1.3 解压并安装nginx [root@localhost nginx-1.6.0]# ./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 [root@localhost nginx-1.6.0]# cd /usr/local/nginx/conf/ [root@localhost conf]# cp nginx.conf nginx.conf.bak [root@localhost conf]# vi nginx.conf [root@localhost conf]# cat nginx.conf user www www; worker_processes 1; error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; pid logs/nginx.pid; events { use epoll; worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream tomcat_server { server 192.168.200.111:8080 weight=1; server 192.168.200.112:8080 weight=1; } server { listen 80; server_name localhost; charset utf-8; #access_log logs/host.access.log main; location / { root html; index index.html index.htm; proxy_pass http://tomcat_server; } #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; } # 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_index index.php; # fastcgi_param SCRIPT_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 { # deny all; #} } # another virtual host using mix of IP-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # HTTPS server # #server { # listen 443 ssl; # server_name localhost; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m; # ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} } ------------------------------------------------------------- 1.5 测试nginx配置文件是否正常: [root@localhost conf]# /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服务器 [root@localhost conf]# /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf [root@localhost conf]# ps aux|grep nginx root 5118 0.0 0.1 45000 1140 ? Ss 22:38 0:00 nginx: master process /usr/local/nginx/sbin/nginx -c /usr/local/nginx/conf/nginx.conf www 5119 0.0 0.1 45452 1736 ? S 22:38 0:00 nginx: worker process root 5122 0.0 0.0 103248 880 pts/0 S+ 22:38 0:00 grep nginx [root@localhost conf]# 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日志分割脚本: =================== [root@localhost nginx]# cat logs/nginx.pid 5118 [root@localhost nginx]# kill -QUIT 5118 -QUIT : 关闭进程 -HUP : 重载服务, 平滑重启 -USR1: 日志切换,(通过结合crontab,每天生成一个新的日志) -USR2: 平滑升级可执行程序 =[root@localhost ~]# 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) --------------------------------------------------------------------------- [root@localhost ~]# chmod +x /opt/fenge-nginx.sh [root@localhost ~]# /opt/fenge-nginx.sh [root@localhost ~]# tree /home/nginx/ /home/nginx/ └── logs └── 2014 └── 12 ├── access.20141220.log └── error.20141220.log 3 directories, 2 files ----------------------------------------------------- [root@localhost ~]# crontab -l 0 0 * * * /opt/fenge-nginx.sh
nginx+tomcat 负载均衡
原创
©著作权归作者所有:来自51CTO博客作者安全守望者的原创作品,谢绝转载,否则将追究法律责任
上一篇:mysql 主从
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章