nginx http和TCP负载均衡

负载均衡

负载均衡是一种用于提高资源利用率和吞吐量的技术手段,采用负载均衡可以提高服务的容错率,在一定程度上减轻服务请求处理的压力。

nginx负载均衡

nginx使用upstream支持负载均衡配置,默认支持http请求负载均衡,nginx1.9以上版本新增支持TCP代理和负载均衡stream模块。

user www-data;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
	worker_connections 1024;
	use epoll; #eploo多路复用IO,提高性能
	# multi_accept on;
}

stream {
	upstream lb {
		#默认情况下采用Round robin轮询调度,哈希一致性调度算法需手动指定
		#使用hash负载均衡调度算法,可以达成持久会话,一个客户端IP连接总是落到同一Server服务上
		#hash $remote_addr consistent;
		server 192.168.1.143:5555;# max_fails=3 fail_timeout=30s; #server1 ip
		server 192.168.1.242:5555;# max_fails=3 fail_timeout=30s; #server2 ip
		server 192.168.1.243:5555;# max_fails=3 fail_timeout=30s; #server3 ip
	}
	server {
		# backlog设置等待连接队列的最大长度
		# reuseport 为每个工作进程创建一个单独的套接字
		# so_keepalive 开启长连接
		listen 5555 backlog=511 reuseport so_keepalive=on;
		proxy_connect_timeout 1s;
		proxy_timeout 3s;
		proxy_pass lb;
	}
}

http {

	##
	# Basic Settings
	##

	sendfile on;
	tcp_nopush on;
	tcp_nodelay on;
	keepalive_timeout 65;
	types_hash_max_size 2048;
	# server_tokens off;

	# server_names_hash_bucket_size 64;
	# server_name_in_redirect off;

	include /etc/nginx/mime.types;
	default_type application/octet-stream;

	##
	# SSL Settings
	##

	ssl_protocols TLSv1 TLSv1.1 TLSv1.2; # Dropping SSLv3, ref: POODLE
	ssl_prefer_server_ciphers on;

	##
	# Logging Settings
	##

	access_log /var/log/nginx/access.log;
	error_log /var/log/nginx/error.log;

	##
	# Gzip Settings
	##

	gzip on;

	# gzip_vary on;
	# gzip_proxied any;
	# gzip_comp_level 6;
	# gzip_buffers 16 8k;
	# gzip_http_version 1.1;
	# gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

	##
	# Virtual Host Configs
	##

	include /etc/nginx/conf.d/*.conf;
	include /etc/nginx/sites-enabled/*;

	#设定负载均衡服务器列表
	upstream loadbalance {
		#weight参数表示权值,权值越高被分配到的几率越大
		server 127.0.0.1:80 weight=10; #default
		server 127.0.0.1:8080 weight=10; #im
		server 127.0.0.1:8081 weight=10; #test
	}
}


#mail {
#	# See sample authentication script at:
#	# http://wiki.nginx.org/ImapAuthenticateWithApachePhpScript
# 
#	# auth_http localhost/auth.php;
#	# pop3_capabilities "TOP" "USER";
#	# imap_capabilities "IMAP4rev1" "UIDPLUS";
# 
#	server {
#		listen     localhost:110;
#		protocol   pop3;
#		proxy      on;
#	}
# 
#	server {
#		listen     localhost:143;
#		protocol   imap;
#		proxy      on;
#	}
#}

上述http请求负载均衡配置中各端口的server配置如下:

##
# You should look at the following URL's in order to grasp a solid understanding
# of Nginx configuration files in order to fully unleash the power of Nginx.
# https://www.nginx.com/resources/wiki/start/
# https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/
# https://wiki.debian.org/Nginx/DirectoryStructure
#
# In most cases, administrators will remove this file from sites-enabled/ and
# leave it as reference inside of sites-available where it will continue to be
# updated by the nginx packaging team.
#
# This file will automatically load configuration files provided by other
# applications, such as Drupal or Wordpress. These applications will be made
# available underneath a path with that package name, such as /drupal8.
#
# Please see /usr/share/doc/nginx-doc/examples/ for more detailed examples.
##

# Default server configuration
#
server {
	listen 80 default_server;
	listen [::]:80 default_server;

	# SSL configuration
	#
	# listen 443 ssl default_server;
	# listen [::]:443 ssl default_server;
	#
	# Note: You should disable gzip for SSL traffic.
	# See: https://bugs.debian.org/773332
	#
	# Read up on ssl_ciphers to ensure a secure configuration.
	# See: https://bugs.debian.org/765782
	#
	# Self signed certs generated by the ssl-cert package
	# Don't use them in a production server!
	#
	# include snippets/snakeoil.conf;

	root /var/www/html;

	# Add index.php to the list if you are using PHP
	index index.html index.htm index.nginx-debian.html;

	server_name www.test.com;

	location / {
		# First attempt to serve request as file, then
		# as directory, then fall back to displaying a 404.
		try_files $uri $uri/ =404;

		#对服务器进行负载均衡请求
		proxy_pass http://loadbalance;	#请求转向loadbalance定义的服务器列表
	}
}

http负载均衡

上述示例中http请求被分发到80,8080,8081三个工程项目中,可将其替换为三个不同的机器地址。当客户端发送http请求至nginx服务器时,服务器将使用轮询的方式依次将请求分发到三台服务器上。

tcp负载均衡

tcp负载均衡使用stream扩展实现,详细说明参见nginx官网说明: ngx_stream_core_module。TCP负载均衡包含轮询和散列两种方式,顾名思义,轮询是将所有请求按照顺序依次分发到三台服务器中,三台服务器中请求数平均分配。在stream模块upstream模块中轮询是默认请求分配方式。

hash $remote_addr consistent;

上述代码指定了负载均衡方式为散列,采用散列方式是根据客户端请求IP来分配接收请求的服务器地址,也就是说如果在本地做负载均衡测验,虽然部署了三台机器做集群,但是同一个客户端发出的所有请求IP都是相同的,所以所有请求都将分配到同一台服务器中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值