Nginx教程:配置TCP/IP转发

  1. 安装nginx服务

  2. 检查是否编译时带with-stream参数

nginx -V |grep with-stream
#有with-stream参数,可以代理tcp协议
  1. 配置nginx的tcp代理

#请注意,stream块和http块是两个不同的模块,
#stream不属于http模块,即不能放到/etc/nginx/conf.d/,
#stream是通过tcp层转发,而不是http转发。
#如配置在http内,启动nginx会报如下错误:
nginx: [emerg] "server" directive is not allowed here
  1. 修改主配置文件,添加stream目录
cd /etc/nginx/
cp -a nginx.conf{,_$(date +%F)}
vim nginx.conf

# 最后追加如下内容
# tcp/ip proxy
include /etc/nginx/tcp.d/*.conf;
  1. 添加tcp转发配置
mkdir tcp.d
cd tcp.d
  1. 在新建的 tcp.d 目录下创建 conf 文件新建一个 tcp 配置,例如我转发到IP为127.0.0.1的7666端口
# vim openldap.conf
# 写入

stream{
    upstream tcpssh{
        hash $remote_addr consistent;
        server  127.0.0.1:7666 max_fails=3 fail_timeout=10s;  
        server  127.0.0.1:7888 max_fails=3 fail_timeout=10s;  
   }
    server{
        listen 7777;
        proxy_connect_timeout 20s;
        proxy_timeout 5m;
        proxy_pass tcpssh;
    }
}

#"upstream tcpssh":转发的目的地址和端口等设置;其中tcpssh为自定义;
#"server":提供转发的服务,即访问localhost:3389,会跳转至代理"tcpssh"指定的转发地址.。
  1. 启动nginx服务
systemctl start nginx.service

#查看是否启动

[root@node1 ~]# systemctl status nginx.service
 nginx.service - nginx - high performance web server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2018-09-29 11:34:01 CST; 5h 37min ago
     Docs: http://nginx.org/en/docs/
 Main PID: 26114 (nginx)
   CGroup: /system.slice/nginx.service
           ├─26114 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
           └─26115 nginx: worker process

Sep 29 11:34:01 node1 systemd[1]: Starting nginx - high performance web server...
Sep 29 11:34:01 node1 systemd[1]: Started nginx - high performance web server.
[root@node1 ~]#
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值