Nginx TCP代理
2024-08-24 10:09:50
nginx 在1.9.0 版本发布以前如果要想做到基于TCP的代理及负载均衡需要通过打名为nginx_tcp_proxy_module的第三方patch来实现,该模块的代码托管在github上 网址:https://github.com/yaoweibin/nginx_tcp_proxy_module/,而我们今年要来简单测试一下nginx 的ngx_stream_core_module,该模块有nginx 官方发布并支持tcp代理及负载均衡。
ngx_stream_core_module 这个模块在1.90版本后将被启用。但是并不会默认安装,需要在编译时通过指定 --with-stream 参数来激活这个模块。
其他改进包括:
- Change: 删除过时的 aio 和 rtsig 事件处理方法
- Feature: 可在 upstream 块中使用 "zone" 指令
- Feature: 流模块,支持 TCP 代理和负载均衡
- Feature: ngx_http_memcached_module 支持字节范围
- Feature: Windows 版本支持使用共享内存,带随机化地址空间布局.
- Feature: "error_log" 指令可在 mail 和 server 级别
- Bugfix: the "proxy_protocol" parameter of the "listen" directive did not work if not specified in the first "listen" directive for a listen socket.
简单配置步骤如下(测试MYSQL负载):
wget http://nginx.org/download/nginx-1.9.3.tar.gz
tar zxvf nginx-1.9..tar.gz
cd nginx-1.9.yum -y install proc*
yum -y install openssl*
yum -y install pcre* ./configure --prefix=/usr/local/nginx-1.9.3_tcp \
--with-http_ssl_module --with-http_spdy_module \
--with-http_stub_status_module --with-pcre \
--with-stream cd /usr/local/nginx-1.9.3_tcp/conf/
mv nginx.conf{,.bak}
vim nginx.conf worker_processes auto;
events {
worker_connections ;
}
error_log /var/log/nginx_error.log info;
stream {
upstream mysqld {
hash $remote_addr consistent;
server 192.168.1.42: weight= max_fails= fail_timeout=10s;
server 192.168.1.43: weight= max_fails= fail_timeout=10s;
} server {
listen ;
proxy_connect_timeout 1s;
proxy_timeout 3s;
proxy_pass mysqld;
} }
另外还可以实现一个ssh 转发代理
upstream ssh {
hash $remote_addr consistent;
server 192.168.1.42: weight=;
} server {
listen ;
proxy_pass ssh;
}
参考:
http://zhangge.net/5037.html
http://nginx.org/en/docs/stream/ngx_stream_core_module.html
最新文章
- WinDbg 蓝屏dump分析教程
- PhoneGap--001 入门 安装
- wlan-mcs来自百度百科
- Maven中的dependencyManagement 意义
- 一个 Sql语句优化的问题- STATISTICS 统计信息
- Unity NGUI 2D场景添加按钮
- Node 写文件
- 【转】Java多线程操作局部变量与全局变量
- 无废话WCF入门教程二[WCF应用的通信过程]
- U3D游戏开发基础
- 第八章 关于SQL查询出错的一些问题
- template.helper()方法
- Java:多态乃幸福本源
- python2,python3同时安装时,python3可以安装并升级pip库,python2报错的解决办法
- Linux 安装 ffmpeg
- Iptables-redhat/centos
- git reset命令使用
- 179. Largest Number(INT, String)
- 对SingleTask和TaskAffinity的理解(转至 http://www.2cto.com/kf/201311/254450.html)
- 一次使用Python连接数据库生成二维码并安装为windows服务的工作任务