nginx反向代理实现简单的负载均衡

1. nginx实现负载均衡策略

2.1 轮询 - 轮流处理请求

每个请求按时间顺序逐一分配到不同的后端服务器,如果后端服务器down掉,能自动剔除。

upstream rr_test{
	server localhost:8080;
	server localhost:8081;
}
server {
	listen     8083;
	server_name    localhost;
	location / {
		proxy_pass http://rr_test;
		proxy_set_header Host $host:$server_port;
	}
}
2.2 权重 — you can you up

通过配置权重,指定轮询几率,权重和访问比率成正比,用于应用服务器性能不均的情况。

upstream weight_test{
	server localhost:8080 weight=1;
	server localhost:8081 weight=9;
}
server {
	listen     8084;
	server_name    localhost;
	location / {
		proxy_pass http://weight_test;
		proxy_set_header Host $host:$server_port;
	}
}
2.3 ip_哈希算法

每个请求按访问ip的hash结果分配,这样每个访客固定访问一个应用服务器,可以解决session共享的问题。

server {
	listen     8085;
	server_name    localhost;
	location / {
		proxy_pass http://ip_hash;
		proxy_set_header Host $host:$server_port;
	}
}
upstream ip_hash{
	ip_hash;
	server localhost:8080;
	server localhost:8081;
}

2. nginx/conf代码示例

user www-data;
worker_processes auto;
pid /run/nginx.pid;

events {
	worker_connections 768;
	# multi_accept on;
}

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_disable "msie6";

	# 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 rr_test{
		server localhost:8080;
		server localhost:8081;
	}
	# 负载均衡,权重模式
	upstream weight_test{
		server localhost:8080 weight=1;
		server localhost:8081 weight=9;
	}
	# 负载均衡,ip哈希模式
	upstream ip_hash{
		ip_hash;
		server localhost:8080;
		server localhost:8081;
	}
	# 服务端,普通测试
	server {
		listen     8081;
		server_name    localhost;
		location /love {
			root /mnt/windows_share/nginx;
			index index.html;
		}
	}
	# 服务端, 代理测试
	server {
		listen     8082;
		server_name    localhost;
		location / {
			proxy_pass http://localhost:8080;
			proxy_set_header Host $host:$server_port;
		}
	}
	# 服务端, 轮询负载均衡测试
	server {
		listen     8083;
		server_name    localhost;
		location / {
			proxy_pass http://rr_test;
			proxy_set_header Host $host:$server_port;
		}
	}
	# 服务端, 权重负载均衡测试
	server {
		listen     8084;
		server_name    localhost;
		location / {
			proxy_pass http://weight_test;
			proxy_set_header Host $host:$server_port;
		}
	}
	# 服务端, ip哈希负载均衡测试
	server {
		listen     8085;
		server_name    localhost;
		location / {
			proxy_pass http://ip_hash;
			proxy_set_header Host $host:$server_port;
		}
	}
}


#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;
#	}
#}

参考:
https://blog.csdn.net/qq_36125138/article/details/84144932
https://www.cnblogs.com/qlqwjy/p/8536779.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
nginx反向代理负载均衡配置可以在同一台服务器上实现,也可以在多台服务器上实现。 以下是一个简单nginx反向代理负载均衡配置示例: ```nginx # upstream定义后端服务器,可以是多个 upstream backend { server backend1.example.com; server backend2.example.com; } # 反向代理配置 server { listen 80; server_name example.com; location / { proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; } } # 负载均衡配置 upstream backend { server backend1.example.com weight=5; server backend2.example.com weight=10; server backend3.example.com weight=5; } server { listen 80; server_name example.com; location / { # ip_hash表示使用客户端IP进行hash负载均衡 # least_conn表示使用最少连接数负载均衡 # round_robin表示使用轮询负载均衡 # 具体使用哪种负载均衡算法可以根据实际需求选择 # max_fails和fail_timeout表示当节点失败次数和失败时间超过设定值时,将该节点暂时从负载均衡池中剔除 # backup表示将该节点设置为备份节点,只有当其他节点都不可用时才会使用 # 具体参数可以参考nginx官方文档 # https://nginx.org/en/docs/http/ngx_http_upstream_module.html#upstream proxy_pass http://backend; proxy_set_header Host $host; proxy_set_header X-Real-IP $remote_addr; # 下面是负载均衡相关配置 # ip_hash; # least_conn; # round_robin; # server backend1.example.com max_fails=3 fail_timeout=30s; # server backend2.example.com max_fails=3 fail_timeout=30s; # server backend3.example.com max_fails=3 fail_timeout=30s backup; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值