0
点赞
收藏
分享

微信扫一扫

nginx 如何转发 其它端口程序 nodejs go

nginx 如何转发 其它端口程序 nodejs go

比如你有一个项目运行在 ​​localhost:3000​​​,不管是出于解决跨域还是什么其它原因,现在你需要将它映射到 ​​localhost/diary-portal​​ 路径上,如何操作呢?

1. 打开服务器的 nginx 配置文件夹

linux 上,默认位置是在 ​​/etc/nginx/conf.d/​​​ 目录下,比如我 CentOS 上的 nginx/1.19.7 的配置文件是这样的,因为我添加了一个 https 支持,所以有一个 ​​https.conf​​​,默认应该是只有 ​​default.conf​​​ 一个文件,这个无所谓,不影响
nginx 如何转发 其它端口程序 nodejs go_pm2

2. 修改 nginx 配置文件,添加转发接口信息

打开 ​​default.conf​​ 文件

vi default.conf

在 http 内部, server 外部,添加以下内容

upstream diary_server {
server localhost:3000;
keepalive 2000;
}

然后在 server 内部添加:

location /diary-portal/ {
proxy_pass http://diary_server/; # 这里对应上面的 upsteam 名字
proxy_set_header Host $host:$server_port; # 这里照搬就可以
}

这样,就会将 ​​localhost:3000​​​ 这个接口映射到 ​​localhost/diary-portal/​​ 这个路径下

3. 重启 nginx 服务

systemctl restart nginx

结论

比如我的一个日记服务就是这样配置的
将 ​​​http://kylebing.cn:3000​​​ 映射到了 ​​https://kylebing.cn/diary-portal​​ 上

线上已运行的例子:
http ​​​http://kylebing.cn:3000/diary/detail?diaryId=5312​​​ https ​​https://kylebing.cn/diary-portal/diary/detail?diaryId=5312​​


举报

相关推荐

0 条评论