首页 » WEB技术 » Nginx » NGINX 支持TCP转发配置

NGINX 支持TCP转发配置

 

Nginx支持TCP转发

nginx1.9开始支持tcp层的转发,通过stream实现的。nginx 的安装详见另一个文档介绍《yum安装nginx》

默认yum安装的Nginx不支持TCP转发,需要安装--with-stream 模块,可以通过源码编译安装这个模块后替换原来的nginx执行程序。 

with-stream 模块安装

1、下载一个同版本可编译的Nginx wget http://nginx.org/download/nginx-1.16.0.tar.gz 2、编译安装nginx

#tar xvf nginx-1.16.0.tar.gz
#cd nginx-1.16.0
#./configure --prefix=/usr/local/nginx --sbin-path=/usr/local/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/usr/local/nginx16/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --with-stream
#make
#make install

2、验证模块安装情况 /usr/local/nginx/nginx -V

3、替换Nginx可执行文件

[root@yunapp nginx]# which nginx
/usr/sbin/nginx
[root@yunapp nginx]#mv /usr/sbin/nginx /usr/sbin/nginxbak
[root@yunapp nginx]#cp /usr/local/nginx/nginx /usr/sbin/

TCP转发配置

1、创建文件夹和配置 cd /etc/nginx mkdir tcp.d 修改配置文件包含文件 vi /etc/nginx/nginx.conf,增加如下行

include /etc/nginx/tcp.d/*.conf;

2、创建tcp转发配置文件

vi /etc/nginx/tcp.d/testdb_proxy.conf
stream {
    upstream testdb_proxy {
        hash $remote_addr consistent;
        # 转发的目的地址和端口
        server 10.168.240.239:1521 weight=5 max_fails=3 fail_timeout=30s;
    }

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

3、检查nginx配置并启动

[root@yunapp nginx]# nginx -t
nginx: the configuration file /usr/local/nginx16/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx16/nginx.conf test is successful

启动nginx

[root@yunapp nginx]#nginx 
[root@yunapp nginx]# ps -ef |grep nginx
root     25414     1  0 11:00 ?        00:00:00 nginx: master process nginx
nginx    25415 25414  0 11:00 ?        00:00:00 nginx: worker process
root     25535 13378  0 11:01 pts/0    00:00:00 grep nginx

[root@yunapp nginx]# netstat -an|grep 8921
tcp        0      0 0.0.0.0:8921                0.0.0.0:*                   LISTEN   

原文链接:NGINX 支持TCP转发配置,转载请注明来源!

5