博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
nginx以upstream分组的方式实现tcp反向代理
阅读量:7090 次
发布时间:2019-06-28

本文共 2418 字,大约阅读时间需要 8 分钟。

nginx以upstream分组的方式实现tcp反向代理

nginx在1.9版本开始支持tcp模式的负载均衡,在1.9.13版本开始支持udp协议的负载均衡,udp主要用于DNS的域名解析,其配置方式和指令和http代理类似,其基于ngx_steam_proxy_module模块实现tcp负载,另外基于ngx_stream_upstream_module实现后端服务器的分组转发、权重分配、状态监测、调度算法等高级功能

官方文档的example:

worker_processes auto;error_log /var/log/nginx/error.log info;events {    worker_connections  1024;}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 dns.example.com:53;    }    server {        listen 12345;        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;    }}

upstream分组实现tcp反向代理

server IP
nginx 172.20.27.10
mysql 172.20.27.20
client 172.20.27.100

反向代理端操作

1.在nginx服务器上定义stream

[root@nginx ~]# vim /apps/nginx/conf/tcp/tcp_proxy.conf stream {    #定义stream    upstream mysql_host {   #定义后端服务器        server 172.20.27.20:3306;  }    server {        listen 172.20.27.10:3306;        proxy_connect_timeout 5s;        proxy_timeout 5s;        proxy_pass mysql_host  }}

2.在著配置文件中导入tcp_proxy.conf配置

[root@nginx ~]# vim /apps/nginx/conf/nginx.confinclude /apps/nginx/conf/tcp/*.conf; #需要定在main配置段

3.检查配置文件启动服务

[root@nginx ~]# nginx -tnginx: the configuration file /apps/nginx/conf/nginx.conf syntax is oknginx: configuration file /apps/nginx/conf/nginx.conf test is successful[root@nginx ~]# nginx -s reload

mariadb端操作

1.安装MySQL

[root@mariadb ~]# yum install mariadb-server -y

2.启动MySQL服务

[root@mariadb ~]# systemctl start mariadb

3.为反向代理授权一个账户

[root@mariadb ~]# msyql -e "GRANT ALL PRIVILEGES ON *.* TO 'root'@'172.20.27.10' IDENTIFIED BY '111111';"

测试

使用客户端去访问反向代理的3306端口

[root@client ~]# mysql -uroot -p111111 -h172.20.27.10 -P3306Welcome to the MariaDB monitor.  Commands end with ; or \g.Your MariaDB connection id is 8Server version: 5.5.60-MariaDB MariaDB ServerCopyright (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)]>

转载于:https://blog.51cto.com/11886307/2403933

你可能感兴趣的文章
沟通的艺术之幻灯片这奇女子
查看>>
一张图介绍CCIE
查看>>
VM增加centos6.5磁盘容量
查看>>
Servlet容器启动过程
查看>>
CentOS安装配置nagios(1)
查看>>
RedHat 6.4 搭建rhcs集群
查看>>
三生万物:决策树
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
Python爬虫框架Scrapy学习笔记原创
查看>>
大数据时代怎么做
查看>>
java基本语法
查看>>
细说HTTP之上篇
查看>>
将Eclipse Maven项目 导入 IDEA 步骤 成功运行 已测试!~LC
查看>>
Exchange Server 2010的俩种版本比较
查看>>
asp.net 插入视频
查看>>
laravel中的表单请求类型和CSRF防护(六)
查看>>
有1000瓶水,其中有一瓶有毒,小白鼠只要尝一点带毒的水24小时后就会死亡,至少要多...
查看>>
我的友情链接
查看>>
监控指定文件所有机器的网络状况
查看>>