nginx4层代理详解
nginx四层负载均衡实现主要就是stream模块
环境
Nginx 10.0.0.78
mysql 10.0.0.71
配置
cat /etc/nginx/nginx.conf
stream {
include /etc/nginx/stream_conf.d/*.conf;
}
mkdir /etc/nginx/stream_conf.d/
cat /etc/nginx/stream_conf.d/lb4-mysql.conf
upstream mysql{
server 10.0.0.71:3306;
}
server {
listen 3306; # 监听的机器本地不能有3306端口。
proxy_pass mysql;
}
# 添加之后若是报错没有stream模块则执行:
yum install -y nginx-mod-stream
yum install -y nginx-all-modules
# 然后重启nginx即可
测试
mysql -h10.0.0.78 -uroot -p'1qaz@WSX'
Welcome to the MariaDB monitor. Commands end with ; or \g.
Your MariaDB connection id is 20
Server version: 5.5.68-MariaDB MariaDB Server
Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
MariaDB [(none)]>
注意
- nginx.conf里不允许多个stream标签。
- stream标签配置为主配置文件nginx.conf main区段。
- 可以在主配置的stream标签里嵌入include /etc/nginx/stream_conf.d/*.conf;