nginx从1.9.0开始,新增加了一个stream模块,用来实现四层协议的转发、代理或者负载均衡等。

(1)关于stream域的模块有哪些?

目前官网上列出的第三方模块、简直就是http模块的镜像、比如access模块访问控制ip和ip段,map模块实现映射、 geo模块实现地理位置映射、等等。使用这些模块的时候一定要看是哪个版本才支持的、比如log模块,只有在nginx-1.11.4才支持。

NGINX的stream相关模块有如下(有些模块特定版本才有,才支持,比如,log模块是NGINX的1.11.4版本):

ngx_stream_core_module

ngx_stream_access_module

ngx_stream_geo_module

ngx_stream_geoip_module

ngx_stream_js_module

ngx_stream_limit_conn_module

ngx_stream_log_module

ngx_stream_map_module

ngx_stream_proxy_module

ngx_stream_realip_module

ngx_stream_return_module

ngx_stream_split_clients_module

ngx_stream_ssl_module

ngx_stream_ssl_preread_module

ngx_stream_upstream_module

ngx_stream_upstream_hc_module

注意:如果使用 nginx 的 stream 功能,在编译时一定要加上 “--with-stream”

这里使用官方提供的方式在线yum安装openesty,默认已经加上stream 功能了

[root@test sbin]# ./nginx -V
nginx version: openresty/1.19.3.2
built by gcc 9.3.1 20200408 (Red Hat 9.3.1-2) (GCC)
built with OpenSSL 1.1.1k 25 Mar 2021 (running with OpenSSL 1.1.1i 8 Dec 2020)
TLS SNI support enabled
configure arguments: --prefix=/usr/local/openresty/nginx --with-cc-opt='-O2 -DNGX_LUA_ABORT_AT_PANIC -I/usr/local/openresty/zlib/include -I/usr/local/openresty/pcre/include -I/usr/local/openresty/openssl111/include' --add-module=../ngx_devel_kit-0.3.1 --add-module=../echo-nginx-module-0.62 --add-module=../xss-nginx-module-0.06 --add-module=../ngx_coolkit-0.2 --add-module=../set-misc-nginx-module-0.32 --add-module=../form-input-nginx-module-0.12 --add-module=../encrypted-session-nginx-module-0.08 --add-module=../srcache-nginx-module-0.32 --add-module=../ngx_lua-0.10.19 --add-module=../ngx_lua_upstream-0.07 --add-module=../headers-more-nginx-module-0.33 --add-module=../array-var-nginx-module-0.05 --add-module=../memc-nginx-module-0.19 --add-module=../redis2-nginx-module-0.15 --add-module=../redis-nginx-module-0.3.7 --add-module=../ngx_stream_lua-0.0.9 --with-ld-opt='-Wl,-rpath,/usr/local/openresty/luajit/lib -L/usr/local/openresty/zlib/lib -L/usr/local/openresty/pcre/lib -L/usr/local/openresty/openssl111/lib -Wl,-rpath,/usr/local/openresty/zlib/lib:/usr/local/openresty/pcre/lib:/usr/local/openresty/openssl111/lib' --with-cc='ccache gcc -fdiagnostics-color=always' --with-pcre-jit --with-stream --with-stream_ssl_module --with-stream_ssl_preread_module --with-http_v2_module --without-mail_pop3_module --without-mail_imap_module --without-mail_smtp_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_auth_request_module --with-http_secure_link_module --with-http_random_index_module --with-http_gzip_static_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-threads --with-compat --with-stream --with-http_ssl_module
wget https://openresty.org/package/centos/openresty.repo
mv openresty.repo /etc/yum.repos.d/
yum check-update
yum -y install openresty systemctl start openresty.service

stream模块通常写在events模块下面,与http同一级别.

如下的配置,是监听本机的5678端口转发给stream中upstream的rabbitmq

