其實Tornado對子域名和泛域名(除了特別說明外,以下子域名和泛域名均簡稱為泛域名)的支持并不是什么新鮮事,兩年多前我用Tornado寫的開源網(wǎng)站 http://poweredsites.org 就有了對泛域名的支持,但是Tornado的官方文檔里并沒有明確對此功能進行說明,雖然源代碼里是有注釋的,終是有點隱晦,這不,近日mywaiting同學(xué)就遇到了這個問題,我應(yīng)邀特撰此博文,分享下我對此的一點點經(jīng)驗。
通常,用Tornado添加url映射路由表是直接傳handlers給Application這種方式的,比如官方的chatdemo:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
class Application(tornado.web.Application): def __init__( self ): handlers = [ (r "/" , MainHandler), (r "/auth/login" , AuthLoginHandler), (r "/auth/logout" , AuthLogoutHandler), (r "/a/message/new" , MessageNewHandler), (r "/a/message/updates" , MessageUpdatesHandler), ] settings = dict ( cookie_secret = "43oETzKXQAGaYdkL5gEmGeJJFuYh7EQnp2XdTP1o/Vo=" , login_url = "/auth/login" , template_path = os.path.join(os.path.dirname(__file__), "templates" ), static_path = os.path.join(os.path.dirname(__file__), "static" ), xsrf_cookies = True , autoescape = "xhtml_escape" , ) tornado.web.Application.__init__( self , handlers, * * settings) |
這種方式其實添加的是一個域名通配的url映射表,即域名&子域名不限,只要訪問能夠解析到這個chatdemo上,“/auth/login” “/auth/login”這些url就都能夠正常運行。假設(shè)www.feilong.me、abc.feilong.me、feilong2.me這個三個(子)域名均配置為可由這個chatdemo程序來host,那么訪問這三個(子)域名均可以正常使用這個chatdemo,總之域名是無關(guān)的。
實際上,這種方式它的內(nèi)部是通過Application里的這個add_handlers來實現(xiàn)的(原碼注釋如下):
1
2
3
4
5
6
7
8
|
def add_handlers( self , host_pattern, host_handlers): """Appends the given handlers to our handler list. Note that host patterns are processed sequentially in the order they were added, and only the first matching pattern is used. This means that all handlers for a given host must be added in a single add_handlers call. """ |
只不過它是隱式的調(diào)用這個add_handlers而已,其關(guān)鍵點就在于第一個參數(shù)host_pattern(匹配域名的)上,上面那種方式,默認添加的host_pattern是”.*$”,即域名通配,若要支持泛域名,只需要顯式的調(diào)用add_handlers來添加相應(yīng)的host_pattern和handlers即可。
接下來就以poweredsites的源碼來介紹Tornado對泛域名的支持,app.py里的Application里面有這么幾句:
1
2
3
4
5
6
|
super (Application, self ).__init__(handlers, * * settings) # add handlers for sub domains for sub_handler in sub_handlers: # host pattern and handlers self .add_handlers(sub_handler[ 0 ], sub_handler[ 1 ]) |
常見的方式super(Application, self).__init__(handlers, **settings)添加的是根域名poweredsites的handlers,接著用for循環(huán)顯式添加的是子域名和泛域名的handlers。這里的sub_handlers依次放有各子域名的handlers,其最后一個是泛域名的handlers:
1
2
3
4
5
|
sub_handlers.append(site.sub_handlers) sub_handlers.append(blog.sub_handlers) sub_handlers.append(admin.sub_handlers) # wildcard subdomain handler for project should be the last one. sub_handlers.append(project.sub_handlers) |
指定的子域名的sub_handlers(site.sub_handlers)是這個樣子的,這里的第一個元素就是host_pattern:
1
2
3
4
5
6
7
8
|
sub_handlers = [ "^sites.poweredsites.org$" , [ (r "/" , _WebsiteIndexHandler), (r "/feeds" , _WebsitesFeedsHandler), (r "/([a-z0-9]{32})" , _WebsiteHandler), (r "/([^/]+)" , WebsiteHandler), ] ] |
泛域名(project.sub_handlers)的區(qū)別也就在于這第一個元素,即用來做host_pattern的是通配一些子域名的:
1
2
3
4
5
6
|
sub_handlers = [ "^[a-zA-Z_\-0-9]*\.poweredsites.org$" , [(r "/" , ProjectIndexHandler), (r "/top" , ProjectTopHandler), (r "/opensource" , ProjectOpensourceHandler), ] ] |
在用到了泛域名的ProjectIndexHandler里,運行時具體的子域名就可以通過下面這樣的方式獲得:
1
2
3
|
class ProjectIndexHandler(ProjectBaseHandler): def get( self ): subdomain = self .request.host.split( "." )[ 0 ] |
需要說明的是,Tornado里面的url映射表和Django一樣是有順序的,即url依次序由上到下匹配,只要匹配到就立即結(jié)束,不再往下匹配,而帶子域名和泛域名的url路由其匹配優(yōu)先級是要高于通配域名”.*$”的(這個不用你操心,add_handlers會自動為你做到這一點)。同樣的,對于泛域名,因為其子域名是通配的,因此指定子域名的handlers需要放到泛域名前添加,如admin、blog這類子域名的handlers要放在泛域名之前,這就是poweredsites里sub_handlers.append(project.sub_handlers)放到最后一條的原因,project這條是對應(yīng)泛域名的,http://tornado.poweredsites.org 就是靠這一條來實現(xiàn)的。
備注:需要支持泛域名,首先要你的域名解析支持泛域名。
轉(zhuǎn)載請注明出處:http://feilong.me/2012/08/wildcard-subdomain-support-in-tornado