现在使用Nginx实现TCP反向代理
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反向代理的更多相关文章
- nginx启用TCP反向代理日志配置
Nginx使用TCP反向代理日志配置不同于http 修改nginx配置文档/usr/local/nginx/conf/nginx.conf 设置日志格式 stream { log_format pro ...
- nginx之TCP反向代理
实现Nginx tcp负载均衡 Nginx在1.9.0版本开始支持tcp模式的负载均衡,在1.9.13版本开始支持udp协议的负载,udp主要用于DNS的域名解析,其配置方式和指令和http 代理类似 ...
- 源码安装Nginx加TCP反向代理模块
说明: 安装方式是源码编译安装,因此先安装相关依赖,否则报错. yum -y install gcc* patch openssl openssl-devel 安装步骤: 下载nginx源码包: wg ...
- 使用Nginx实现TCP反向代理
Nginx 在1.9.0版本发布以前如果要想做到基于TCP的代理及负载均衡需要通过打名为 nginx_tcp_proxy_module 的第三方patch来实现,该模块的代码托管在github上网址: ...
- nginx作为TCP反向代理
基于windows环境 基于nginx1.12.2版本 1. 解压nginx 2. 修改conf配置 # 打开conf/nginx,conf文件,写入以下配置 # upstream backend 里 ...
- ContOS7中使用Nginx进行TCP反向代理
一.安装Nginx 1.下载:http://nginx.org/en/download.html wget http://nginx.org/download/nginx-1.16.1.tar.gz ...
- 简易nginx TCP反向代理设置
nginx从1.9.0开始支持TCP反向代理,之前只支持HTTP.这是我的系统示意图: 为何需要? 为什么需要反向代理?主要是: 负载均衡 方便管控 比如我现在要更新后端服务器,如果不用负载均衡的话, ...
- Nginx设置Https反向代理,指向Docker Gitlab11.3.9 Https服务
目录 目录 1.GitLab11.3.9的安装 2.域名在阿里云托管,申请免费的1年证书 3.Gitlab 的 https 配置 4.Nginx 配置 https,反向代理指向 Gitlab 配置 目 ...
- Nginx 部署、反向代理配置、负载均衡
Nginx 部署.反向代理配置.负载均衡 最近我们的angular项目部署,我们采用的的是Nginx,下面对Nginx做一个简单的介绍. 为什么选择Nginx 轻:相比于Apache,同样的web服务 ...
随机推荐
- popupWindow设置后完美解决返回键响应无效的方案以及popupWindow背景透明方案
// 点击其他地方消失 viewPuwAddNew.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouc ...
- [转]Raw Queries in Laravel
本文转自:https://fideloper.com/laravel-raw-queries Business logic is often complicated. Because of this, ...
- Mac下写博客工具ecto相关资料
下载地址: https://www.macupdate.com/app/mac/8918/ecto 相关注册码: http://www.cnblogs.com/yssgyw/p/3284501.htm ...
- mysql length和char_length
length和char_length都是为了统计字符串的长度,length是按照字节来统计,char_lenght是按照字符来统计. 位(bit):计算机储存的最小单位. 字节(byte):计算机处理 ...
- java并发编程小结
旭日Follow_24 的CSDN 博客 ,全文地址请点击: https://blog.csdn.net/xuri24/article/details/82078467 线程简介: 线程是操作系统调度 ...
- RNP项目遇到的坑
1.nginx问题 和前端约定了在header中存放登录态k-v,选择的key是带下划线的. nginx 默认会丢弃带下划线的 header. 设置 underscores_in_headers on ...
- Eureka开启登录认证
Eureka服务端配置 一.Eureka的pom.xml 引入spring-boot-starter-security坐标 <dependency> <groupId>org. ...
- 我从Angular 2转向Vue.js, 也没有选择React
译者按: 通过使用Angular的经历,作者已经完全转为Vue粉了!我们Fundebug目前还是用AngularJS 1,坦白说,学习曲线蛮陡的. 原文: Why we moved from Angu ...
- 查询文章的上下篇Sql语句
直接开入正题 文章内容页一般都会有上一篇和下一篇的功能: 那么查询上下篇的sql语句应该怎么写呢:示例数据表:zmd_article自增主键:id当前文章id:10 肯定有人说,这简单啊id+1和id ...
- BZOJ2946 [Poi2000]公共串(后缀自动机)
Description 给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 任务: l 读入单词 l 计算最长公共子串的长度 l 输 ...