worker_processes  1;
error_log logs/error.log;
pid logs/nginx.pid; events {
worker_connections 1024;
} stream{
upstream rabbitmq{
server 192.168.20.100:5672 max_fails=2 fail_timeout=5s weight=2;
server 192.168.20.101:5672 max_fails=2 fail_timeout=5s weight=2;
server 192.168.20.102:5672 max_fails=2 fail_timeout=5s weight=2;
}
server{
listen 5678; # 任意不占用的端口
proxy_connect_timeout 10s;
proxy_timeout 300s;
proxy_pass rabbitmq; # 注意写法,不带http://
}
} http {
include mime.types;
default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log logs/access.log main; sendfile on;
tcp_nopush on; keepalive_timeout 65; gzip on;
gzip_min_length 1k;
gzip_comp_level 7;
gzip_types text/plain application/javascript application/x-javascript text/css application/xml text/javascript application/x-httpd-php image/jpeg image/gif image/png;
gzip_vary on;
gzip_disable "MSIE [1-6]\."; upstream mqweb {
ip_hash;
server 192.168.20.100:15672 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.20.101:15672 weight=1 max_fails=2 fail_timeout=30s;
server 192.168.20.102:15672 weight=1 max_fails=2 fail_timeout=30s;
} server {
listen 80;
server_name localhost;
charset utf-8;
location / {
proxy_pass http://mqweb;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 100m;
client_body_buffer_size 256k; proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 60; proxy_buffer_size 256k;
proxy_buffers 8 256k;
proxy_busy_buffers_size 512k;
proxy_temp_file_write_size 512k;
}
access_log logs/host.access.log main;
}
}

改变负载均衡的方法

a)least-connected :对于每个请求,nginx plus选择当前连接数最少的server来处理:

stream{
upstream rabbitmq{
least_conn;
server 192.168.20.100:5672 max_fails=2 fail_timeout=5s weight=2;
server 192.168.20.101:5672 max_fails=2 fail_timeout=5s weight=2;
server 192.168.20.102:5672 max_fails=2 fail_timeout=5s weight=2;
}
server{
listen 5672;
proxy_connect_timeout 10s;
proxy_timeout 300s;
proxy_pass rabbitmq;
}
}

b)ip_hash :客户机的IP地址用作散列键,用于确定应该为客户机的请求选择服务器组中的哪个服务器

stream{
upstream rabbitmq{
ip_hash;
server 192.168.20.100:5672 max_fails=2 fail_timeout=5s weight=2;
server 192.168.20.101:5672 max_fails=2 fail_timeout=5s weight=2;
server 192.168.20.102:5672 max_fails=2 fail_timeout=5s weight=2;
}
server{
listen 5672;
proxy_connect_timeout 10s;
proxy_timeout 300s;
proxy_pass rabbitmq;
}
}

注:这个least time均衡方法没有

c)普通的hash算法:nginx plus选择这个server是通过user_defined 关键字,就是IP地址:$remote_addr;

stream{
upstream rabbitmq{
hash $remote_addr consistent;
server 192.168.20.100:5672 max_fails=2 fail_timeout=5s weight=2;
server 192.168.20.101:5672 max_fails=2 fail_timeout=5s weight=2;
server 192.168.20.102:5672 max_fails=2 fail_timeout=5s weight=2 max_conns=3;;
}
server{
listen 5672;
proxy_connect_timeout 10s;
proxy_timeout 300s;
proxy_pass rabbitmq;
}
}

