将自定义域与 IIS Express 结合使用
传统上,我将自定义域与本地主机开发服务器一起使用。大概是这样的:
dev.example.com
dev.api.example.com
这为我在使用 Facebook 等外部 API 时提供了很大的灵活性。这在过去与内置的 Visual Studio 开发服务器配合使用非常有效,因为我所需要做的就是向那些指向 127.0.0.1
的 DNS 记录添加 CNAME。
但是,我无法让它与 IIS Express 一起使用。我所尝试的一切似乎都失败了。我什至已将正确的 XML 配置添加到 IIS Express 的 applicationHost.config
文件中,但它似乎无法像真正安装 IIS 那样识别这些条目。
<binding protocol="http" bindingInformation="*:1288:dev.example.com" />
每当我输入此行并尝试请求 http://dev.example.com:1288
时,我都会收到以下消息:
错误请求 - 无效主机名
有人知道我是否遗漏了一些明显的东西吗?或者 IIS Express 团队真的缺乏预见这种用途的远见吗?
Traditionally I use custom domains with my localhost development server. Something along the lines of:
dev.example.com
dev.api.example.com
This has provided me a ton of flexibility when working with external APIs such as Facebook. This has worked great in the past with the built-in Visual Studio Development Server because all I needed to do was add a CNAME to those DNS records pointing to 127.0.0.1
.
However, I have not been able to get this to work with IIS Express. Everything I have tried seems to have failed. I have even added the correct XML config to the applicationHost.config
file for IIS Express, but it doesn't seem to recognize the entries as valid as a true install of IIS would.
<binding protocol="http" bindingInformation="*:1288:dev.example.com" />
Whenever I enter this line and try to request http://dev.example.com:1288
I get the following message:
Bad Request - Invalid Hostname
Does anybody know if I am missing something obvious? Or did the IIS Express team really lack the foresight to see this type of use?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
这对我有用(针对 VS 2013 进行了更新,请参阅 2010 年的修订历史记录,对于 VS 2015 请参阅:https://stackoverflow.com/a/32744234/218971):
右键单击您的 Web 应用程序项目 ▶
Properties
▶Web
,然后配置Servers
部分如下:http://localhost
http://dev.example.com
默认站点
更改为除端口之外的任何内容):80
,请确保 Skype 未使用端口 80 等)可选:将
起始 URL
设置为http://dev.example.com
打开
%USERPROFILE%\My Documents\IISExpress\config\applicationhost。 config
(Windows XP、Vista 和 7)并在
配置块中编辑站点定义,使其符合以下内容:如果运行 MVC:请确保将
applicationPool
设置为“Integrated”选项之一(例如“Clr2IntegratedAppPool”)。打开您的
hosts
文件并添加以下行127.0.0.1 dev.example.com
。► 开始您的应用程序!
评论中的一些很好的建议:
This is what worked for me (Updated for VS 2013, see revision history for 2010, for VS 2015 see this: https://stackoverflow.com/a/32744234/218971):
Right-click your Web Application Project ▶
Properties
▶Web
, then configure theServers
section as follows:http://localhost
http://dev.example.com
Default Site
to anything but port:80
, make sure Skype isn't using port 80, etc.)Optionally: Set the
Start URL
tohttp://dev.example.com
Open
%USERPROFILE%\My Documents\IISExpress\config\applicationhost.config
(Windows XP, Vista, and 7) and edit the site definition in the<sites>
config block to be along the lines of the following:If running MVC: make sure the
applicationPool
is set to one of the "Integrated" options (like "Clr2IntegratedAppPool").Open your
hosts
file and add the line127.0.0.1 dev.example.com
.► Start your application!
Some great advice from the comments:
对于 Visual Studio 2015,上述答案中的步骤适用,但
applicationhost.config
文件位于新位置。在“解决方案”文件夹中遵循路径,如果您升级并且计算机上有两个版本的applicationhost.config
,这会令人困惑。在该文件夹中,您将看到 applicationhost.config 文件。
或者,您也可以在解决方案文件夹中搜索 .config 文件并以这种方式找到它。
我个人使用了以下配置:
在我的主机文件中包含以下内容:
在我的 applicationhost.config 文件中包含以下内容:
记住以管理员身份运行 Visual Studio 2015 实例!< /strong> 如果您不想每次我推荐时都这样做:
默认情况下如何以管理员身份运行 Visual Studio?
我在尝试升级到 Visual Studio 2015 时遇到了问题,并意识到我的所有配置都没有被保留。
For Visual Studio 2015 the steps in the above answers apply but the
applicationhost.config
file is in a new location. In your "solution" folder follow the path, this is confusing if you upgraded and would have TWO versions ofapplicationhost.config
on your machine.Within that folder you will see your applicationhost.config file
Alternatively you could just search your solution folder for the .config file and find it that way.
I personally used the following configuration:
With the following in my hosts file:
And the following in my applicationhost.config file:
Remember to run your instance of visual studio 2015 as an administrator! If you don't want to do this every time I recomend this:
How do I run Visual Studio as an administrator by default?
I had issues when trying to upgrade to visual studio 2015 and realized that none of my configurations were being carried over.
将 Visual Studio 2012 及更高版本与 IIS Express 结合使用时,更改现有绑定不会永久有效。 (在您关闭 VS 之前它可能会起作用,但在那之后,事情就会变得非常混乱。)
关键是保留现有的 localhost 绑定并在其后添加一个新的绑定。
除非您以管理员身份运行,否则您还需要运行
netsh http add urlacl
(授予自己以标准用户身份运行非本地主机站点的权限)。如果您想允许任何主机名,完整过程如下:
创建您的 Web 应用程序,并找出它正在使用的端口(请参阅项目属性、Web 选项卡、项目 Url)。
在管理员提示符下,运行以下命令(将端口号替换为您在 #1 中计算出的端口号):
您还可以使用您的用户名 (DOMAIN\USER) 而不是所有人,以提高安全性。
打开 applicationhost.config(通常位于“我的文档\IIS Express\config”下),然后找到包含您的端口号的元素。
再添加一个与所需主机名的绑定(在本例中为 *)。例如:
请注意,如果要打开所有主机名 (*),则需要两个 netsh 命令(一个用于 *,一个用于 localhost)。如果您只想打开特定主机名,则并不严格需要第二个 netsh 命令(localhost);只需包含您的特定主机名就足够了。
When using Visual Studio 2012 and above with IIS Express, changing an existing binding does not work permanently. (It may work until you close VS, but after that, things get really messed up.)
The key is keeping the existing localhost binding and adding a new binding after it.
Unless you're running as administrator, you'll also need to run
netsh http add urlacl
(to give yourself permissions to run a non-localhost site as a standard user).If you want to allow any host name, the full process is as follows:
Create your web application, and find out what port it is using (see project properties, Web tab, Project Url).
From an administrator prompt, run the following commands (replacing portnumber with the port number you figured out in #1):
You can also use your user name (DOMAIN\USER) instead of everyone for better security.
Open applicationhost.config (usually under My Documents\IIS Express\config), and find the element with your port number.
Add one more binding with the host name you want (in this case, *). For example:
Note that, if want to open up all host names (*), you'll need two netsh commands (one for * and one for localhost). If you only want to open up a specific host name, you don't strictly need the second netsh command (localhost); just the one with your specific host name is sufficient.
无效的主机名表明您在 IIS Express 配置文件中配置的实际站点(很可能)未运行。 IIS Express 没有像 IIS 那样的进程模型。
为了让您的站点运行,需要显式启动(通过从 webmatrix 打开和访问,或者从命令行使用 /site 参数调用 iisexpress.exe(从其安装目录)。
一般来说,允许完全限定的步骤用于本地访问的 DNS 名称是
让我们使用 DNS 名称 dev.example.com 的示例
为您的站点(例如:Site1)配置 IIS Express 绑定以包含 dev.example.com。或者,可以使用
通过 http.sys 进行一次性 URL 预留。
netsh http add urlacl url=http://dev.example.com:/ user=
启动
iisexpress /site:Site1
或在 WebMatrix 中打开 Site1The invalid hostname indicates that the actual site you configured in the IIS Express configuration file is (most likely) not running. IIS Express doesn't have a process model like IIS does.
For your site to run it would need to be started explicitly (either by opening and accessing from webmatrix, or from command line calling iisexpress.exe (from it's installation directory) with the /site parameter.
In general, the steps to allow fully qualified DNS names to be used for local access are
Let's use your example of the DNS name dev.example.com
Configure IIS Express binding for your site (eg:Site1) to include dev.example.com. Administrative privilege will be needed to use the binding. Alternatively, a one-time URL reservation can be made with http.sys using
netsh http add urlacl url=http://dev.example.com:<port>/ user=<user_name>
start
iisexpress /site:Site1
or open Site1 in WebMatrix在我的 WebMatrix IIS Express 安装上,从
"*:80:localhost"
更改为"*:80:custom.hostname"
不起作用(“Bad Hostname”,甚至具有正确的 etc\hosts 映射),但是"*:80:"
确实工作 - 并且没有此处其他答案所需的任何额外步骤。请注意,"*:80:*"
不会这样做;省略第二个星号。On my WebMatrix IIS Express install changing from
"*:80:localhost"
to"*:80:custom.hostname"
didn't work ("Bad Hostname", even with proper etc\hosts mappings), but"*:80:"
did work--and with none of the additional steps required by the other answers here. Note that"*:80:*"
won't do it; leave off the second asterisk.就像上面的 Jessa Flint 一样,我不想手动编辑 .vs\config\applicationhost.config 因为我希望更改保留在源代码管理中。我也不想有一个单独的批处理文件。我正在使用 VS 2015。
项目属性→构建事件→预构建事件命令行:
只需确保将端口号更改为您所需的端口即可。
Like Jessa Flint above, I didn't want to manually edit .vs\config\applicationhost.config because I wanted the changes to persist in source control. I also didn't want to have a separate batch file. I'm using VS 2015.
Project Properties→Build Events→Pre-build event command line:
Just make sure you change the port number to your desired port.
我试图将公共 IP 地址集成到我的工作流程中,但这些答案没有帮助(我喜欢使用 IDE 作为 IDE)。但以上内容引导我找到了解决方案
(我花了大约 2 个小时的时间将其与 Visual Studio 2012 / Windows 8 集成),这就是最终对我有用的方法。
applicationhost.config 由 VisualStudio 在
C:\Users\usr\Documents\IISExpress\config
下生成管理员
身份运行,以便它可以绑定到外部地址(不是本地主机)最终结果是您可以在我的案例中浏览到
192.168.2.102
并进行测试(例如在 Android 模拟器中)。我真的希望这对其他人有帮助,因为这对我来说绝对是一个令人恼火的过程。显然它是一项我希望看到禁用的安全功能。
I was trying to integrate the public IP Address into my workflow and these answers didn't help (I like to use the IDE as the IDE). But the above lead me to the solution
(after about 2 hours of beating my head against a wall to get this to integrate with Visual Studio 2012 / Windows 8) here's what ended up working for me.
applicationhost.config generated by VisualStudio under
C:\Users\usr\Documents\IISExpress\config
Administrator
so that it can bind to outside addresses (not local host)Administrator
so that it can start the process as an administrator allowing the binding to take place.The net result is you can browse to
192.168.2.102
in my case and test (for instance in an Android emulator. I really hope this helps someone else as this was definitely an irritating process for me.Apparently it is a security feature which I'd love to see disabled.
投票的答案是有效的..这些信息对我帮助很大。我知道这个主题之前已经讨论过,但我想添加一些额外的意见。
人们说您必须“手动编辑”Users IISExpress/Config 目录中的 application.config 文件。这对我来说是一个大问题,因为我想通过源代码控制将配置分发给各个开发人员。
我发现您可以使用“C:\Program Files\IIS Express\appcmd.exe”程序自动更新此文件。我花了一段时间才找到控制参数,但我会在这里分享我的发现。本质上,您可以创建一个同时运行 NETSH 命令和 APPCMD.EXE 的 .bat 文件(如果您愿意,也可以交换主机文件),以便使用 IIS Express 轻松配置主机头。
你的安装bat文件看起来像这样:
我还将制作一个“卸载”bat文件来清理这些绑定..(因为很多时候我只是伪造DNS,以便我可以处理对主机名敏感的代码)
我希望这些信息对某人有帮助。我花了一些时间才发现。
The up-voted answer is valid.. and this information helped me quite a bit. I know this topic has been discussed before but I wanted to add some additional input.
People are saying that you must "manually edit" the application.config file in the Users IISExpress/Config directory. This was a big issue for me because I wanted to distribute the configuration via Source control to various developers.
What I found is that you can automate the updating of this file using the "C:\Program Files\IIS Express\appcmd.exe" program. It took a while to find out the control parameters but Ill share my findings here. Essentially you can make a .bat file that runs both the NETSH command and the APPCMD.EXE (and perhaps swap out a host file if you like) to make host header configuration easy with IIS Express.
Your install bat file would look something like this:
I also will make a "Uninstall" bat file that will clean up these bindings..(because often times Im just faking out DNS so that I can work on code that is host name sensitive)
I hope this information is helpful to someone.. It took me a bit to uncover.
此方法已经过测试并适用于 ASP.NET Core 3.1 和 Visual Studio 2019。
将“*:44320:localhost”更改为“*:44320:*”。
两个链接都有效:
现在,如果您想要应用程序要使用自定义域,只需将以下行添加到 host 文件中:
现在:
注意:如果您的应用程序在没有 SSL 的情况下工作,请更改 protocol="http" 部分。
This method has been tested and worked with ASP.NET Core 3.1 and Visual Studio 2019.
Change "*:44320:localhost" to "*:44320:*".
Both links work:
Now if you want the app to work with the custom domain, just add the following line to the host file:
Now:
NOTE: If your app works without SSL, change the protocol="http" part.
按照 Jaro 的建议,我只需稍作修改就可以在 Windows XP 和 IIS Express(通过 Web Matrix 安装)下运行该程序,并且不仅限于本地主机。只需正确设置绑定即可。
%USERPROFILE%\My Documents\IISExpress\config\applicationhost.config
(Windows XP、Vista 和 7 路径类似) 并在中编辑站点定义>
配置块应遵循以下内容:如果运行 MVC,则将 applicationPool 设置为“Integrated”选项之一。
Following Jaro's advice, I was able to get this working under Windows XP and IIS Express (installed via Web Matrix) with a small modification and was not limited to only localhost. It's just a matter of setting the bindings correctly.
%USERPROFILE%\My Documents\IISExpress\config\applicationhost.config
(Windows XP. Vista and 7 paths will be similar) and edit the site definition in the<sites>
config block to be along the lines of the following:If running MVC, then keep the applicationPool set to one of the "Integrated" options.
大卫的解决方案很好。但我发现页面中的
仍然警告“localhost”,因为项目 Url 仍然是 localhost,即使它已被 <代码>http://dev.example.com。我遇到的另一个问题是,即使我按照 David Murdoch 的建议使用 80 端口号禁用了 Skype,它也会提醒我端口 80 已被使用。所以我想出了另一个更简单的解决方案:
http://localhost:33333/
),然后将其从http://localhost:33333/
更改代码> 到http://mydomain:333333/
注意:更改后,您既不能单击“项目 URL”框右侧的“创建虚拟目录”按钮,也不能单击 Visual Studio 的“保存”按钮,否则不会成功。您可以在下一步 3 后保存设置。
http://mydomain:333333/
下打开,页面中的将提醒“mydomain”。
注意:上面列出的端口号假设为33333。您需要将其更改为您的视觉工作室设置的端口号。
编辑后:今天我尝试使用另一个域名并收到以下错误:无法启动 IIS Express Web 服务器。注册 URL 失败...访问被拒绝。 (0x80070005)。我通过右键单击 Windows 任务栏右上角的 IIS Express 图标退出 IIS Express,然后以管理员身份重新启动我的 Visual Studio,问题就消失了。
David's solution is good. But I found the
<script>alert(document.domain);</script>
in the page still alerts "localhost" because the Project Url is still localhost even if it has been override withhttp://dev.example.com
. Another issue I run into is that it alerts me the port 80 has already been in use even if I have disabled the Skype using the 80 port number as recommended by David Murdoch. So I have figured out another solution that is much easier:http://localhost:33333/
), then change it fromhttp://localhost:33333/
tohttp://mydomain:333333/
Note: After this change, you should neither click the "Create Virtual Directory" button on the right of the Project Url box nor click the Save button of the Visual Studio as they won't be succeeded. You can save your settings after next step 3.
http://mydomain:333333/
, and<script>alert(document.domain);</script>
in the page will alert "mydomain".Note: The port number listed above is assumed to be 33333. You need to change it to the port number set by your visual studio.
Post edited: Today I tried with another domain name and got the following error: Unable to launch the IIS Express Web server. Failed to register URL... Access is denied. (0x80070005). I exit the IIS Express by right clicking the IIS Express icon at the right corner in the Windows task bar, and then re-start my visual studio as administrator, and the issue is gone.
我尝试了以上所有方法,但没有任何效果。解决该问题的方法是在主机文件中添加 IPv6 绑定。在 @David Murdochs 回答的第 5 步中,添加两行而不是一行,即:
我通过从命令行检查
$ ping localhost
来计算出来,该命令用于返回:Reply from 127.0.0.1 0.1: bytes=32 time<1ms TTL=128
相反,它现在返回:
Reply from ::1: time<1ms
我不知道为什么,但由于某种原因 IIS Express开始使用 IPv6 而不是 IPv4。
I tried all of above, nothing worked. What resolved the issue was adding IPv6 bindings in the hosts file. In step 5 of @David Murdochs answer, add two lines instead of one, i.e.:
I figured it out by checking
$ ping localhost
from command line, which used to return:Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
Instead, it now returns:
Reply from ::1: time<1ms
I don't know why, but for some reason IIS Express started using IPv6 instead of IPv4.
以防万一有人可能需要...
我的要求是:
443
在 IISExpress 中设置此 URL:
http://my.customdomain。 为了进行设置
,我使用了以下设置:
项目 URL:
http://localhost:57400
启动 URL:
http://my.customdomain.com
<代码>/.vs/{solution-name}/config/applicationhost.config 设置:
Just in case if someone may need...
My requirement was:
443
Setup this URL in IISExpress:
http://my.customdomain.com
To setup this I used following settings:
Project Url:
http://localhost:57400
Start URL:
http://my.customdomain.com
/.vs/{solution-name}/config/applicationhost.config
settings:我的目标是用 URL 中的
127.0.0.1
替换localhost
。最初我收到
Bad Request - Invalid Hostname
这对我有用(Microsoft Visual Studio Professional 2022(64 位)- 当前 - 版本 17.6.4)
以管理员身份运行 cmd并运行这些命令(来自 dmatson 的答案):
备份
\.vs\config\applicationhost.config
。编辑
\.vs\config\applicationhost.config
以将其添加到应用的绑定部分中(将44300
替换为您自己的端口号):确保它按该顺序列出。
如果 VS 已打开,请将其关闭。
以管理员身份运行 VS,您应该能够访问
https://127.0.0.1:portnumber
。My goal was to replace
localhost
with127.0.0.1
in the URL.Originally I was getting
Bad Request - Invalid Hostname
This worked for me (Microsoft Visual Studio Professional 2022 (64-bit) - Current - Version 17.6.4)
Run cmd as admin and run these commands (from dmatson's answer):
Make a backup of
\.vs\config\applicationhost.config
.Edit
\.vs\config\applicationhost.config
to have this in your app's bindings section (replace44300
with your own port number):Make sure it's listed in that order.
Close VS if it's already open.
Run VS as admin, and you should be able to go to
https://127.0.0.1:portnumber
.我为此使用了 iisexpress-proxy (来自 npm)。
https://github.com/icflorescu/iisexpress-proxy
I was using iisexpress-proxy (from npm) for this.
https://github.com/icflorescu/iisexpress-proxy
将其留在这里以防万一有人需要...
我需要在 IIS Express 中为 Wordpress 多站点设置拥有自定义域,但直到我以管理员身份运行 Webmatrix/Visual Studio 为止,一切都不起作用。然后我就能够将子域绑定到同一个应用程序。
然后将运行 http://whatever.localhost:12345/ 。
Leaving this here just in case anyone needs...
I needed to have custom domains for a Wordpress Multisite setup in IIS Express but nothing worked until I ran Webmatrix/Visual Studio as an Administrator. Then I was able to bind subdomains to the same application.
Then going to http://whatever.localhost:12345/ will run.