Carry の Blog Carry の Blog
首页
  • Nginx
  • Prometheus
  • Iptables
  • Systemd
  • Firewalld
  • Docker
  • Sshd
  • DBA工作笔记
  • MySQL
  • Redis
  • TiDB
  • Elasticsearch
  • Python
  • Shell
  • MySQL8-SOP手册
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Carry の Blog

好记性不如烂键盘
首页
  • Nginx
  • Prometheus
  • Iptables
  • Systemd
  • Firewalld
  • Docker
  • Sshd
  • DBA工作笔记
  • MySQL
  • Redis
  • TiDB
  • Elasticsearch
  • Python
  • Shell
  • MySQL8-SOP手册
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 工作笔记

  • K8S

  • Systemd

  • Nginx

    • Nginx添加用户认证
    • 利用nginx+sftp实现一个可供用户下载的服务
    • nginx配置文件及模块
    • 通过脚本按天切割nginx的日志
    • nginx通过四层代理实现端口转发
    • NGINX基于cookie针对同一域名进行分流转发
    • nginx利用内置模块配置限速限流
    • 利用NGINX内置模块mirror进行流量复制等操作
    • 使用$remote_user字段记录访问NGINX的用户
    • 从NGINX自身配置文件中定义访问日志按时间切割
    • NGINX配置单独代理百度的sitemap文件
    • nginx配置微信小程序校验及其他
    • nginx配置gzip压缩
    • 由Nginx集中代理分散的PHP集群的实践
    • http状态码详解
    • OpenResty-1-13-6-2-新增ldap模块儿
    • 排查NGINX的open_file_cache导致发布后访问404的问题
    • 制作OpenResty-1-19-9-1的RPM包
    • nginx-proxy-managar使用笔记
    • Nginx 的端口复用:提升服务器并发能力
  • Supervisord

  • OpenLdap

  • OpenVPN

  • GitLab

  • Sshd

  • WebDev

  • Docker

  • Prometheus

  • Rclone

  • Iptables

  • Firewalld

  • Linux笔记
  • Nginx
Carry の Blog
2018-11-08

nginx通过四层代理实现端口转发收藏(二丫讲梵)备查

公司原有的测试数据库在主机192.168.10.5上边,现在数据库转移到了192.168.10.4上,为了不让各个地方都需要更改地址,现在需要一个四层代理工具,将原来请求到192.168.10.5的3306端口转发到192.168.10.4的3306端口。

这个工具,用到了 nginx 的四层代理。

官方文档:http://nginx.org/en/docs/stream/ngx_stream_core_module.html

四层代理依赖模块ngx_stream_core_module,该模块自 1.9.0 版开始可用。默认情况下,此模块不构建,应使用配置参数启用 --with-stream。

安装过程简示:

[root@linux-node1 src]# tar xf nginx-1.10.3.tar.gz 
[root@linux-node1 src]# cd nginx-1.10.3
[root@linux-node1 nginx-1.10.3]# useradd -s /sbin/nologin -M www
[root@linux-node1 nginx-1.10.3]# yum install gcc gcc-c++ zlib-devel pcre-devel openssl openssl-devel -y
[root@linux-node1 nginx-1.10.3]# ./configure --prefix=/usr/local/nginx-1.10.3 --user=www --group=www --with-http_ssl_module --with-http_stub_status_module --with-file-aio --with-stream
[root@linux-node1 nginx-1.10.3]# make && make install 
1
2
3
4
5
6

可以通过nginx -V查看一下是否将上述模块编译进来,如果没有,可以重新编译一下。

img

来到主配置:

worker_processes  1;
events {
    worker_connections  1024;
}
stream {  
        upstream tcp_proxy {
        hash $remote_addr consistent;  #远程地址做个hash
        server 192.168.10.4:22;
   }
      server {
        listen 2222;
        proxy_connect_timeout 1s;
        proxy_timeout 10s;  #后端连接超时时间
        proxy_pass tcp_proxy;
     }
  }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

此配置是将本机的 2222 端口转发到 192.168.10.4 的 22 端口,配置之后,试验一下:

[root@7-3 nginx]$ssh -p 2222 [email protected]
The authenticity of host '[192.168.10.5]:2222 ([192.168.10.5]:2222)' can't be established.
ECDSA key fingerprint is 05:2f:63:e9:87:be:b4:44:d3:d7:77:a0:52:e0:4f:2f.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '[192.168.10.5]:2222' (ECDSA) to the list of known hosts.
[email protected]'s password:
Last login: Wed Nov  7 15:24:33 2018 from 192.168.10.1
[root@7-2 ~]$hostname -I
192.168.10.4
1
2
3
4
5
6
7
8
9

刚刚设置了 10 的超时,如果需要的话,可以将之注释掉。

同理,配置数据库端口的转发也就非常简单了:

worker_processes  1;
events {
    worker_connections  1024;
}
stream {  
        upstream tcp_proxy {
        hash $remote_addr consistent;  #远程地址做个hash
        server 192.168.10.4:3306;
   }
      server {
        listen 3306;
        proxy_connect_timeout 1s;
       # proxy_timeout 10s;  #后端连接超时时间
        proxy_pass tcp_proxy;
     }
  }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

这样一来,用户连接192.168.10.5:3306的时候,就会被转发到192.168.10.4:3306了。

本文参考地址:https://www.cnblogs.com/w787815/p/6682824.html

#nginx
上次更新: 4/24/2025

← 通过脚本按天切割nginx的日志 NGINX基于cookie针对同一域名进行分流转发→

最近更新
01
tidb fast ddl
04-04
02
TiDB配置文件调优 原创
04-03
03
如何移除TiDB中的表分区 原创
04-03
更多文章>
Theme by Vdoing
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式