openresty(nginx) 配置 stream 转发的更多相关文章

  1. Nginx配置proxy_pass转发的/路径问题

    Nginx配置proxy_pass转发的/路径问题 在nginx中配置proxy_pass时,如果是按照^~匹配路径时,要注意proxy_pass后的url最后的/,当加上了/,相当于是绝对根路径,则 ...

  2. nginx 配置301转发

    学习nginx 推荐 http://www.nginx.cn/nginx-how-to 1. 设置域名解析 daijun.me 指向 234.33.22.21 2.主机234.33.22.21 ngi ...

  3. Nginx配置域名转发实例

    域名:cps.45wan.com   所在阿里云主机:123.35.9.12 45wan没有在阿里云备案 67wan已经在阿里云备案 阿里云主机(假如123.35.9.12)上原来的nginx配置: ...

  4. nginx配置https转发到tomcat(使用自签名的证书)

    一.使用openSSL生成自签名的证书 1.生成RSA私钥 命令:openssl genrsa -des3 -out server.key 1024 说明:生成rsa私钥,des3算法,1024强度, ...

  5. NGINX通过Stream转发ftp请求

    一.NGINX 1.9之前,需要安装第三方的TCP插件: http://www.cnblogs.com/i-blog/p/6165378.html 二.1.9之后直接使用Stream配置就可以了,当然 ...

  6. nginx配置https转发http

    生成ssl证书: 1.首先要生成服务器端的私钥,运行时会提示输入密码,此密码用于加密key文件: openssl genrsa -des3 -out server.key 1024 2.去除key文件 ...

  7. Nginx配置proxy_pass转发的/路径

    请求原地址 :http://servername/static_js/test.html location ^~ /static_js/ { proxy_cache js_cache; proxy_s ...

  8. Nginx配置请求转发location及rewrite规则

    一个示例: location = / { # 精确匹配 / ,主机名后面不能带任何字符串 [ configuration A ] } location / { # 因为所有的地址都以 / 开头,所以这 ...

  9. Nginx 配置 stream SSL 第四层 代理

    场景:服务器F针对访问终端需要添加白名单操作,由到终端数量较多,所以用了一台代理服务器 P,在服务F中添加 服务器P IP地址的白名单,所有终端访问服务器P 由于我已经安装过 Nginx 所以只需要添 ...

随机推荐

  1. 如何用Python实现配置热加载?

    背景 由于最近工作需求,需要在已有项目添加一个新功能,实现配置热加载的功能.所谓的配置热加载,也就是说当服务收到配置更新消息之后,我们不用重启服务就可以使用最新的配置去执行任务. 如何实现 下面我分别 ...

  2. python解决“failed to execute pyi_rth_pkgres”问题

    pip uninstall pyinstaller pip install https://github.com/pyinstaller/pyinstaller/archive/develop.zip

  3. error: cannot open .git/FETCH_HEAD: Permission denied

    可能原因:该操作的执行者对该目录没有写权限 解决:1.类Unix平台,使用chown将目录改为自己: 2.Windows平台,取消只读选项,给everyone用户所有权限:

  4. AI写代码! 神器copilot介绍+安装+使用

    !郑重提示!!!!!!!: 正在学编程.算法的同学请千万不要依赖此插件,否则你可能甚至无法手写出一个for循环 AI帮我写代码?我帮AI写代码?庄周梦蝶?蝶梦庄周?十分梦幻. copilot在VSco ...

  5. 【Unity基础知识】认识常用的生命周期函数(Awake、Start、Update...)

    一.了解帧的概念 游戏的本质就是一个死循环 每一次循环都会处理游戏逻辑 并 更新一次游戏画面 之所以能看到画面在动 是因为 切换画面速度达到一定速度时 人眼就会认为画面是动态且流畅的 一帧就是执行了一 ...

  6. PE格式: 分析IatHook并实现

    Ring 3层的 IAT HOOK 和 EAT HOOK 其原理是通过替换IAT表中函数的原始地址从而实现Hook的,与普通的 InlineHook 不太一样 IAT Hook 需要充分理解PE文件的 ...

  7. Splash (渲染JS服务)介绍安装

    一. splash介绍 1.Splash 是一个带有 HTTP API 的 javascript 渲染服务.它是一个带有 HTTP API 的轻量级浏览器,使用 Twisted 和 QT5 在 Pyt ...

  8. Linux Shell 自动交互功能

    需求背景:   近日,在安装某软件过程,发现在安装过程需要输入一些信息才能继续下一步操作,在机器数量较少情况下,我们可以单台登录上去完成安装操作,但当机器数量超过一定时,如果再手动登录操作,就会产生大 ...

  9. MySQL 的prepare使用中的bug解析过程

    GreatSQL社区原创内容未经授权不得随意使用,转载请联系小编并注明来源. 目录 一.问题发现 二.问题调查过程 三.问题解决方案 四.问题总结 一.问题发现 在一次开发中使用 MySQL PREP ...

  10. 万答#1,MySQL中如何查询某个表上的IS(意向共享)锁

    欢迎来到 GreatSQL社区分享的MySQL技术文章,如有疑问或想学习的内容,可以在下方评论区留言,看到后会进行解答 问题 问题原文是这样的: 假如在MySQL事务里,给某个表的一行加了 共享锁,理 ...