nginx对多台tomcat实行负载均衡以减轻服务器压力

1.准备

1.nginx 2.tomcat

2.启动

1.tomcat安装配置这里不再赘述

2.nginx启动后访问浏览器 默认端口80这里我改了10010

start nginx

访问浏览器若不成功可能端口冲突,在nginx的conf.xml 修改一下listen 值

3.启动俩个tomcat服务

4.nginx 中的conf.xml进行配置,达到对着俩个tomcat的负载均衡 conf.xml文件的配置

events {
          
   
    # 各工程最大连接数
    worker_connections  1024;
}


http {
          
   
    include       mime.types;
    default_type  application/octet-stream;

	# upstream 各服务器地址以及权重, 权重越大代表访问率越大
	upstream  fx.com {
          
     
              server   localhost:8080 weight=1;  
              server   localhost:8081 weight=2;  
    }
    server {
          
   
	    # 端口
        listen       10010;
		#配置域名 由于没有域名,故使用localhost
        server_name  localhost;
        charset utf-8;  
        location / {
          
     
            root   html;  
            index  index.html index.htm;
			#反向代理,这里的地址与上面配置的upstream需一致
            proxy_pass  http://fx.com;
            proxy_set_header  X-Real-IP  $remote_addr;
            client_max_body_size  100m;
        }
    }

}

5.重启nginx

nginx -s reload

6.访问nginx地址可以看到已对这俩tomcat实现负载均衡

经验分享 程序员 微信小程序 职场和发展