【运维笔记】使用 Nginx + tomcat 实现负载均衡、集群

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/seesun2012/article/details/84566574

准备工作:

  • nginx下载地址:http://nginx.org/en/download.html;
  • 选择稳定版Stable version),解压得到文件夹:nginx-1.14.1;
  • 在一个服务器上启动三台tomcat,端口分别为8080、9090、10010;
  • 进入conf目录,备份nginx.conf.bak配置文件,修改nginx.conf,参考【nginx.conf 集群精简版】;
  • 启动nginx,访问nginx,完成集群搭建。

nginx.conf 配置 tomcat 集群精简版:


events {
    worker_connections  1024;					# 单个后台进程的最大并发链接数,默认1024
}

http {

	include       mime.types;
	default_type  application/octet-stream;			# 默认数据类型
	
	sendfile        on;					# 发送文件:on 开启  off 关闭
	keepalive_timeout  65;					# 响应超时时间
	
	# 部分默认配置省略...
	# gzip  on;
	
	upstream seesun2012.oicp.net {
		server 127.0.0.1:8080 weight=2;			# 轮询格式:server ip:端口 weight=权重比
		server 127.0.0.1:9090 weight=1;
		server 127.0.0.1:10010 weight=3;		# weight代表权重,数字越高,权重越大,被访问的次数越多
	}
	
	server {
		listen       80;				# 监听端口号
		server_name  seesun2012.oicp.net;		# 定义使用 www.xx.com 访问(多个以空格隔开)
		charset utf-8;					# 编码格式
		location / {
			proxy_pass http://seesun2012.oicp.net;	# 必须要加 http 开头,访问的就是此地址
			proxy_set_header   Host             $host;
			proxy_set_header   X-Real-IP        $remote_addr;
			proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
			root   html;
			index  index.html index.htm;
		}
		
		# 部分默认配置省略...
		
		error_page   500 502 503 504  /50x.html;
		location = /50x.html {
			root   html;
		}
		
	}

}

名词解释:

  • http 设定http服务器,利用它的反向代理功能提供负载均衡支持;
  • weight 权重比:数字越大,权重越大,被访问的次数越多;
  • proxy_pass 被代理的
  • upstream 代理解析方式:upstream后配置 配置参考

猜你喜欢

转载自blog.csdn.net/seesun2012/article/details/84566574
今日推荐