Nginx在1.9.0版本開始支持tcp模式的負(fù)載均衡,在1.9.13版本開始支持udp協(xié)議的負(fù)載,udp主要用于DNS的域名解析,其配置方式和指令和http 代理類似,其基于ngx_stream_proxy_module模塊實(shí)現(xiàn)tcp負(fù)載,另外基于模塊ngx_stream_upstream_module實(shí)現(xiàn)后端服務(wù)器分組轉(zhuǎn)發(fā)、權(quán)重分配、狀態(tài)監(jiān)測、調(diào)度算法等高級(jí)功能
如果編譯安裝,需要指定 --with-stream 選項(xiàng)才能支持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
4.6.2 實(shí)現(xiàn)TCP協(xié)議的反向代理
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"
4.6.3 實(shí)現(xiàn)TCP協(xié)議的負(fù)載均衡
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 編輯過