Ubuntu下nginx(包管理安装) tcp代理转发

场景:希望用户对A端口发送请求,请求转发至目标端口B

 

首先nginx1.9之后支持TCP的转发,需要stream模块的支持。可以使用源码安装,源码安装的配置详细过程可以参考https://www.cnblogs.com/flyingeagle/articles/5789269.html

也可以使用最简单的包管理安装,使用包管理安装的nginx和源码安装的nginx文件目录有很大的差别,源码安装的有些命令包管理安装也不可用,这里要描述包管理安装nginx的过程以及tcp转发的配置:

包管理安装nginx:

1. sudo apt-get update

2.sudo apt-get install nginx 

nginx已经安装完毕,一般包管理安装nginx版本高于1.9都有stream模块!在终端中输入nginx -V(V大写),会出现nginx的版本和相关详细信息,如下:

nginx version: nginx/1.10.3 (Ubuntu)  
built with OpenSSL 1.0.2g  1 Mar 2016
TLS SNI support enabled
configure arguments: --with-cc-opt='-g -O2 -fPIE -fstack-protector-strong -Wformat -Werror=format-security -Wdate-time -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -fPIE -pie -Wl,-z,relro -Wl,-z,now' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_auth_request_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_v2_module --with-http_sub_module --with-http_xslt_module --with-stream --with-stream_ssl_module --with-mail --with-mail_ssl_module --with-threads

 

有--with-stream模块便可以使用tcp层转发,需要对相应配置文件进行配置:

包管理安装,文件一般在etc/目录下,需要修改的配置文件在etc/nginx/目录下的nginx.conf文件,需要添加stream{****}:

user ****;
worker_processes ****;
pid /run/nginx.pid;

events {
    worker_connections 768;
    # multi_accept on;
}

http {

**********

}
stream {
    # 添加socket转发的代理
    upstream kuye {
        hash $remote_addr consistent;
        # 转发的目的地址和端口
      server 127.0.0.1:81  weight=5 max_fails=3 fail_timeout=30s;
    }

    # 提供转发的服务,即访问localhost:7041,会跳转至代理kuye指定的转发地址
    server {
       listen 7041;
       proxy_connect_timeout 1s;
       proxy_timeout 3s;
       proxy_pass kuye;
    }
}

#mail {
*****************
#}#mail {
*****************
#}

nginx.conf文件下只需要添加以上stream{****}之间的配置就行,不需要修改其他。(注意:stream{***}不能放在http的大括号内)

 

nginx.cof修改需要超级管理权限,在etc/nginx目录下打开终端,使用sudo vim nginx.conf 可以打开修改配置文件。

修改配置文件后需要重启nginx, sudo nginx -s reload

然后,打开浏览器测试,我的nginx的端口修改为81,我访问的是localhost:7041,因为有以上的tcp层代理转发,对本机7041端口的请求会转发到指定的服务器和端口(我指定的端口是本机的81端口,即nginx的访问页面)!

在stream{****}中可以添加多个upstream和server进行代理,server中的proxy_pass名称要和需要upstream 相同才可以实现代理转发。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值