windows下nginx+tomcat配置负载均衡的方法

2019-10-17 19:24:02王旭

这样我们就成功的搭建了一个nginx服务,成功的配置了两个tomcat应用实例。

3、Nginx+Tomcat负载均衡配置
这里只需要修改Nginx的配置,让它通过tomcat来转发。
a、nginx.conf配置文件

worker_processes 1;

events {
  worker_connections 1024;
}

http {
  include    mime.types;
  default_type application/octet-stream;
  sendfile    on;
  keepalive_timeout 10;
  include extra/upstream01.conf;
}

b、extra/upstream01.conf文件,负载均衡配置信息

upstream mysite {
  server localhost:8081 weight=5;
  server localhost:8082 weight=5;
}
 
server {
  listen 80;
  server_name localhost;
 
  location / {
  proxy_pass http://mysite;
  }
}

当有请求到localhost时,请求会被分发到对应的upstream设置的服务器列表上,每一次请求分发的服务器都是随机的。

接着在运行一次start nginx,当你不断刷新http://localhost的时候,浏览器上就会来回切换"this is 8081 port"和"this is 8082 port"。

这样说明负载均衡配置成功了!!!!!!