tomcat详解2-nginx反向代理实现tomcat负载均衡

9 篇文章 0 订阅
5 篇文章 0 订阅
 nginx反向代理实现tomcat负载均衡
单主机

设置多主机域名

[root@rocky /usr/local/tomcat/conf]$cat server.xml
.....................
       </Host>
  <Host name="node1.yaya.com"  appBase="/data/webapps1/" unpackWARs="true"
autoDeploy="true">
           <Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs"
              prefix="node1_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
       </Host>
       <Host name="node2.yaya.com"  appBase="/data/webapps2/" unpackWARs="true"
autoDeploy="true">
       <Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs"
              prefix="node2_access_log" suffix=".txt"
              pattern="%h %l %u %t &quot;%r&quot; %s %b" />
.................................

实现nginx,tomcat动静分离

[root@rocky /etc/nginx/conf.d]$ cat tomcat.conf
server{
        listen 80;
        server_name node1.yaya.com;
        location /{
                proxy_pass http://node1.yaya.com:8080;
                proxy_set_header Host $http_host;
        }
         location ~* .*\.(png|jpg|jpeg|gif)$ {
         root /var/www/html;
         proxy_set_header Host $http_host;
     }
}
server{
        listen 80;
        server_name node2.yaya.com;
        location /{
                proxy_pass http://node2.yaya.com:8080;
                proxy_set_header Host $http_host;
        }
         location ~* .*\.(png|jpg|jpeg|gif)$ {
         root /var/www/html;
         proxy_set_header Host $http_host;
     }
}

实现多主机的负载均衡的动静分离

upstream static {
   server 10.0.0.8:80;
}
upstream tomcat {
   server 10.0.0.18:8080;
}
server {
   listen 80;
     server_name www.wang.org;
     location / {
         proxy_pass http://tomcat;
         proxy_set_header Host $http_host;
     }
     location ~* .*\.(png|jpg|jpeg|gif)$ {
         proxy_pass http://static;
         proxy_set_header Host $http_host;
     }
}
tomcat负载均衡

10.0.0.150 node.yaya.com tomcat

10.0.0.151 node.yaya.com nginx+tomcat

tomcat文件

[root@rocky /etc/nginx/conf.d]$ cat /usr/local/tomcat/conf/server.xml
..........................................
       </Host>
  <Host name="node.yaya.com"  appBase="/data/webapps1/" unpackWARs="true"
autoDeploy="true">
           <Valve className="org.apache.catalina.valves.AccessLogValve"
directory="logs"
              prefix="node1_access_log" suffix=".txt"
               pattern="%h %l %u %t &quot;%r&quot; %s %b" />
...........................................

编写测试jsp文件,放到node1和node2目录下

[root@rocky /data/webapps1/ROOT]$ cat index.jsp
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <title>tomcat test</title>
</head>
<body>
<div>On <%=request.getServerName() %></div>
<div><%=request.getLocalAddr() + ":" + request.getLocalPort() %></div>
<div>SessionID = <span style="color:blue"><%=session.getId() %></span></div>
<%=new Date()%>
</body>
</html>

nginx.conf文件

[root@rocky /etc/nginx/conf.d]$ cat tomcat.conf
upstream tomcat {
#server域名解析成ip,然后锚定机器走对应ip所在的url
        server  node1.yaya.com:8080;  
        server  node2.yaya.com:8080;
}
server{
        listen 80;
        server_name node.yaya.com;
        location / {
                proxy_pass http://tomcat;
                proxy_set_header Host $http_host ;
        }
}

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值