系统相关
首页 > 系统相关> > nginx实现后端tomcat的负载均衡调度

nginx实现后端tomcat的负载均衡调度

作者:互联网

一、编译安装nginx

请跳转查阅:编译安装nginx

二、tomcat的二进制安装

请跳转查阅:二进制安装tomcat

三、配置nginx代理

# cat  /apps/nginx/conf/nginx.conf
worker_processes  1;

events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream tomcat-hosts {
	server	10.0.0.18:8080;
	server	10.0.0.28:8080;
    }
    server {
        listen       80;
        server_name  localhost;

        location / {
            root   html;
            index  index.html index.htm;
        }
        location ~* \.(jsp|do)$ {
            proxy_pass http://tomcat-server;
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
    }
}

四、配置后端tomcat服务器

4.1 配置node1

两台后端服务器配置大致相同,下面展示了node1的配置。

# vim /usr/local/tomcat/conf/server.xml
#添加虚拟主机,并且设置为默认主机
<Engine name="Catalina" defaultHost="test.node1.org">
	<Host name="t1.magedu.org" appBase="/data/webapps" autoDeploy="true" >
	</Host>
</Engine>

#准备文件目录
# mkdir /data/webapps/ROOT -pv
mkdir: created directory '/data'
mkdir: created directory '/data/webapps'
mkdir: created directory '/data/webapps/ROOT'
# vim /data/webapps/ROOT/index.jsp
<%@ page import="java.util.*" %>
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>tomcat test node1</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>

#设置权限
# chown -R tomcat.tomcat /data/webapps/

# systemctl restart tomcat

4.2 测试访问

在Windows端配置域名解析,然后打开浏览器访问test.node1.org:8080

由此,后端服务器可用。

五、测试nginx代理

因为在nginx配置了资源类型jsp代理,默认使用轮询反向代理到后端tomcat服务器

标签:负载,tomcat,webapps,server,nginx,html,data
来源: https://www.cnblogs.com/hzgy/p/15844165.html