g87616758 发表于 2018-12-7 09:52:47

Linux 下 tomcat基于nginx做负载均衡

  测试目的:在一台装有nginx服务器上访问nginx这台的ip地址,刷新一次就会显示后端三台不同的tomcat服务器的测试页。
  测试环境:三台centos 6.8
  一台 centos 7.3

  软件版本: nginx 1.12.1
  tomcat 8

  软件部署的话 就不操作了 之前已经部署好了的,不会的话 看我之前的博客里都有。
  nginx 安装
  http://dklwj.blog.运维网.com/9199080/1949570
  

  tomcat 安装:
  http://dklwj.blog.运维网.com/9199080/1955403
  

  在三台tomcat上做些修改
  1、把/usr/local/tomcat/webapps下原有的东西都删了除ROOT目录留着,不过ROOT目录下的东西可以清空然后把咱自己用到的测试页及所需用到的文件佳放进去。
  # ls
  classesindex.jsplibMETA-INFWEB-INF--这几个文件的话是java必须有的文件
  然后编辑index.jsp文件,后面两个的话 从第一个配置好的服务器拷贝过去 修改下里头的标题这里的话 我就用A、B、C 来代替三台服务器页面的测试标题。然后保存退出即可。

  # vim index.jsp
  

  
  
  tomcatB
  
  
   tomcatB.dklwj.com
  
  
  Session ID
  
  
  
  
  Created on
  
  
  
  
  
  启动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/local/jdk
Using CLASSPATH:       /usr/local/tomcat/bin/bootstrap.jar:/usr/local/tomcat/bin/tomcat-juli.jar
Tomcat started.
  用ss -tnl看下端口是否被监听

  # ss -tnl
  State      Recv-Q Send-Q                              Local Address:Port                              Peer Address:Port
  LISTEN   0      1                              ::ffff:127.0.0.1:8005                                          :::*
  LISTEN   0      100                                          :::8009                                          :::*
  LISTEN   0      100                                          :::8080                                          :::*
  LISTEN   0      128                                          :::22                                          :::*
  LISTEN   0      128                                             *:22                                             *:*
  其它两台tomcat配置一样这里就忽略了。
  配置nginx服务器
  # cd /etc/nginx/
  # vim nginx.conf
  在http配置栏下 增加以下一段

  upstream tcsrvs {
  server 192.168.2.32:8080;
  server 192.168.2.38:8080;
  server 192.168.2.39:8080;
  

  }
  在server配置里头加上,然后保持退出;
  server {
  listen       80;
  server_namelocalhost;
  

  #charset koi8-r;
  

  #access_loglogs/host.access.logmain;
  

  location / {
  root   html;
  indexindex.html index.htm;
  proxy_pass http://tcsrvs;
  }
  用nginx -t 检查配置文件是否报错。
  # nginx -t
  nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
  nginx: configuration file /etc/nginx/nginx.conf test is successful
  #
  然后浏览器验证:一刷新就跳到其它两台上去,说明测试成功了。
https://s3.运维网.com/wyfs02/M00/A3/3B/wKioL1mj7c_Tb7AeAABDYdw0gtE085.jpg-wh_500x0-wm_3-wmp_4-s_1794468385.jpg
https://s3.运维网.com/wyfs02/M02/04/8B/wKiom1mj7eGBPLdjAABDBEdZ9_4161.jpg-wh_500x0-wm_3-wmp_4-s_2648981917.jpg
https://s3.运维网.com/wyfs02/M02/04/8B/wKiom1mj7eHCaElWAAA7Pr2rhxI682.jpg-wh_500x0-wm_3-wmp_4-s_422553470.jpg
  有些地方有问题的,还请各位大佬指出来!万分感谢!!



页: [1]
查看完整版本: Linux 下 tomcat基于nginx做负载均衡