我有一台主机,为许多 Web 应用程序提供服务(不是基于 Node.js)。它使用不同的端口来执行此操作。这意味着以下应用程序已上线:
- app1: http://主机名:3000
- app2: http://主机名:3001
- app3: http://主机名:3003
接下来,我有一个基于 Node.js 的 Web 应用程序(在端口 80 上运行),我想将其用作某种路由器。当有人导航到http://localhost/app/app1。我希望它导航到http://主机名:3000。使用简单的重定向,这相对简单。但是,我想保留网址http://localhost/app/app1。有人可以向我指出一种使用 node.js/express 完成这项工作的方法吗?
我的路由逻辑看起来有点像这样(伪代码)。
app.route('/app/:appName')
.get(appEngine.gotoApp);
appEngine.gotoApp = function(req, res) {
redirectToApp logic
}
你可能更好地使用Nginx为每个应用程序设置不同位置的反向代理。
这不是你所要求的,因为它不使用node.js,但如果这是唯一的目的,Nginx 确实适合你的需求。
例如,Nginx 配置文件应该按照您想要的方式工作:
server {
listen 80;
server_name myapp.com;
location /app1 {
proxy_pass http://APP_PRIVATE_IP_ADDRESS:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /app2 {
proxy_pass http://APP_PRIVATE_IP_ADDRESS:3001;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
location /app3 {
proxy_pass http://APP_PRIVATE_IP_ADDRESS:3003;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)