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

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

[[email protected] ~]# 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配置

[[email protected] ~]# vim /apps/nginx/conf/nginx.conf
include /apps/nginx/conf/tcp/*.conf; #需要定在main配置段

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

[[email protected] ~]# 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
[[email protected] ~]# nginx -s reload

mariadb端操作

1.安装MySQL

[[email protected] ~]# yum install mariadb-server -y

2.启动MySQL服务

[[email protected] ~]# systemctl start mariadb

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

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

测试

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

[[email protected] ~]# mysql -uroot -p111111 -h172.20.27.10 -P3306
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.60-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)]> 

原文地址:https://blog.51cto.com/11886307/2403933

时间: 2024-08-18 22:42:11

nginx以upstream分组的方式实现tcp反向代理的相关文章

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

nginx反向代理高级应用 nginx可以将客户的的请求转发至后端服务器但是无法转至特定一组的服务器,而且不能对后端服务器提供响应的服务器状态检测,但是Nginx可以基于ngx_http_upstream_module模块提供服务器分组.转发.权重分配.状态检测.调度算法等高级功能upstream_module的官方文档:http://nginx.org/en/docs/http/ngx_http_upstream_module.html upstream Syntax: upstream na

nginx之TCP反向代理

nginx从1.9.0版本以后支持面向TCP的反向代理.莫约是2015年初发布的. 注:如今最新版nginx为1.11.12 nginx从1.9.0后引入模块ngx_stream_core_module,模块是没有编译的,需要用到编译需添加--with-stream配置参数,stream负载均衡官方配置样例 配置编译的时候需要加上 先切换到nginx解压后目录内 # ./configure --prefix=/usr/local/nginx --user=www --group=www --wi

用nginx TCP反向代理作mail邮件代理

用nginx TCP反向代理作mail邮件代理 用nginx TCP反向代理作mail邮件代理 1. 背景 2. Nginx安装(包括nginx_upstream_check_module) 3. Nginx配置 4. 总结 1. 背景 新版本nginx有TCP反向代理功能,nginx的mail proxy配置认证又太麻烦,于是就想用TCP反向功能作mail代理. 2. Nginx安装(包括nginx_upstream_check_module) cd /tmptar zxf pcre-8.35

Nginx配置二级目录/路径 映射不同的反向代理和规避IP+端口访问

   当配置Nginx来映射不同的服务器 可以通过二级路径来反向代理 来解决一个外网端口实现多个服务访问. 配置如下: server { listen 80; server_name demo.domain.com; #通过访问service二级目录来访问后台 location /service/ { #DemoBackend1后面的斜杠是一个关键,没有斜杠的话就会传递service到后端节点导致404 proxy_pass http://backend1/; proxy_redirect of

Nodejs实现TCP反向代理

场景: 你有若干机器,却只有一台能被外网访问,如果要让外网访问到你其它机器上的服务,就需要做反向代理,在上一篇文章中,我们用nodejs轻松实现了http反向代理.如果是TCP服务,例如mysql数据库,同样用nodejs来做实现一个反向代理也是很容易的. 第一种方式: var net = require('net'); // parse "80" and "localhost:80" or even "42mEANINg-life.com:80"

Linux 下面安装 nginx 以及进行TCP反向代理、负载均衡的过程

1. 下载安装nginx 注意 因为stream 并不是 nginx自带的module  所以需要 在安装是 通过 --with 的方式增加上. 下载必要的程序包 # openssl wget https://www.openssl.org/source/openssl-1.1.1c.tar.gz #zilib wget http://www.zlib.net/zlib-1.2.11.tar.gz #pcre wget https://netix.dl.sourceforge.net/proje

Nginx实现TCP反向代理

默认Nginx只支持http的反向代理,要想nginx支持tcp的反向代理,还需要在编译时增加tcp代理模块支持,即nginx_tcp_proxy_module 下面操作步骤只让nginx支持tcp_proxy,没有加入prce.gzip.ssl等功能,如需要,可自行在编译时加上相关参数. wget https://github.com/yaoweibin/nginx_tcp_proxy_module/archive/master.zip unzip master cd nginx-1.6.2

nginx之安装、多虚拟主机、反向代理和负载均衡

一.web服务器与web框架 1.web服务器简介 Web 网络服务是一种被动访问的服务程序,即只有接收到互联网中其他主机发出的请求后才会响应,最终用于提供服务程序的Web服务器会通过 HTTP(超文本传输协议)或 HTTPS(安全超文本传输协议)把请求的内容传送给用户. 目前能够提供 Web 网络服务的程序有 IIS.Nginx 和 Apache 等.其中,IIS(Internet Information Services,互联网信息服务)是Windows系统中默认的Web服务程序Nginx

nginx做nodejs(express等通用)反向代理

首先配置环境nginx+nodejs...(没有请看我的其他文章,此处不重复) cd 到nginx的site-available目录 ubuntu的在 cd /etc/nginx/site-available/ 创建一个server文件 sudo touch testServer 然后可以copy下面的代码进去了(勤劳的coder可以手写一遍的哦) http{ keepalive 65; #超时 gzip on; #是否开启压缩模块 gzip_comp_level 6; #压缩比例 1-9 gz