Grails和子域 [英] Grails and Subdomains

查看:82
本文介绍了Grails和子域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Grails是否了解子域名(即subdomain.domain.com)?我没有在手册中看到它的讨论。这纯粹是一个应用程序服务器/ Web服务器的问题?可以绑定到grails控制器,无论是静态还是动态?

解决方案

无论哪个主机访问java web应用程序。


  1. 假设您在一台主机上分隔了多个客户端,例如customer1.yourhost.com,customer2.yourhost.com等,所有的客户端都具有相同的功能。

    在最简单的例子中,我只是用写过滤器,它总是会放一些请求变量,如下所示:

      def filters = {
    all(controller:' ('。')){
    def clientName =
    request.serverName。 substring(0,request.serverName.indexOf(。))

    request.currentClient = Client.findByClientName(clientName)//例如
    }
    }
    }
    }

    然后在任何地方你可以检查 request.currentClient 为当前访问的子域。



    然而,如果它变得更复杂看看Grails的一些多租户插件。

  2. 如果你想为每个子域拥有不同的功能,例如help.yourhost.com和www.yourhost.com,我会建议你编写独立的Grails应用程序。然后,您可以设置NGINX服务器,将这些请求重定向到应用服务器上运行的相应应用程序。

    Does Grails know anything about sub-domains (i.e. subdomain.domain.com) ? I don't see it discussed in the manual. Is this purely an app server/web server issue? Can be tied into grails controllers, either statically or dynamically?

    解决方案

    It does not matter which host is accessed for a java web application.

    1. Supposing you have multiple clients separated on one host, e.g. customer1.yourhost.com, customer2.yourhost.com, etc. and all clients will have same functionalities.

      In the simplest case I propse, that you just use write a filter, which will always put some request variable, like this:

      def filters = {
          all(controller:'*', action:'*') {
              before = {
                  if (request.serverName.contains(".")) {
                      def clientName = 
                        request.serverName.substring(0, request.serverName.indexOf("."))
      
                      request.currentClient = Client.findByClientName(clientName) // e.g.
                  }
              }
          }
      }
      

      Then at any place you can check request.currentClient for the current accessed subdomain.

      However if it gets more complicated have a look at some multi-tenant plugins for grails.

    2. If you want to have different functionalities for each subdomain e.g. help.yourhost.com and www.yourhost.com, I would propose that you write independent grails applications. You then setup a NGINX server to redirect those requests to the appropriate application running on your application server.

    这篇关于Grails和子域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