Nginx 反向代理(三):四层代理和负载均衡
|
admin
2025年6月29日 18:36
本文热度 48
|
Nginx在1.9.0版本开始支持tcp模式的负载均衡,在1.9.13版本开始支持udp协议的负载,udp主要用于DNS的域名解析,其配置方式和指令和http 代理类似,其基于ngx_stream_proxy_module模块实现tcp负载,另外基于模块ngx_stream_upstream_module实现后端服务器分组转发、权重分配、状态监测、调度算法等高级功能
如果编译安装,需要指定 --with-stream 选项才能支持ngx_stream_proxy_module模块
http://nginx.org/en/docs/stream/ngx_stream_proxy_module.html
https://nginx.org/en/docs/stream/ngx_stream_upstream_module.html
stream {
server {
listen 3306;
proxy_pass 10.0.0.210:3306;
}
server {
listen 6379;
proxy_pass 10.0.0.159:6379;
}
}
[root@ubuntu ~]
mysql> create user proxyer@'10.0.0.%' identified by '123456';
Query OK, 0 rows affected (0.01 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
[root@ubuntu ~]
LISTEN 0 70 127.0.0.1:33060 0.0.0.0:* users: (("mysqld",pid=2461,fd=21))
LISTEN 0 151 127.0.0.1:3306 0.0.0.0:* users: (("mysqld",pid=2461,fd=23))
[root@ubuntu ~]
skip-name-resolve
[root@ubuntu ~]
[root@ubuntu ~]
LISTEN 0 70 *:33060 *:* users: (("mysqld",pid=3928,fd=21))
LISTEN 0 151 *:3306 *:* users: (("mysqld",pid=3928,fd=23))
[root@ubuntu ~]
[root@ubuntu ~]
LISTEN 0 511 127.0.0.1:6379 0.0.0.0:* users:(("redisserver",pid=2153,fd=6))
LISTEN 0 511 [::1]:6379 [::]:* users:(("redisserver",pid=2153,fd=7))
[root@ubuntu ~]
protected-mode no
[root@ubuntu ~]
[root@ubuntu ~]
LISTEN 0 511 0.0.0.0:6379 0.0.0.0:* users:(("redisserver",pid=2480,fd=7))
LISTEN 0 511 [::]:6379 [::]:* users:(("redisserver",pid=2480,fd=6))
[root@ubuntu ~]
[root@ubuntu ~]
[root@ubuntu ~]
mysql> show processlist\G
*************************** 2. row ***************************
Id: 9
User: proxyer
Host: 10.0.0.208:42714
db: NULL
Command: Sleep
Time: 358
State:
Info: NULL
[root@ubuntu ~]
[root@ubuntu ~]
FIN-WAIT-2 0 0 10.0.0.159:6379 10.0.0.208:39838 timer: (timewait,43sec,0) ino:0 sk:1004
[root@ubuntu ~]
mysql> show processlist\G
*************************** 1. row ***************************
Id: 12
User: proxyer
Host: 10.0.0.206:49452
db: NULL
Command: Query
Time: 0
State: init
Info: show processlist
1 row in set, 1 warning (0.00 sec)
[root@ubuntu ~]
10.0.0.206:6379> keys *
(empty array)
10.0.0.206:6379> set test 123
OK
10.0.0.206:6379> keys *
1) "test"
stream {
upstream mysql {
server 10.0.0.210:3306;
server 10.0.0.159:3306;
}
upstream redis {
server 10.0.0.210:6379;
server 10.0.0.159:6379;
}
server {
listen 3306;
proxy_pass mysql;
}
server {
listen 6379;
proxy_pass redis;
}
}
— END —
阅读原文:原文链接
该文章在 2025/7/1 23:14:44 编辑过