Tomcat虚拟主机&通配符dns匹配 [英] Tomcat Virtual Host & Wildcard dns matching
问题描述
我创建了一个应用程序,它需要接受通配符dns并使用该通配符值向数据库请求。我试图为自己模拟一个虚拟环境来实现我想要的东西:
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)在我的Linux操作系统中我改变了/ etc / hosts并添加了这些行:
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)我更改了我的tomcat server.xml文件,如下所示:
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)我将项目war文件'Root.war'部署到$ {TOMCAT} / webapps / ROOT文件夹
3) I deploy my project war file 'Root.war' into the ${TOMCAT}/webapps/ROOT folder
现在当我请求 www.test-domain.com 时我看到网络主页页面,我希望当我输入这个网址 mehdi.test-domain.com 时,我会从网址中获取mehdi并对其执行操作并向我的用户显示mehdi页面。
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.
问题1:如何在我的应用程序中获取通配符值?
Question 1: How I can fetch the wildcard value in my application ?
问题2:我们是否有可能让tomcat在运行时将请求URL转换为其他内容并将其传递给应用程序(我需要通配符值ex:mehdi)?
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
http://mehdi.test-domain.com CONVERT TO http://test-domain.com/mehdi
问题3:是否可以只有第二个<主机名= ... 定义并删除第一个?我怎么能在我的应用程序中处理它?</ p>
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 ?
推荐答案
Q1:使用request.getServerName()
Q1: use request.getServerName()
Q2:您可以使用UrlRewrite过滤器或类似的
Q2: you can use UrlRewrite filter or similar
Q3:不支持AFAIK通配符,因此您需要单个主机名=localhost将收到所有请求
参见如何在Tomcat和Java上托管随机或通配符子域
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
这篇关于Tomcat虚拟主机&通配符dns匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!