nginx安装并转发socket服务

本文介绍了在CentOS系统中如何安装Nginx,并通过编辑nginx.conf配置文件,利用stream模块实现Socket服务的转发。在完成安装及配置后,可以成功通过特定端口访问原本不同端口上的Socket和HTTP服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、安装编译工具

yum -y install make zlib zlib-devel gcc-c++

二、安装 pcre

PCRE download | SourceForge.net

tar -zxvf pcre-8.38.tar.gz
cd pcre-8.38
./configure
make
make install

安装完可以  pcre-config --version  查看pcre已经安装好了,版本为8.38

三、安装niginx

nginx: download

tar -zxvf nginx-1.21.5.tar.gz
cd nginx-1.21.5
./configure  --prefix=/usr/local/nginx --with-stream
make
make install

四、修改 nginx.conf

cd /usr/local/nginx/conf

vim nginx.conf

 如图,在events和http块之间插入 代码段stream,用来转发socket

stream{
    upstream abcdef{
        server 127.0.0.1:9988;
    }
    server{
        listen 9999;
        proxy_pass abcdef;
    }
}

http服务转发是http内部的server内的location

五、启动nginx

cd /usr/local/nginx/sbin
./nginx
ps -ef|grep nginx

这样就可以通过9999端口访问原来发布在9988端口的socket服务

通过801端口访问原来发布在1557端口的http服务

[root@localhost ~]# curl http://192.168.137.141:801/multiply?a=3\&b=4
{"res": "12"}[root@localhost ~]# curl http://192.168.137.141:801/add?a=3\&b=4
{"res": "7"}[root@localhost ~]# curl http://192.168.137.141:1557/multiply?a=3\&b=4
{"res": "12"}[root@localhost ~]# curl http://192.168.137.141:1557/add?a=3\&b=4
{"res": "7"}[root@localhost ~]# 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值