Tomcat Virtual Host & Wildcard dns matching

匿名 (未验证) 提交于 2019-12-03 10:24:21

问题:

I have created an application which needs to accept wildcard dns and request to database using that wildcard value. I tried to simulate a virtual environment for myself to achieve what I want like this:

1) in my Linux OS I changed the /etc/hosts and add these lines :

127.0.0.1   test-domain.com 127.0.0.1   mehdi.test-domain.com 

2) I changed my tomcat server.xml file as follows :

<Host name="test-domain.com"      appBase="webapps/ROOT"      unpackWARs="true"      autoDeploy="true"     xmlValidation="false"      xmlNamespaceAware="false">     <Alias>www.test-domain.com</Alias>     <Context path="" docBase="."/> </Host>  <Host name="*.test-domain.com"          appBase="webapps/ROOT"          unpackWARs="true"          autoDeploy="true"         xmlValidation="false"          xmlNamespaceAware="false">         <Alias>*.test-domain.com</Alias>         <Context path="" docBase="."/> </Host>  

3) I deploy my project war file 'Root.war' into the ${TOMCAT}/webapps/ROOT folder

now when I request "www.test-domain.com" I see the web home page, and I want when I type this url "mehdi.test-domain.com", I fetch mehdi from the URL and do something with it and show mehdi page to my users.

Question 1: How I can fetch the wildcard value in my application ?

Question 2: Is it possible that we let tomcat convert the request URL to something else at runtime like this and pass it to the application (I need the wildcard value ex: mehdi)?

http://mehdi.test-domain.com CONVERT TO http://test-domain.com/mehdi

Question 3: Is it possible to have only the second < Host name=... definition and remove the first one? how could I handle it inside my application ?

回答1:

Q1: use request.getServerName()

Q2: you can use UrlRewrite filter or similar

Q3: AFAIK wildcards are not supported so you will need single Host name="localhost" that will receive all requests See also How to host random or wildcard subdomains on Tomcat and Java



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