Nginx在1.9.0版本開始支持tcp模式的負載均衡,在1.9.13版本開始支持udp協議的負載,udp主要用於DNS的域名解析,其配置方式和指令和http 代理類似,其基於ngx_stream_proxy_module模塊實現tcp負載,另外基於模塊ngx_stream_upstream_module實現後端服務器分組轉發、權重分配、狀態監測、調度算法等高級功能。
tcp負載均衡配置參數:
stream { #定義stream
upstream backend { #定義後端服務器
hash $remote_addr consistent; #定義調度算法
server backend1.example.com:12345 weight=5; #定義具體server
server 127.0.0.1:12345 max_fails=3 fail_timeout=30s;
server unix:/tmp/backend3;
}
upstream dns { #定義後端服務器
server 192.168.0.1:53535; #定義具體server
server dns.example.com:53;
}
server { #定義server
listen 12345; #監聽IP:PORT
proxy_connect_timeout 1s; #連接超時時間
proxy_timeout 3s; #轉發超時時間
proxy_pass backend; #轉發到具體服務器組
}
server {
listen 127.0.0.1:53 udp reuseport;
proxy_timeout 20s;
proxy_pass dns;
}
server {
listen [::1]:12345;
proxy_pass unix:/tmp/stream.socket;
}
}
負載均衡實例–Redis:
#後端web服務
✘root@U8-2: ~✘# apt -y install redis
✘root@U8-2: ~✘# vim /etc/redis/redis.conf
#修改該行
bind 0.0.0.0
✘root@U8-2: ~✘# systemctl start redis
✘root@U8-2: ~✘# lsof -i:6379
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
redis-ser 2739 redis 6u IPv4 93193 0t0 TCP localhost:6379 (LISTEN)
redis-ser 2739 redis 7u IPv6 93196 0t0 TCP localhost:6379 (LISTEN)
#NGINX代理服務器
[root@U8: ~]# mkdir /apps/nginx/conf/tcp
[root@U8: ~]# vim /apps/nginx/conf/tcp/tcp.conf
stream {
upstream redis_server {
server 192.168.124.32:6379 max_fails=3 fail_timeout=30s;
}
server {
listen 192.168.124.30:6379;
proxy_connect_timeout 3s;
proxy_timeout 3s;
proxy_pass redis_server;
}
}
[root@U8: ~]# vim /apps/nginx/conf/nginx.conf
#添加此行
include /apps/nginx/conf/tcp/tcp.conf; #此處的include與http模塊平級
[root@U8: ~]# /apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@U8: ~]# /apps/nginx/sbin/nginx -s reload
[root@U8: ~]# ss -tnl | grep 6379
LISTEN 0 511 192.168.124.30:6379 0.0.0.0:*
#測試通過nginx 負載連接redis:
✘root@U8-2: ~✘# redis-cli -h 192.168.124.30
192.168.124.30:6379> set name dushansao
OK
192.168.124.30:6379> get name
“dushansao”
192.168.124.30:6379>
負載均衡實例:MySQL
#後端web服務安裝mysql
✘root@U8-2: ~✘# apt -y install mariadb-server
✘root@U8-2: ~✘# systemctl start mariadb
#啓動安全初始腳本,設置mysql管理員密碼:123456
✘root@U8-2: ~✘# mysql_secure_installation
#登錄maraidb
✘root@U8-2: ~✘# mysql -uroot -p123456
MariaDB [(none)]> GRANT ALL PRIVILEGES ON . TO ‘root’@’%’ IDENTIFIED BY ‘123456’ WITH GRANT OPTION;
Query OK, 0 rows affected (0.00 sec)
MariaDB [(none)]> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)
#NGINX代理服務器
[root@U8: ~]# vim /apps/nginx/conf/tcp/tcp.conf
stream {
upstream redis_server {
server 192.168.124.32:6379 max_fails=3 fail_timeout=30s;
}
upstream mysql_server {
least_conn;
server 192.168.124.32:3306 max_fails=3 fail_timeout=30s;
}
server {
listen 192.168.124.30:3306;
proxy_connect_timeout 6s;
proxy_timeout 15s;
proxy_pass mysql_server;
}
server {
listen 192.168.124.30:6379;
proxy_connect_timeout 3s;
proxy_timeout 3s;
proxy_pass redis_server;
}
}
[root@U8: ~]# /apps/nginx/sbin/nginx -t
nginx: the configuration file /apps/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /apps/nginx/conf/nginx.conf test is successful
[root@U8: ~]# /apps/nginx/sbin/nginx -s reload
#後端web
✘root@U8-2: ~✘# vim /etc/mysql/mariadb.conf.d/50-server.cnf
#找出此行將其註釋
#bind-address = 127.0.0.1
#在該行下設置超時時長
[mysqld]
wait_timeout=86400 #8小時
#重啓mariadb
✘root@U8-2: ~✘# systemctl restart mariadb
#測試通過nginx負載連接MySQL:
#後端web
✘root@U8-2: ~✘# mysql -uroot -p123456 -h 192.168.124.30
MariaDB [(none)]> create database dushansao;
Query OK, 1 row affected (0.00 sec)
MariaDB [(none)]> show databases;
+--------------------+
| Database |
+--------------------+
| dushansao |
| information_schema |
| mysql |
| performance_schema |
+--------------------+
4 rows in set (0.00 sec)
MariaDB [(none)]>