tomcat简单部署,nginx负载均衡

2017-09-02  本文已影响0人  素瑾汐

tomcat简单部署,nginx负载均衡

实验说明:主机A、B上部署tomcat,C上安装nginx部署实现负载均衡,主机A、B、C部署如下:
A tomcat 172.16.10.15
B tomcat 172.16.10.17
C nginx 172.16.10.16

A主机和B主机做tomcat,并分别在主机上部署一个应用(添加JSP测试页)
1、安装tomcat和openjdk
yum -y install java-1.8.0-openjdk-devel tomcat tomcat-admin-webapps tomcat-docs-webapp tomcat-lib tomcat-webapps
2、编辑tomcat配置文件,部署应用(添加JSP测试页)
vim /etc/tomcat/server.xml

...
<Engine name="Catalina" defaultHost="www.xmj.com">
  <Host name="www.xmj.com" appBase="/data/webapps" unpackWARs="true" autoDeploy="true">
    <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs"
      prefix="xmj_access_log." suffix=".log"
      pattern="%h %l %u %t "%r" %s %b" />
  </Host>
...

创建应用目录,并编辑测试页
mkdir -pv /data/webapps/ROOT/{class,lib,WEB-INF}
cd /data/webapps/ROOT/
vim index.jsp

<%@ page language="java" %>
<html>
   <head><title>TomcatA</title></head>
   <body>
      <h1><font color="red">TomcatA.magedu.com</font></h1>
      <table align="centre" border="1">
        <tr>
          <td>Session ID</td>
        <% session.setAttribute("magedu.com","magedu.com"); %>
          <td><%= session.getId() %></td>
       </tr>
       <tr>
          <td>Created on</td>
          <td><%= session.getCreationTime() %></td>
      </tr>
    </table>
  </body>
</html>

重启tomcat服务
systemctl restart tomcat
ss -ntl 会看到tomcat服务对应的8080、8005和8009端口都已打开
3、A、B、C主机编写hosts文件
vim /etc/hosts
172.16.10.16 www.xmj.com
测试:
http://172.16.10.15:8080


http://172.16.10.17:8080


http://www.xmj.com

C主机上部署nginx,实现负载均衡
yum -y install nginx
vim /etc/nginx/nginx.conf

...
http {
...
upstream appsrvs{
server 172.16.10.15:8080;
server 172.16.10.17:8080;
}
...
server {
listen 80;
server_name www.xmj.com;
index index.jsp index.html;
location / {
proxy_pass http://appsrvs/;}
}
...
}
...

启动nginx服务
systemctl restart nginx

测试
http://www.xmj.com


刷新

上一篇下一篇

猜你喜欢

热点阅读