Nginx 在1.9.0版本发布以前如果要想做到基于TCP的代理及负载均衡需要通过打名为 nginx_tcp_proxy_module 的第三方patch来实现,该模块的代码托管在github上网址:https://github.com/yaoweibin/nginx_tcp_proxy_module/。

Nginx 从1.9.0开始发布ngx_stream_core_module模块,该模块支持tcp代理及负载均衡。

今天我们就要来简单测试一下 Nginx 的 ngx_stream_core_module 模块。

安装Nginx并启用模块

ngx_stream_core_module这个模块并不会默认启用,需要在编译时通过指定--with-stream参数来激活这个模块。

  • 编译安装
$ yum -y install proc* openssl* pcre*
$ wget http://nginx.org/download/nginx-1.9.4.tar.gz
$ tar zxvf nginx-1.9.4.tar.gz
$ cd nginx-1.9.4
$ ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-threads --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'
$ make
$ make install
  • 配置stream模块

实例一:测试MYSQL负载均衡

stream模块必需在nginx.conf中配置

$ mv nginx.conf{,.bak}
$ vim /etc/nginx/nginx.conf worker_processes auto;
events {
worker_connections 1024;
}
error_log /var/log/nginx_error.log info; stream {
upstream mysqld {
hash $remote_addr consistent;
server 192.168.1.42:3306 weight=5 max_fails=1 fail_timeout=10s;
server 192.168.1.43:3306 weight=5 max_fails=1 fail_timeout=10s;
} server {
listen 3306;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass mysqld;
} }

实例二:实现SSH转发

upstream ssh {
hash $remote_addr consistent;
server 192.168.1.42:22 weight=5;
} server {
listen 2222;
proxy_pass ssh;
}

实例三:官方一个较完整的配置示例

stream模块的配置里还支持类似server unix:/tmp/backend3.sock;这样的sock数据交换接口,也可以直接proxy_pass unix:/tmp/stream.socket;

worker_processes auto;
error_log /var/log/nginx/error.log info;
events {
worker_connections 1024;
} stream {
upstream backend {
hash $remote_addr consistent; server backend1.example.com:12345 weight=5;
server 127.0.0.1:12345 max_fails=3 fail_timeout=30s;
server unix:/tmp/backend3;
} server {
listen 12345;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass backend;
} server {
listen [::1]:12345;
proxy_pass unix:/tmp/stream.socket;
}
}

ngx_stream_core_module也同样的支持tcp长连接保持。keepidle是保持时间,keepintvl是间隔时间 ,keepcnt是发送的个数。

so_keepalive=on|off|[keepidle]:[keepintvl]:[keepcnt]

参考文档

http://www.google.com
http://zhangge.net/5037.html
http://nginx.org/en/docs/stream/ngx_stream_core_module.html

http://nginx.org/en/docs/stream/ngx_stream_proxy_module.html#proxy_timeout

