IIS7 中的通配符子域。是否有可能使它们像在 Apache 中一样?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/458757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Wildcard subdomains in IIS7. Is it possible to make them like it is in Apache?
提问by GrZeCh
Is this possible to configure IIS7 to achieve the same functionality like Apache has regarding wildcard domains? I'm interested in routing user in ASP.NET web application based on subdomain user used in URL.
这是否可以配置 IIS7 以实现与 Apache 具有的通配符域相同的功能?我对基于 URL 中使用的子域用户在 ASP.NET Web 应用程序中路由用户感兴趣。
Something like is described here:
这里描述了类似的东西:
http://steinsoft.net/index.php?site=programming/articles/apachewildcarddomain
http://steinsoft.net/index.php?site=programming/articles/apachewildcarddomain
Thanks
谢谢
回答by Daniel Liuzzi
The answer is No, IIS7 (still) does not support wildcard hostnames (see this). If you want to serve multiple domain on one website, the only workaround for now, as notandy suggested, is using a dedicated IP and doing it with DNS, which does support wildcards.
答案是否定的,IIS7(仍然)不支持通配符主机名(请参阅此)。如果您想在一个网站上提供多个域服务,那么目前唯一的解决方法是使用专用 IP 并使用 DNS 进行处理,DNS 支持通配符。
2013 Update
2013年更新
For completeness, IIS8 does not yet have this feature either.
为完整起见,IIS8 也没有此功能。
2016 Update
2016年更新
Finally, IIS 10 in Windows 2016 supports wildcard subdomains.
最后,Windows 2016 中的 IIS 10 支持通配符子域。
回答by notandy
Does IIS support wildcard host header? Can I capture and redirect *.mydomain.com to one web site?
IIS 是否支持通配符主机头?我可以捕获 *.mydomain.com 并将其重定向到一个网站吗?
The answer is Yes/No. Yes, because you are able to redirect *.mydomain.com to one web site. No, because the magic is in DNS and not IIS.
答案是是/否。是的,因为您可以将 *.mydomain.com 重定向到一个网站。不,因为魔法在 DNS 而不是 IIS。
Here's how you do it:
At IIS MMC,
configure a web site with NO host
header, then assign an IP address to
the site. (if you have one IP address
in the box, then you can skip this).
With this, the web site will bound to
the specific IP and will listen to all
HTTP requests send to the IP, and you
are done :)
操作方法如下:
在 IIS MMC 中,配置一个没有主机头的网站,然后为该站点分配一个 IP 地址。(如果您在框中有一个 IP 地址,则可以跳过此步骤)。有了这个,网站将绑定到特定的 IP 并将侦听发送到 IP 的所有 HTTP 请求,您就完成了:)
Next step is to make sure your name resolution works for the wildcard query and reply with the correct IP address. If you using Microsoft DNS service, it won't allow you to create a '*' A record (assuming you already created the domain zone in DNS MMC), you need to do the following:
下一步是确保您的名称解析适用于通配符查询并使用正确的 IP 地址进行回复。如果您使用 Microsoft DNS 服务,它不允许您创建 '*' A 记录(假设您已经在 DNS MMC 中创建了域区域),您需要执行以下操作:
- Navigate to
%windir%\system32\dns\ - Find the zone file. E.g.
mydomain.com.dns, open it with Notepad - Add an entry. E.g.
* A IP.IP.IP.IP - Save the zone data file
- Reload the zone data in DNS MMC.
- 导航
%windir%\system32\dns\ - 找到区域文件。比如
mydomain.com.dns,用记事本打开 - 添加条目。例如
* A IP.IP.IP.IP - 保存区域数据文件
- 在 DNS MMC 中重新加载区域数据。
Take note that by doing this, all * will response to the IP that you configured earlier. E.g. abc.mydomain.com, www.mydomain.com, K2k.mydomain.com and etc.
请注意,通过这样做,所有 * 都会响应您之前配置的 IP。例如 abc.mydomain.com、www.mydomain.com、K2k.mydomain.com 等。
To verify that it is working, try ping
utility ping (insert anything here).mydomain.comand you should
get replies from IP.IP.IP.IP
要验证它是否正常工作,请尝试 ping 实用程序ping (insert anything here).mydomain.com,您应该会收到来自 IP.IP.IP.IP 的回复
Then try browsing, http:// (insert anything here).mydomain.com/, you should get the same web page that you have configured.
然后尝试浏览,http://(在此处插入任何内容)。mydomain.com/,您应该获得与您配置的相同的网页。
回答by Centijo
You cannot create a wildcard (*) A record in Microsoft's DNS, but you cancreate a wildcard CNAME. If all you are trying to do is direct all subdomains to a particular IP, this would work. For example, if you have an A record for www.mydomain.com, you could add a CNAMErecord for *.mydomain.comand point that at www.mydomain.com. The hostname in the request header will still be the subdomain, so your web app should be able to catch it and handle it if you want.
您不能在 Microsoft 的 DNS 中创建通配符 (*) A 记录,但您可以创建通配符CNAME。如果您要做的只是将所有子域定向到特定 IP,这将起作用。例如,如果您有 的 A 记录www.mydomain.com,您可以添加一条CNAME记录*.mydomain.com并将其指向www.mydomain.com。请求标头中的主机名仍将是子域,因此您的 Web 应用程序应该能够捕获它并根据需要处理它。
回答by James Wilkins
You can use IIS if you use the Web Platform Installer (http://www.microsoft.com/web/downloads/platform.aspx) and install the "URL Rewrite" module. If you add an "A" record like "*.example.com", this allows all sub-domains to be sent to your IIS server. In the IIS server you simply add a URL rewrite rule to the default website to do whatever you wish, such as redirect to another URL. When a user enters, say, "xyz.example.com", you could redirect to "example.com", and point all unknown subdomains to the root domain. The steps are:
如果您使用 Web 平台安装程序 ( http://www.microsoft.com/web/downloads/platform.aspx) 并安装“URL 重写”模块,则可以使用 IIS 。如果您添加“*.example.com”之类的“A”记录,这将允许将所有子域发送到您的 IIS 服务器。在 IIS 服务器中,您只需将 URL 重写规则添加到默认网站即可执行您想要的任何操作,例如重定向到另一个 URL。当用户输入“xyz.example.com”时,您可以重定向到“example.com”,并将所有未知子域指向根域。步骤是:
Install Web Platform Installer: http://www.microsoft.com/web/downloads/platform.aspx
Run it, search for "URL Rewrite", and install it.
Go to the default website, and open "URL Rewrite".
Click "Add Rule(s)" on far right side.
Name the rule and set the following:
Requested URL: Matches the Pattern
Using: Regular Expressions
Pattern: (.*)
Logical Grouping: Match All
Add a new condition (click the "Add.." button), and set:
Condition Input: {CACHE_URL}
Check if input string: Matches the Pattern
Pattern (modify as needed): (http|https)://(.*?)\.rootdomain\.tld
Click [OK].
Now scroll down to action and set:
Action Type: Redirect
Redirect URL (modify as needed): {C:1}://rootdomain.tld{R:0}
安装 Web 平台安装程序:http: //www.microsoft.com/web/downloads/platform.aspx
运行它,搜索“URL Rewrite”,然后安装它。
转到默认网站,然后打开“URL 重写”。
单击最右侧的“添加规则”。
命名规则并设置以下内容:
请求的 URL:匹配模式
使用:正则表达式
图案: (。*)
逻辑分组:全部匹配
添加新条件(单击“添加..”按钮),并设置:
条件输入:{CACHE_URL}
检查输入字符串是否匹配模式
模式(根据需要修改):(http|https)://(.*?)\.rootdomain\.tld
单击 [确定]。
现在向下滚动到操作并设置:
操作类型:重定向
重定向 URL(根据需要修改):{C:1}://rootdomain.tld{R:0}
The "{C:#}" and "{R:#}" come from the test windows when you click the "Test pattern" buttons next to the regex pattern entries. You can match part of a server value and reuse it later.
当您单击正则表达式模式条目旁边的“测试模式”按钮时,“{C:#}”和“{R:#}”来自测试窗口。您可以匹配服务器值的一部分并在以后重复使用。
Also, you may want to checkout this tip on passing on the HTTP/HTTPS in the URL (the "{C:1}" in step 6 above): http://weblogs.asp.net/owscott/url-rewrite-protocol-http-https-in-the-action
此外,您可能想查看有关在 URL 中传递 HTTP/HTTPS 的提示(上面第 6 步中的“{C:1}”):http: //weblogs.asp.net/owscott/url-rewrite-协议-http-https-in-the-action
回答by Damon Earl
Update 2: Link is broken, upgrade to IIS 10, it supports wildcards now.
更新 2:链接已损坏,升级到 IIS 10,它现在支持通配符。
UPDATE: This works for a while, after a while I became bombarded by multiple requests coming through. I would not recommend this method.
更新:这有一段时间了,过了一段时间,我被多个请求轰炸了。我不会推荐这种方法。
Make sure that your DNS records are setup correctly (CNAME record of *->@) and it will work flawlessly. Tested with multiple domains.
确保您的 DNS 记录设置正确(*->@ 的 CNAME 记录),它会完美运行。用多个域进行了测试。
Link is broken now used to be http://pzxc.com/apache-style-wildcard-hostname-rewrite-rules-in-iis
链接已断开现在曾经是 http://pzxc.com/apache-style-wildcard-hostname-rewrite-rules-in-iis
回答by Lex Li
For users who cannot upgrade to IIS 10+, it is not very difficult to enable wildcard host name support via reverse proxy.
对于无法升级到 IIS 10+ 的用户,通过反向代理启用通配符主机名支持并不是很困难。
The HTTP example is as below, while other setup is just a few more steps.
HTTP 示例如下,而其他设置只是几个步骤。
- Create a catch-all site which binds to *:80 and no host name.
- Create first site which binds to *:8091 and no host name.
- Create second site which binds to *:8092 and no host name.
- Test out both sites and move on.
- Install ARR and URL Rewrite module.
Create rewrite rules on the catch-all site to dispatch requests to the two sites,
<system.webServer> <rewrite> <rules> <rule name="site1" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTP_HOST}" pattern="^(.*).site1.com$" /> </conditions> <action type="Rewrite" url="http://localhost:8091/{R:0}" /> </rule> <rule name="site2" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTP_HOST}" pattern="^(.*).site2.com$" /> </conditions> <action type="Rewrite" url="http://localhost:8092/{R:0}" /> </rule> </rules> </rewrite> </system.webServer>
- 创建一个绑定到 *:80 且没有主机名的包罗万象的站点。
- 创建第一个绑定到 *:8091 且没有主机名的站点。
- 创建绑定到 *:8092 且没有主机名的第二个站点。
- 测试两个站点并继续。
- 安装 ARR 和 URL Rewrite 模块。
在包罗万象的站点上创建重写规则以将请求分派到两个站点,
<system.webServer> <rewrite> <rules> <rule name="site1" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTP_HOST}" pattern="^(.*).site1.com$" /> </conditions> <action type="Rewrite" url="http://localhost:8091/{R:0}" /> </rule> <rule name="site2" stopProcessing="true"> <match url=".*" /> <conditions> <add input="{HTTP_HOST}" pattern="^(.*).site2.com$" /> </conditions> <action type="Rewrite" url="http://localhost:8092/{R:0}" /> </rule> </rules> </rewrite> </system.webServer>
Note that at server level, ARR proxy mode must be turned on,
注意在服务器层面,必须开启ARR代理模式,
<system.webServer>
<proxy enabled="true" />
</system.webServer>
IIS 10 wildcard host name is simpler and also has better performance, because ARR has its overhead (but generally speaking negligible).
IIS 10 通配符主机名更简单,也有更好的性能,因为 ARR 有它的开销(但一般来说可以忽略不计)。

