ngx_stream_proxy_module模块--实现Nginx tcp负载均衡
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | 七层代理和三层代理的配置文件结构 http { #ngx_http_core_module模块 upstream httpdsrvs { server 192.168.80.120 weight=3 ; } server { listen 80; server_name www.magedu.net; #监听域名和端口 location / { proxy_pass http: //httpdsrvs; #ngx_http_upstream_module模块 } } } <br> <br>----------------------------------------------------------------------------------------------------------- stream { #ngx_stream_core_module模块 upstream mysqlsrvs { server 192.168.80.120:3306 weight=3 ; } server { listen 172.16.100.100:3306; #监听IP和端口 proxy_pass mysqlsrvs; #ngx_stream_proxy_module模块 } } 实现Nginx tcp负载均衡 Nginx在1.9.0版本开始支持tcp模式的负载均衡,在1.9.13版本开始支持udp协议的负载,udp主要用于DNS的域名解析,其配置式和指令和http代理类似, 其基于ngx_stream_proxy_module模块实现tcp负载,另外基于模块ngx_stream_upstream_module实现后端服务器分组转发、权重分配、状态监测、调度算法等高级功能。 ngx_stream_core_module模块: 模拟反代基于tcp或udp的服务连接,即工作于传输层的反代或调度器,定义在main区,与http平级,一个七层,一个四层。 stream { ... } 定义stream相关的服务;Context:main #定义调度算法 hash $remote_addr consistent; 源地址hash调度方法,基于的客户端的remote_addr least_conn; 最少连接调度算法 ngx_stream_proxy_module模块 :可实现代理基于TCP,UDP (1.9.13), UNIX-domain sockets的数据流 proxy_pass address; 指定后端服务器地址或socket proxy_timeout timeout; 无数据传输时,保持连接状态的超时时长,默认为10m proxy_connect_timeout time; 设置nginx与被代理的服务器尝试建立连接的超时时长,默认为60s stream { #redis服务器 upstream redis_server { #hash $remote_addr consistent; server 192.168.80.130:6379 max_fails=3 fail_timeout=30s; } server { listen 192.168.80.110:6379; proxy_connect_timeout 3s; proxy_timeout 3s; proxy_pass redis_server; } #数据库服务器 upstream mysql_server { least_conn; server 192.168.80.140:3306 max_fails=3 fail_timeout=30s; } server { listen 192.168.80.110:3306; proxy_connect_timeout 3s; proxy_timeout 3s; proxy_pass mysql_server; } } [root@ ]# vim /apps/nginx/conf/nginx.conf 21 include /apps/nginx/conf/tcp/tcp.conf; #注意此处的include与http模块平级 [root@localhost7B ~]# netstat -antlp | grep 6379 tcp 0 0 192.168.80.110:6379 0.0.0.0:* LISTEN 96215/nginx: master [root@localhost7B ~]# [root@localhost7B ~]# netstat -antlp | grep 3306 tcp 0 0 192.168.80.110:3306 0.0.0.0:* LISTEN 96215/nginx: master redis服务器配置 yum install redis -y vim /etc/redis.conf bind 0.0.0.0 systemctl start redis systemctl enable redis netstat -antlp | grep 6379 #测试成功 # redis-cli -h 192.168.80.110 192.168.80.110:6379> set name zzhz OK 192.168.80.110:6379> get name "zzhz" 数据库服务器配置 yum install mariadb mariadb-server -y systemctl start mariadb systemctl enable mariadb mysql_secure_installation mysql -uroot -p123456 MariaDB [(none)]> GRANT ALL PRIVILEGES ON *.* TO 'root' @ '%' IDENTIFIED BY '123456' WITH GRANT OPTION; MariaDB [(none)]> FLUSH PRIVILEGES; #测试成功 mysql -uroot -p123456 -h 192.168.80.110 |
分类:
nginx
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了