现在使用Nginx实现TCP反向代理的更多相关文章

  1. nginx启用TCP反向代理日志配置

    Nginx使用TCP反向代理日志配置不同于http 修改nginx配置文档/usr/local/nginx/conf/nginx.conf 设置日志格式 stream { log_format pro ...

  2. nginx之TCP反向代理

    实现Nginx tcp负载均衡 Nginx在1.9.0版本开始支持tcp模式的负载均衡,在1.9.13版本开始支持udp协议的负载,udp主要用于DNS的域名解析,其配置方式和指令和http 代理类似 ...

  3. 源码安装Nginx加TCP反向代理模块

    说明: 安装方式是源码编译安装,因此先安装相关依赖,否则报错. yum -y install gcc* patch openssl openssl-devel 安装步骤: 下载nginx源码包: wg ...

  4. 使用Nginx实现TCP反向代理

    Nginx 在1.9.0版本发布以前如果要想做到基于TCP的代理及负载均衡需要通过打名为 nginx_tcp_proxy_module 的第三方patch来实现,该模块的代码托管在github上网址: ...

  5. nginx作为TCP反向代理

    基于windows环境 基于nginx1.12.2版本 1. 解压nginx 2. 修改conf配置 # 打开conf/nginx,conf文件,写入以下配置 # upstream backend 里 ...

  6. ContOS7中使用Nginx进行TCP反向代理

    一.安装Nginx 1.下载:http://nginx.org/en/download.html wget http://nginx.org/download/nginx-1.16.1.tar.gz ...

  7. 简易nginx TCP反向代理设置

    nginx从1.9.0开始支持TCP反向代理,之前只支持HTTP.这是我的系统示意图: 为何需要? 为什么需要反向代理?主要是: 负载均衡 方便管控 比如我现在要更新后端服务器,如果不用负载均衡的话, ...

  8. Nginx设置Https反向代理,指向Docker Gitlab11.3.9 Https服务

    目录 目录 1.GitLab11.3.9的安装 2.域名在阿里云托管,申请免费的1年证书 3.Gitlab 的 https 配置 4.Nginx 配置 https,反向代理指向 Gitlab 配置 目 ...

  9. Nginx 部署、反向代理配置、负载均衡

    Nginx 部署.反向代理配置.负载均衡 最近我们的angular项目部署,我们采用的的是Nginx,下面对Nginx做一个简单的介绍. 为什么选择Nginx 轻:相比于Apache,同样的web服务 ...

随机推荐

  1. [转]Virtualbox主机和虚拟机之间文件夹共享及双向拷贝(Windows<->Windows, Windows<->Linux)

    本文转自:https://www.jb51.net/article/97271.htm 最近学习Virtualbox的一些知识,记录下,Virtualbox下如何实现主机和虚拟机之间文件夹共享及双向拷 ...

  2. .NET CORE 设置cookie以及获取cookie

    使用我这个方式的前提是在mvc中,确认你安装了:Microsoft.AspNetCore.Mvc. 然后在继承了Controller的类型中使用我所说的方法. 直接使用即可,我是封装了方法供我自己使用 ...

  3. php 函数小技巧(一)

    密码加密与验证 password_hash — 创建密码的哈希(hash) string password_hash ( string $password , integer $algo [, arr ...

  4. Hibernate入门(六)---------HQL语句

    Query: 代表面向对象的一个Hibernate查询操作.在Hibernate中,通常使用session.createQuery()方法接收一个HQL语句,然后调用Query的 list()或uni ...

  5. 正则去除html字符串中的注释、标签、属性

    var str = '<!-- 注释1 --><h1 style="color:#00ff00;text-align: center;">ProsperLe ...

  6. 性能测试 CentOS下结合InfluxDB及Grafana图表实时展示JMeter相关性能数据

    CentOS下结合InfluxDB及Grafana图表实时展示JMeter相关性能数据   by:授客 QQ:1033553122 实现功能 1 测试环境 1 环境搭建 2 1.安装influxdb ...

  7. Android内存优化(四)LeakCanary使用详解

    LeakCanary是检测App内存泄露的工具, 内存泄露是Android开发中常见的问题, 使用程序的稳定性下降. LeakCanary 的机制如下: RefWatcher.watch() 会以监控 ...

  8. WPF:解决DataGrid横向滚动条无法显示的问题

    DataGrid的最后一列的宽度设置为“Width=”auto””即可. 如果显示指定长度或者设置为“*”,那么不管怎么拖动列头,或者不管行里面的内容有没有超过DataGrid的显示区域,DataGr ...

  9. 使用VSTS的Git进行版本控制(三)——评审历史记录

    使用VSTS的Git进行版本控制(三)--评审历史记录 Git使用存储在每个提交中的父引用信息来管理开发的完整历史记录.评审该提交历史记录,能够找出文件更改的时间,并确定代码版本之间的差异. Git使 ...

  10. 穷举,迭代,while循环

    1. 2.大马驼2石粮食,中等马驼1石粮食,两头小马驼1石粮食,要用100匹马,驼100石粮食,该如何分配? 3. 4. 5. 6.