Tomcat + nginx群集和动静分离

  

实验环境

  
  

Nginx服务器(192.168.13.177)
Tomcat1服务器(192.168.13.151)
Tomcat2服务器(192.168.13.178)
客户端测试机

     

一、负载均衡

  

1,在Tomcat1, Tomcat2上安装Tomcat服务

  
 <代码> [root@tomcat1 ~] # systemctl firewalld停止。服务# #关闭防火墙
  root@tomcat1 ~ # mkdir/abc
  root@tomcat1 ~ #山。cifs//192.168.100.3/LNMP-C7/abc/(root@tomcat1 ~) # cd/abc/tomcat/(root@tomcat1 tomcat) #焦油zxvf jdk-8u91-linux-x64.tar。广州- c/usr/local/# #解压JDK
  (root@tomcat1 tomcat) # vim/etc/profile # #配置环境变量
  # #大G末行加入
  出口JAVA_HOME=/usr/地方/jdk1.8.0_91
  出口JRE_HOME=$ {JAVA_HOME}/jre
  出口CLASSPATH=: $ {JAVA_HOME}/lib: $ {JRE_HOME}/lib
  导出路径=$ {JAVA_HOME}/bin:美元的道路
  源/etc/profile (root@tomcat1 tomcat) # # #刷新配置文件
  (root@tomcat1 tomcat) #焦油zxvf apache-tomcat-8.5.16.tar。广州- c/usr/local/# #解压
  (root@tomcat1 tomcat) # cd/usr/local/root@tomcat1当地# mv apache-tomcat-8.5.16/tomcat
  # ln - s/usr/local/tomcat/bin/startup. root@tomcat1当地sh/usr/local/bin/ 
  

将启动和关闭脚本便于系统识别

  
 <代码> # ln - s/usr/local/tomcat/bin/shutdown. root@tomcat1当地sh/usr/local/bin/(当地root@tomcat1) # mkdir - p/web/webapp1 # #创建站点
  (当地root@tomcat1) # vim/web/webapp1/索引。jsp # #写入jsp网页内容
  网# # jsp页内容
  % @ & lt;页面语言=癹ava”=癹ava.util进口。*”pageEncoding=" utf - 8 " %比;
  & lt; html>
  & lt; head>
  & lt; title> JSP test1 page
  & lt;/head>
  & lt; body>
  & lt; %。println(“欢迎KGC Web”); %比;# #输出信息
  & lt;/body>
  & lt;/html>
  # vim/usr/local/tomcat/conf/server. root@tomcat1当地xml # #修改Tomcat配置文件
  & lt;主机名=發ocalhost”appBase=皐ebapps”
  unpackWARs=" true " autoDeploy=" true "比;
  & lt;上下文docBase="/web/webapp1”路径=可”“重载=癴alse”比;# #添加站点信息
  & lt;/Context>
  root@tomcat1 ~ #启动。sh # #启动服务
  # # Tomcat上网页内容为accp其他配置一样 
  

2,在Nginx服务器上安装Nginx

  
 <代码> [root@nginx ~] # systemctl firewalld停止。服务# #关闭防火墙
  (root@nginx ~) # setenforce 0
  (root@nginx ~) # yum安装pcre-devel zlib-devel gcc gcc-c + +使- y # #安装环境必要组件
  root@nginx ~ # mkdir/abc
  root@nginx ~ #山。cifs//192.168.100.3/LNMP-C7/abc/# #挂载
  密码root@//192.168.100.3/LNMP-C7:
  root@nginx ~ # cd/abc/(root@nginx abc) #焦油zxvf nginx-1.12.2.tar。广州- c/usr/local/# #解压
  (root@nginx abc) # useradd - m - s/sbin/nologin nginx # #创建系统用户
  (root@nginx abc) # cd/usr/local/nginx-1.12.2/[root@nginx nginx-1.12.2] # ./configure \ # #配置
  比;——prefix=/usr/地方/nginx \
  比;——用户=nginx \
  比;——组=nginx \
  比;——with-http_stub_status_module \
  比;——with-http_gzip_static_module \
  比;——with-http_flv_module
  [root@nginx nginx-1.12.2] #做,,使安装# #编译安装 
  

3,修改Nginx配置文件

  
 <代码> [root@nginx nginx-1.12.2] # vim/usr/local/nginx/conf/nginx.conf
  # keepalive_timeout 0;
  keepalive_timeout 65;
  
  # gzip alt=" Tomcat + nginx群集和动静分离“> 
 Tomcat + nginx群集和动静分离

  

二、动静分离

  

1,在Nginx上修改配置文件

  
 <代码> [root@nginx nginx-1.12.2] # vim/etc/init.d/nginx # #编写服务启动脚本
  # !/bin/bash
  # chkconfig: 20 - 99
  描述:Nginx服务控制脚本
  学监="/usr/地方/nginx/sbin/nginx”
  PIDF="/usr/地方/nginx/日志/nginx.pid”
  “1美元”
  开始)
  美元掠夺
  ;;
  停止)
  杀- s退出美元(猫PIDF美元)
  ;;
  重启)
  0美元停止
  $ 0开始
  ;;
  重新加载)
  杀- s玫瑰(猫PIDF美元美元)
  ;;
  *)
  回声”用法:$ 0{启动|停止| |重启重载}”
  出口1
  esac
  退出0
  [root@nginx nginx-1.12.2] # chmod + x/etc/init.d/nginx
  [root@nginx nginx-1.12.2] # chkconfig——添加nginx
  [root@nginx nginx-1.12.2] # vim/usr/local/nginx/conf/nginx.conf
  服务器{
  …省略……
  位置~。*。jsp ${# #匹配jsp页面跳转代理服务器池
  proxy_pass http://tomcat-server;
  proxy_set_header主机主机美元;
  }
  位置/{
  根html/测试;# #修改站点
  指数指数。html你;
  proxy_pass http://tomcat-server;
  }
  [root@nginx nginx-1.12.2] # vim/usr/local/nginx/html/index.html # #编写静态网页
  & lt; !DOCTYPE html>
  & lt; html>
  & lt; head>
  & lt;元http-equiv=? type”内容=" text/html; charset=utf - 8”比;
  & lt; title>静态页面& lt;/title>
  & lt; style>
  身体{
  宽度:35 em;
  保证金:0汽车;
  Verdana字体类型:大河马字体,Arial,无衬线;
  }
  & lt;/style>
  & lt;/head>
  & lt; body>
  & lt; h2>静态页面& lt;/h2>
  & lt; p>这是个静态页面& lt;/p>
  & lt;/body>
  & lt;/html>
  [root@nginx nginx-1.12.2] #服务nginx停止# #关闭开启服务
  [root@nginx nginx-1.12.2] #服务nginx开始

Tomcat + nginx群集和动静分离