系统相关
首页 > 系统相关> > nginx使用4层模式(tcp)ip透传到应用

nginx使用4层模式(tcp)ip透传到应用

作者:互联网

1,nginx编译安装时要加上stream模块里的--with-stream --with-stream_realip_module,第一个用来四层负载,第二个用来获得客户端真实IP地址

[root@localhost ~]# nginx -V

nginx version: nginx/1.14.0

built by gcc 4.4.7 20120313 (Red Hat 4.4.7-18) (GCC) 

built with OpenSSL 1.0.2o  27 Mar 2018

TLS SNI support enabled

configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-stream --with-stream_realip_module --with-http_stub_status_module --with-http_v2_module --with-http_ssl_module --with-http_gzip_static_module --with-http_realip_module --with-http_flv_module --with-http_mp4_module --with-openssl=../openssl-1.0.2o --with-pcre=../pcre-8.42 --with-pcre-jit --with-ld-opt=-ljemalloc --add-module=./nginx_upstream_check_module-master

 

2,Nginx加入主配置文件4层的配置nginx.conf

stream {

    log_format proxy '$remote_addr [$time_local] '

                     '$protocol $status $bytes_sent $bytes_received '

                     '$session_time "$upstream_addr" '

                     '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"';

    include vhost/*.stream;

}

 

3,具体配置文件的写法

upstream test9001 {

        server 192.168.0.1:9001 weight=1 max_fails=3 fail_timeout=30s;   #负载

    }

server {

        listen 9001;   #监听端口

        set_real_ip_from 10.3.0.0/24;    #上一层代理IP地址

        proxy_protocol on;   #打开代理协议

        proxy_pass test9001;

        proxy_connect_timeout 600;

        access_log /data/wwwlogs/test.log proxy;

        error_log /data/wwwlogs/test.error.log error;

       }
-----------------------------------
©著作权归作者所有:来自51CTO博客作者wx5a9e3b462cab6的原创作品,请联系作者获取转载授权,否则将追究法律责任
nginx使用4层模式(tcp)ip透传到应用
https://blog.51cto.com/u_13628717/4567630

标签:http,stream,--,ip,module,nginx,tcp,upstream
来源: https://www.cnblogs.com/netflix/p/16333402.html