使用nginx代理socket端口

本文将介绍如何使用nginx代理socket端口。

环境准备:

该功能需使用nginx的 ngx_stream_core_module 模块,且该模块在安装时并不是默认构建的。

因此在执行 ./configure 时需要加上参数 --with-stream

安装流程:

1.下载nginx安装包

地址:http://nginx.org/download/

wget http://nginx.org/download/

2.解压

tar -zxvf nginx-1.4.0.tar.gz

3.编译安装(编译安装前确保安装了nginx依赖的库:pcre、zlib、openssl)

cd nginx-1.4.0
./configure --prefix=/usr/local/nginx --with-stream
make && make install

配置nginx:

打开/usr/local/nginx/conf目录下的nginx.conf

添加与events块同级的stream块:

stream{
    upstream socket_server{
	server 127.0.0.1:3802 weight=1;#发布socket1服务端口
	server 127.0.0.1:3803 weight=1;#发布socket2服务端口
    }
    #监听socket端口 
    server {
	listen 3801;
	proxy_pass socket_server;
    }
}
启动nginx,至此socket端口代理完毕。

猜你喜欢

转载自blog.csdn.net/x8826054/article/details/81003266