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. ___树形菜单Ztree.js显示.

    ----视图@{ Layout = null;} <!DOCTYPE html><HTML><HEAD> <TITLE> ZTREE DEMO - be ...

  2. Python网络编程Socket之协程

    一.服务端 __author__ = "Jent Zhang" import socket import gevent from gevent import monkey monk ...

  3. mysql的coalesce使用技巧

    今天无意间发现mysql的coalesce, coalesce()解释:返回参数中的第一个非空表达式(从左向右依次类推): 使用示例:a,b,c三个变量. ,); // Return 2 select ...

  4. 【Java每日一题】20170329

    20170328问题解析请点击今日问题下方的“[Java每日一题]20170329”查看(问题解析在公众号首发,公众号ID:weknow619) package Mar2017; public cla ...

  5. window 服务器的Tomcat 控制台日志保存到日志文件.

    在Linux系统中,Tomcat 启动后默认将很多信息都写入到 catalina.out 文件中,我们可以通过tail  -f  catalina.out 来跟踪Tomcat 和相关应用运行的情况. ...

  6. Elasticsearch(ES)API 增删查改常用操作

    常用操作 查询所有数据 POST http://192.168.97.173:27009/logstash_test_2018/doc/_search { "query": { & ...

  7. 7;XHTML表单

    1.表单的功能结构 2.文本栏.密码栏.隐藏栏 3.复选栏.单选栏 4.窗体栏位.区块栏位 5.按钮.图像按钮 6.允许上传文件 7.表单的外框和标题 8.元件的次序和快捷键 表单是提供让读者在网页上 ...

  8. AOJ1370: Hidden Anagrams(hash)

    题意 题目链接 Sol 直接对出现的次数hash即可,复杂度\(O(26n^2)\) 一开始没判长度条件疯狂wa #include<bits/stdc++.h> //#define int ...

  9. 扩展Linux磁盘空间

    适用于虚拟机内系统HyperV/Centos7已测 先为虚拟磁盘扩容,比如10G加到20G 最好进入单用户模式:init 1 进入管理UI:fdisk -l /dev/sda依次n {new part ...

  10. 【数据分析】线性回归与逻辑回归(R语言实现)

    文章来源:公众号-智能化IT系统. 回归模型有多种,一般在数据分析中用的比较常用的有线性回归和逻辑回归.其描述的是一组因变量和自变量之间的关系,通过特定的方程来模拟.这么做的目的也是为了预测,但有时也 ...