Nginx教程:配置TCPIP转发

2019-10-15  本文已影响0人  遗步看风景

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 proxy
include /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 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 ~]#
上一篇下一篇

猜你喜欢

热点阅读