Nginx如何实现四层负载均衡? Nginx的四层静态负载均衡需要启用 / Nginx如何实现四层负载均衡? Nginx的四层静态负载均衡需要启用

user  hadoop hadoop; worker_processes  auto;   error_log  logs/error.log; #error_log  logs/error.log  notice; #error_log  logs/error.log  info;   #pid        logs/nginx.pid;     events {  use epoll;     worker_connections  1024; }   stream {  upstream mysql_backend {   server 127.0.0.1:1111; #占位server   upsync 192.168.175.100:8500/v1/kv/upstreams/mysql_backend upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;   upsync_dump_path /usr/local/nginx-1.17.2/conf/mysql_backend.conf;  }  server {   #监听端口,默认使用的是tcp协议,如果需要UDP协议,则配置成listen 3307 udp;   listen 3307;   #失败重试   proxy_next_upstream on;   proxy_next_upstream_timeout 0;   proxy_next_upstream_tries 0;   #超时配置   #配置与上游服务器连接超时时间,默认60s   proxy_connect_timeout 1s;   #配置与客户端上游服务器连接的两次成功读/写操作的超时时间,如果超时,将自动断开连接   #即连接存活时间,通过它可以释放不活跃的连接,默认10分钟   proxy_timeout 1m;   #限速配置   #从客户端读数据的速率,单位为每秒字节数,默认为0,不限速   proxy_upload_rate 0;   #从上游服务器读数据的速率,单位为每秒字节数,默认为0,不限速   proxy_download_rate 0;   #上游服务器   proxy_pass mysql_backend;  }  server {   listen 13307;   upstream_show;  } } 

Nginx的完整配置如下:

Nginx完整配置

配置upstream_show指令后,可以通过curl http://192.168.175.100:13307/upstream_show查看当前动态负载均衡上游服务器列表。

server {  listen 13307;  upstream_show; } 
配置upstream_show
curl -X DELETE http://192.168.175.100:8500/v1/kv/upstreams/mysql_backend/192.168.175.202:3306
从Consul删除上游服务器
curl -X PUT -d "{\"weight\":1, \"max_fails\":2, \"fail_timeout\":10}" http://192.168.175.100:8500/v1/kv/upstreams/mysql_backend/192.168.175.201:3306 curl -X PUT -d "{\"weight\":1, \"max_fails\":2, \"fail_timeout\":10}" http://192.168.175.100:8500/v1/kv/upstreams/mysql_backend/192.168.175.202:3306 
从Consul添加上游服务器
server {  #监听端口,默认使用的是tcp协议,如果需要UDP协议,则配置成listen 3307 udp;  listen 3307;  #失败重试  proxy_next_upstream on;  proxy_next_upstream_timeout 0;  proxy_next_upstream_tries 0;  #超时配置  #配置与上游服务器连接超时时间,默认60s  proxy_connect_timeout 1s;  #配置与客户端上游服务器连接的两次成功读/写操作的超时时间,如果超时,将自动断开连接  #即连接存活时间,通过它可以释放不活跃的连接,默认10分钟  proxy_timeout 1m;  #限速配置  #从客户端读数据的速率,单位为每秒字节数,默认为0,不限速  proxy_upload_rate 0;  #从上游服务器读数据的速率,单位为每秒字节数,默认为0,不限速  proxy_download_rate 0;  #上游服务器  proxy_pass mysql_backend; } 
配置server

upsync_dump_path指定从consul拉取的上游服务器后持久化到的位置,这样即使consul服务器出现问题,本地还有一个备份

strong_dependency配置nginx在启动时是否强制依赖配置服务器,如果配置为on,则拉取配置失败时Nginx启动同样失败。

upsync_type指定使用consul配置服务器;

upsync_interval配置从consul拉取上游服务器配置的间隔时间;

upsync_timeout配置从consul拉取上游服务器配置的超时时间;

upsync指令指定从consul哪个路径拉取上游服务器配置;

upstream mysql_backend {  server 127.0.0.1:1111; #占位server  upsync 192.168.175.100:8500/v1/kv/upstreams/mysql_backend upsync_timeout=6m upsync_interval=500ms upsync_type=consul strong_dependency=off;  upsync_dump_path /usr/local/nginx-1.17.2/conf/mysql_backend.conf; } 
配置upstream
stream {  upstream mysql_backend {   ......  }  server {   ......  } } 

配置HTTP负载均衡时,都是配置在http指令下,配置四层负载均衡,则是在stream指令下,结构如下所示,

配置四层负载均衡
git clone https://github.com/xiaokai-wang/nginx-stream-upsync-module.git git clone https://github.com/weibocom/nginx-upsync-module.git git clone https://github.com/CallMeFoxie/nginx-upsync.git cp -r nginx-stream-upsync-module/* nginx-upsync/nginx-stream-upsync-module/ cp -r nginx-upsync-module/* nginx-upsync/nginx-upsync-module/   ./configure --prefix=/usr/local/nginx-1.17.2 --with-openssl=/usr/local/src/openssl-1.0.2s --with-pcre=/usr/local/src/pcre-8.43 --with-zlib=/usr/local/src/zlib-1.2.11 --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-cc-opt=-O3 --with-stream --add-module=/usr/local/src/nginx-upsync --with-http_ssl_module

使用如下命令为Nginx添加nginx-stream-upsync-module模块和nginx-upsync-module模块,此时,Nginx会同时支持四层动态负载均衡和HTTP七层动态负载均衡。

使用Nginx的四层动态负载均衡有两种方案:使用商业版的Nginx和使用开源的nginx-stream-upsync-module模块。注意:四层动态负载均衡可以使用nginx-stream-upsync-module模块,七层动态负载均衡可以使用nginx-upsync-module模块。

这是因为Worker进程维持的长连接一直在使用,所以无法退出,只能杀掉进程。可以使用Nginx的四层动态负载均衡解决这个问题。

nginx: worker process is shutting down; 

配置Nginx四层静态负载均衡后,重启Nginx时,Worker进程一直不退出,会报错,如下所示。

动态负载均衡
user  hadoop hadoop; worker_processes  auto;   error_log  logs/error.log; #error_log  logs/error.log  notice; #error_log  logs/error.log  info;   #pid        logs/nginx.pid;     events {  use epoll;     worker_connections  1024; }   stream {  upstream mysql_backend {   server 192.168.175.100:3306 max_fails=2 fail_timeout=10s weight=1;   least_conn;  }  server {   #监听端口,默认使用的是tcp协议,如果需要UDP协议,则配置成listen 3307 udp;   listen 3307;   #失败重试   proxy_next_upstream on;   proxy_next_upstream_timeout 0;   proxy_next_upstream_tries 0;   #超时配置   #配置与上游服务器连接超时时间,默认60s   proxy_connect_timeout 1s;   #配置与客户端上游服务器连接的两次成功读/写操作的超时时间,如果超时,将自动断开连接   #即连接存活时间,通过它可以释放不活跃的连接,默认10分钟   proxy_timeout 1m;   #限速配置   #从客户端读数据的速率,单位为每秒字节数,默认为0,不限速   proxy_upload_rate 0;   #从上游服务器读数据的速率,单位为每秒字节数,默认为0,不限速   proxy_download_rate 0;   #上游服务器   proxy_pass mysql_backend;  } } 

完整的Nginx配置如下:

Nginx完整配置

配置完之后,就可以连接Nginx的3307端口,访问数据库了。

server {  #监听端口,默认使用的是tcp协议,如果需要UDP协议,则配置成listen 3307 udp;  listen 3307;  #失败重试  proxy_next_upstream on;  proxy_next_upstream_timeout 0;  proxy_next_upstream_tries 0;  #超时配置  #配置与上游服务器连接超时时间,默认60s  proxy_connect_timeout 1s;  #配置与客户端上游服务器连接的两次成功读/写操作的超时时间,如果超时,将自动断开连接  #即连接存活时间,通过它可以释放不活跃的连接,默认10分钟  proxy_timeout 1m;  #限速配置  #从客户端读数据的速率,单位为每秒字节数,默认为0,不限速  proxy_upload_rate 0;  #从上游服务器读数据的速率,单位为每秒字节数,默认为0,不限速  proxy_download_rate 0;  #上游服务器  proxy_pass mysql_backend; } 
配置server
upstream mysql_backend {  server 192.168.175.201:3306 max_fails=2 fail_timeout=10s weight=1;  server 192.168.175.202:3306 max_fails=2 fail_timeout=10s weight=1;  least_conn; } 
配置upstream
stream {  upstream mysql_backend {   ......  }  server {   ......  } } 

配置HTTP负载均衡时,都是配置在http指令下,配置四层负载均衡,则是在stream指令下,结构如下所示.

配置四层负载均衡
./configure --prefix=/usr/local/nginx-1.17.2 --with-openssl=/usr/local/src/openssl-1.0.2s --with-pcre=/usr/local/src/pcre-8.43 --with-zlib=/usr/local/src/zlib-1.2.11 --with-http_realip_module --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module --with-cc-opt=-O3 --with-stream  --with-http_ssl_module

Nginx的四层静态负载均衡需要启用ngx_stream_core_module模块,默认情况下,ngx_stream_core_module是没有启用的,需要在安装Nginx时,添加--with-stream配置参数启用,如下所示。

静态负载均衡

随着现代互联网的高速发展,人们对于网络体验的要求也越来越高。在这个过程中,浏览器作为人们连接互联网的入口扮演着极其重要的角色。而Chrome浏览器则是其中最为出色的代表之一。

Chrome浏览器是由谷歌公司开发的一款免费的网络浏览器,它的推出引领了浏览器市场的变革。相较于其他浏览器,Chrome浏览器拥有更快的页面加载速度、更强大的扩展性以及更好的稳定性。这些特点使得Chrome浏览器成为了众多互联网用户的首选浏览器,其市场占有率也一直居于领先地位。

在现代网络时代,Chrome浏览器的地位愈发凸显。随着云计算、大数据、人工智能等技术的快速发展,互联网变得越来越复杂。在这样的背景下,Chrome浏览器以其先进的技术和创新的特性,为用户提供了更为优质的网络浏览体验。

首先,Chrome浏览器拥有强大的安全性能,它能够保护用户在互联网上的隐私和安全。Chrome浏览器的安全机制不仅仅包括防病毒、防黑客等常规的安全功能,还具有自动更新、强制HTTPS等特性,能够保障用户的网络安全。

其次,Chrome浏览器拥有丰富的扩展性能,用户可以通过安装各种插件来丰富浏览器的功能。Chrome浏览器的扩展库中涵盖了数以万计的扩展程序,从广告拦截、密码管理到日程管理等,涵盖了各种不同的用户需求。

此外,Chrome浏览器的同步功能也是其特色之一。用户可以通过Chrome账号将自己的浏览器数据同步到不同的设备上,无论是在电脑、手机还是平板电脑上,用户都能够享受到同样的浏览体验。