06
2020
03
14:46:56

Nginx配置TCPIP转发



推荐点击下面图片,通过本站淘宝优惠价购买:

image.png

1.安装nginx服务

1.1 安装nginx
暂无
1.2 对于已经安装nginx的,检查是否编译时带with-stream参数
nginx -V |grep with-stream
#有with-stream参数,可以代理tcp协议

2.配置nginx的tcp代理

请注意,stream块和http块是两个不同的模块,stream不属于http模块,即不能放到/etc/nginx/conf.d/,stream是通过tcp层转发,而不是http转发。
如配置在http内,启动nginx会报如下错误:

nginx: [emerg] "server" directive is not allowed here
2.1 修改主配置文件,添加stream目录
cd /etc/nginx/cp -a nginx.conf{,_$(date +%F)}vim nginx.conf# 最后追加如下内容# tcp/ip proxyinclude /etc/nginx/tcp.d/*.conf;

2.2 添加tcp转发配置

mkdir tcp.d
cd tcp.d

在新建的 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"指定的转发地址.。

2.3启动nginx服务

启动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 processSep 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 ~]#


作者:遗步看风景
链接:https://www.jianshu.com/p/024650f58383
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


本文链接:http://www.hqyman.cn/post/1181.html 非本站原创文章欢迎转载,原创文章需保留本站地址!

分享到:





休息一下,本站随机推荐观看栏目:


« 上一篇 下一篇 »

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

您的IP地址是: