13

如何允许客户在域中使用其组织名称访问 SaaS?

例如,一个 Web 应用程序 example.com 可能有 2 个客户,OrgA 和 OrbB。

登录后,每个客户都会被重定向到他们的网站 orga.example.com / orgb.example.com。

一旦包含子域的请求到达节点服务器,我希望使用单个“/”路由处理请求。在路由处理程序内部,它只是检查主机标头并将子域视为组织的参数。就像是:

app.get "/*", app.restricted, (req, res) ->
  console.log "/* hit with #{req.url} from #{req.headers.host}"
  domains = req.headers.host.split "."
  if domains
    org = domains[0]
    console.log org
    # TODO. do something with the org name (e.g. load specific org preferences)
  res.render "app/index", { layout: "app/app" }

注意。域数组中的第一项是组织名称。我假设主机标头中没有出现任何端口,目前我不考虑如何处理非组织子域名(例如 www、blog 等)。

因此,我的问题更多是关于如何配置 node/express 来处理具有不同主机头的请求。这通常在 Apache 中使用通配符别名或在 IIS 中使用主机头来解决。

一个 Apache/Rails 示例是 @ http://37signals.com/svn/posts/1512-how-to-do-basecamp-style-subdomains-in-rails

如何在节点中实现相同的功能?

4

3 回答 3

7

如果不想使用 express.vhost,可以使用 http-proxy 来实现更有条理的路由/端口系统

var express = require('express')
var app = express()
var fs = require('fs')

/*
Because of the way nodejitsu deals with ports, you have to put 
your proxy first, otherwise the first created webserver with an 
accessible port will be picked as the default.

As long as the port numbers here correspond with the listening 
servers below, you should be good to go. 
*/

var proxyopts = {
  router: {
    // dev
    'one.localhost': '127.0.0.1:3000',
    'two.localhost': '127.0.0.1:5000',
    // production
    'one.domain.in': '127.0.0.1:3000',
    'two.domain.in': '127.0.0.1:4000',

  }
}

var proxy = require('http-proxy')
  .createServer(proxyopts) // out port
  // port 4000 becomes the only 'entry point' for the other apps on different ports 
  .listen(4000); // in port


var one = express()
  .get('/', function(req, res) {
   var hostport = req.headers.host
   res.end(hostport)
 })
 .listen(3000)


var two = express()
  .get('/', function(req, res) {
    res.end('I AM APP FIVE THOUSAND')
  })
  .listen(5000)
于 2012-12-15T23:28:09.870 回答
2

我认为任何到达节点服务器的 IP 地址和端口的请求都应该由节点服务器处理。这就是您在 apache 中创建虚拟主机的原因,以区分 apache 通过例如子域接收的请求。

如果您想了解它如何处理子域,请查看express-subdomains的源代码(代码只有 41 行)。

于 2012-01-16T17:10:13.083 回答
2

我也遇到过类似的情况,虽然我有主站点 example.com,用户可以在其中登录和管理他们的站点,然后 OrgA.example.com 是一个面向公众的站点,它将根据登录用户在示例中的操作进行更新。 com。

我最终创建了两个单独的应用程序并在连接中设置虚拟主机,将“example.com”指向一个应用程序,将“*”指向另一个应用程序。这是有关如何执行此操作的示例。

于 2012-01-16T17:19:01.490 回答