Grails and Subdomains

有些话、适合烂在心里 提交于 2019-12-03 11:04:29

问题


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?


回答1:


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.




回答2:


We run a few Grails apps on a single host using various sub domains. In all cases we use Apache to front the Tomcat server and use mod jk or forward proxy to handle the applications to the different Grails app. Most of it is rather straight forward, what we have not figured out is running the applications at the root level for the various domains, for instance - http://app1.domain.com instead of http://app1.domain.com/app1




回答3:


The only place I'm aware of subdomains being considered is for tenant resolution when using the multi-tenant plugin. See http://tinyurl.com/6tuxwvs.



来源:https://stackoverflow.com/questions/9242527/grails-and-subdomains

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!