当前位置: 代码网 > 服务器>服务器>Linux > nginx安装并转发socket服务实现方式

nginx安装并转发socket服务实现方式

2025年07月20日 Linux 我要评论
一、安装编译工具yum -y install make zlib zlib-devel gcc-c++二、安装 pcrepcre download | sourceforge.nettar -zxvf

一、安装编译工具

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 ~]# 

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com