如何在同一台服务器上 运行 多个实例?
How to run several instances on the same server?
我想 运行 在同一台服务器上 Sails.js API 的两个实例。我只有一个域名,想要这样的域名:
- v1.0 运行宁 https://api.example.com/1.0/
- v1.1 运行宁 https://api.example.com/1.1/
Sails/Express/Node.js 怎么可能?是否有任何模块可以处理此类问题?或者我需要像 Apache 或 Nginx 这样的代理服务器吗?
你可以用 node-http-proxy
例如,您在端口 8080 中航行服务器 1,在端口 8081 中航行服务器 2。
现在,通过端口 80 上的 http-proxy,您可以将 /1.0/ 重定向到服务器 1,将 v1.1 重定向到服务器 2。
这是您可以使用的 http-proxy 示例(未测试):
var http = require('http'), httpProxy = require('http-proxy'), proxy = httpProxy.createProxyServer({}), url = require('url');
proxy.on("error", function(err){console.log(err);});//Prevent proxy crash by socket hang out error
var proxyServer = http.createServer(function (req, res)
{
var hostname = req.headers.host.split(":")[0];
switch (hostname+req.url)
{
case 'api.example.com/1.0':
proxy.web(req, res, {target : 'http://localhost:8080'});
break;
case 'api.example.com/1.1':
proxy.web(req, res, {target : 'http://localhost:8081'});
break;
default:
proxy.web(req, res, {target : 'http://localhost:8080'});
}
});
//
// Listen to the `upgrade` event and proxy the
// WebSocket requests as well.
//
proxyServer.on('upgrade', function (req, socket, head)
{
var hostname = req.headers.host.split(":")[0];
switch (hostname+req.url)
{
case 'api.example.com/1.0':
proxy.ws(req, socket, {target : 'ws://localhost:8080'});
break;
case 'api.example.com/1.1':
proxy.ws(req, socket, {target : 'ws://localhost:8081'});
break;
default:
proxy.ws(req, socket, {target : 'ws://localhost:8080'});
}
});
proxyServer.listen(80, function ()
{
console.log('proxy listening on port 80');
});
您可以对 Apache 或 Nginx 执行相同的操作,但您需要完整的节点解决方案:)
我想 运行 在同一台服务器上 Sails.js API 的两个实例。我只有一个域名,想要这样的域名:
- v1.0 运行宁 https://api.example.com/1.0/
- v1.1 运行宁 https://api.example.com/1.1/
Sails/Express/Node.js 怎么可能?是否有任何模块可以处理此类问题?或者我需要像 Apache 或 Nginx 这样的代理服务器吗?
你可以用 node-http-proxy
例如,您在端口 8080 中航行服务器 1,在端口 8081 中航行服务器 2。 现在,通过端口 80 上的 http-proxy,您可以将 /1.0/ 重定向到服务器 1,将 v1.1 重定向到服务器 2。
这是您可以使用的 http-proxy 示例(未测试):
var http = require('http'), httpProxy = require('http-proxy'), proxy = httpProxy.createProxyServer({}), url = require('url');
proxy.on("error", function(err){console.log(err);});//Prevent proxy crash by socket hang out error
var proxyServer = http.createServer(function (req, res)
{
var hostname = req.headers.host.split(":")[0];
switch (hostname+req.url)
{
case 'api.example.com/1.0':
proxy.web(req, res, {target : 'http://localhost:8080'});
break;
case 'api.example.com/1.1':
proxy.web(req, res, {target : 'http://localhost:8081'});
break;
default:
proxy.web(req, res, {target : 'http://localhost:8080'});
}
});
//
// Listen to the `upgrade` event and proxy the
// WebSocket requests as well.
//
proxyServer.on('upgrade', function (req, socket, head)
{
var hostname = req.headers.host.split(":")[0];
switch (hostname+req.url)
{
case 'api.example.com/1.0':
proxy.ws(req, socket, {target : 'ws://localhost:8080'});
break;
case 'api.example.com/1.1':
proxy.ws(req, socket, {target : 'ws://localhost:8081'});
break;
default:
proxy.ws(req, socket, {target : 'ws://localhost:8080'});
}
});
proxyServer.listen(80, function ()
{
console.log('proxy listening on port 80');
});
您可以对 Apache 或 Nginx 执行相同的操作,但您需要完整的节点解决方案:)