nginx安装并转发socket服务实现方式
更新时间:2025年07月19日 16:54:35 作者:mteng59101
文章指导如何安装编译工具、PCRE和Nginx,并通过修改配置文件添加stream块实现端口转发,将9999/801端口分别映射至9988/1557端口
一、安装编译工具
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
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 ~]#
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
nginx php-fpm环境中chroot功能的配置使用方法
这篇文章主要介绍了nginx php-fpm环境中chroot功能的配置使用方法,此方法是比禁用PHP敏感函数更好的一个安全防护手手段,需要的朋友可以参考下2014-05-05
Nginx防盗链根据UA屏蔽恶意User Agent请求(防蜘蛛)
相对于 Apache,Nginx 占用的系统资源更少,更适合 VPS 使用。恶意盗链的 User Agent 无处不在,博客更换到 WordPress 没几天,就被 SPAM(垃圾留言)盯上,又被暴力破解后台用户名密码。今天来介绍 Nginx 屏蔽恶意 User Agent请求的方法2016-07-07
详解Nginx 出现 403 Forbidden 的解决办法
本篇文章主要介绍了详解Nginx 出现 403 Forbidden 的解决办法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-08-08


最新评论