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;
       }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值