nginx+tomcat配置负载均衡
第一步 下载tomcat,配置多tomcat运行环境;
① 下载tomcat,解压出两份到D:\tomcats目录下,并重新命名:
tomcat_8080
tomcat_8090
② 替换tomcat_8080的bin目录下的startup.bat和catalina.bat中CATALINA_HOME为CATALINA_HOME_8080
③ 替换tomcat_8090的bin目录下的startup.bat和catalina.bat中CATALINA_HOME为CATALINA_HOME_8090
④ 配置系统环境变量
变量名CATALINA_HOME_8080 变量值:D:\tomcats\tomcat_8080
变量名CATALINA_HOME_8090 变量值:D:\tomcats\tomcat_8090
⑤修改tomcat_8080的conf目录下的server.xml中三处
<Server port="8005" shutdown="SHUTDOWN">
<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<Connector port="8009" protocol="AJP/1.3" redirectPort="8443" />
⑥修改tomcat_8090的conf目录下的server.xml中三处
<Server port="8006" shutdown="SHUTDOWN">
<Connector port="8090" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
<Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />
第二步 下载nginx,配置nginx运行环境
①下载nginx的window版本
② 配置nginx的conf下的nginx.conf文件
#运行用户
#user nobody;
#开启进程数 <=CPU数
worker_processes 1;
#错误日志保存位置
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
#等待事件
events {
#Linux下打开提高性能
#use epoll;
#每个进程最大连接数(最大连接=连接数x进程数)
worker_connections 1024;
}
http {
#文件扩展名与文件类型映射表
include mime.types;
#默认文件类型
default_type application/octet-stream;
#日志文件输出格式 这个位置相于全局设置
#log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"';
#请求日志保存位置
#access_log logs/access.log main;
#打开发送文件
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#打开gzip压缩
#gzip on;
#配置一个负载均衡组
upstream mytomcats {
server 192.168.0.119:8080;
server 192.168.0.119:8090;
}
#第一个虚拟主机
server {
listen 80;
server_name 192.168.0.119;
charset utf-8;
location / {
root test;
index index.jsp; #设置默认页
proxy_pass http://mytomcats;
deny 127.0.0.1; #拒绝的ip
allow 192.168.0.119; #允许的ip
}
error_page 500 502 503 504 /50x.html;
}
}
第三 创建一个Dynamic Web Project 项目,部署到tomcat_8080和tomcat_8090,index.jsp页面分别如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>负载均衡测试-8080</title>
</head>
<body>
<p>8080服务</p>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()+ request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>负载均衡测试-8090</title>
</head>
<body>
<p>8090服务</p>
</body>
</html>
第四 启动tomcat_8080和tomcat_8090在浏览器中输入192.168.0.119/test/index.jsp,再刷新可以看到输出的结果:
为“8080服务”和“8090服务”。
总结:模拟应用负载均衡搭建完毕。