Nginx支持4层的反向代理

阅读: 评论:0

Nginx支持4层的反向代理

Nginx支持4层的反向代理

前言

大家通常使用,nginx作为http层的7层代理软件。无意间发现nginx居然也能支持4层代理。

支持说明

官方文档:.html
四层代理依赖模块ngx_stream_core_module,该模块自1.9.0版开始可用。
可以使用nginx -V查看,有–with-stream模块表示支持

[root@localhost nginx]# nginx -V
nginx version: nginx/1.20.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) 
built with OpenSSL 1.1.1g FIPS  21 Apr 2020 (running with OpenSSL 1.1.1k  FIPS 25 Mar 2021)
TLS SNI support enabled
configure arguments: --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/f --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --http-client-body-temp-path=/var/lib/nginx/tmp/client_body --http-proxy-temp-path=/var/lib/nginx/tmp/proxy --http-fastcgi-temp-path=/var/lib/nginx/tmp/fastcgi --http-uwsgi-temp-path=/var/lib/nginx/tmp/uwsgi --http-scgi-temp-path=/var/lib/nginx/tmp/scgi --pid-path=/run/nginx.pid --lock-path=/run/lock/subsys/nginx --user=nginx --group=nginx --with-compat --with-debug --with-file-aio --with-google_perftools_module --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_degradation_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module=dynamic --with-http_mp4_module --with-http_perl_module=dynamic --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-http_xslt_module=dynamic --with-mail=dynamic --with-mail_ssl_module --with-pcre --with-pcre-jit --with-stream=dynamic --with-stream_ssl_module --with-stream_ssl_preread_module --with-threads --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -m64 -mtune=generic' --with-ld-opt='-Wl,-z,relro -specs=/usr/lib/rpm/redhat/redhat-hardened-ld -Wl,-E'

1.20.1版本默认有--with-stream=dynamic模块,直接支持4层代理。但是是动态库,需要再执行安装。

yum安装stream模块

nginx 用yum源安装的,stream模块也用yum源安装
安装stream模块动态模块
shell> yum install -y nginx-mod-stream

安装所有的动态模块
shell>yum install -y nginx-all-modules

查看安装后的动态模块
[root@localhost nginx]# ls /usr/lib64/nginx/modules
ngx_stream_module.so

手动编译安装stream模块

参考:yum安装下的nginx,如何添加模块,和添加第三方模块

Nginx4层代理配置相关

在f文件中使用stream模块来配置4层代理,该模块与http模块平级

http {include       /etc/pes;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  /var/log/nginx/access.log  main;sendfile        on;#tcp_nopush     on;keepalive_timeout  65;#gzip  on;include /etc/nginx/conf.d/*.conf;
}stream {                                         #stream模块 和http模块是一同等级;做四层代理时需要添加上这个模块;server {listen 40028;                          #40028端口将以4层TCP协议方式转发至后端app_sever;proxy_pass app_server;}upstream app_server{server 192.168.1.100:3306;}server {listen 40030;                          #40030端口将以4层TCP协议方式转发至后端app_sever;proxy_pass mongo;}upstream mongo{server 192.168.1.101:27017;}
}

本文发布于:2024-02-04 19:42:48,感谢您对本站的认可!

本文链接:https://www.4u4v.net/it/170715031958941.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:Nginx
留言与评论(共有 0 条评论)
   
验证码:

Copyright ©2019-2022 Comsenz Inc.Powered by ©

网站地图1 网站地图2 网站地图3 网站地图4 网站地图5 网站地图6 网站地图7 网站地图8 网站地图9 网站地图10 网站地图11 网站地图12 网站地图13 网站地图14 网站地图15 网站地图16 网站地图17 网站地图18 网站地图19 网站地图20 网站地图21 网站地图22/a> 网站地图23