[英]Using Custom Domains With 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 团队真的缺乏预见这种用途的先见之明?
这对我有用(针对 VS 2013 更新,请参阅 2010 年的修订历史,对于VS 2015,请参阅: https : //stackoverflow.com/a/32744234/218971 ):
右键单击您的 Web 应用程序项目 ▶ Properties
▶ Web
,然后按如下方式配置Servers
部分:
http://localhost
http://dev.example.com
: http://dev.example.com
Default Site
更改为端口:80
任何内容,确保Skype 未使用端口 80等) 可选:将Start URL
设置为http://dev.example.com
打开%USERPROFILE%\\My Documents\\IISExpress\\config\\applicationhost.config
(Windows XP、Vista 和 7),然后将<sites>
配置块中的站点定义编辑为如下所示:
<site name="DevExample" id="997005936"> <application path="/" applicationPool="Clr2IntegratedAppPool"> <virtualDirectory path="/" physicalPath="C:\\path\\to\\application\\root" /> </application> <bindings> <binding protocol="http" bindingInformation=":80:dev.example.com" /> </bindings> <applicationDefaults applicationPool="Clr2IntegratedAppPool" /> </site>
如果运行 MVC:确保applicationPool
设置为“Integrated”选项之一(如“Clr2IntegratedAppPool”)。
打开您的hosts
文件并添加行127.0.0.1 dev.example.com
。
► 开始您的申请!
来自评论的一些很好的建议:
- 您可能需要以管理员身份运行 Visual Studio。
- 如果你想让其他开发者看到你的 IIS 运行
netsh http add urlacl url=http://dev.example.com:80/ user=everyone
- 如果您希望站点为所有主机解析,请设置
bindingInformation="*:80:"
。 使用任何你想要的端口,80只是方便。 要解析所有主机,您需要以管理员身份运行 Visual Studio
对于Visual Studio 2015 ,上述答案中的步骤适用,但applicationhost.config
文件位于新位置。 在您的“解决方案”文件夹中跟随路径,如果您升级并且在您的机器上有两个版本的applicationhost.config
,这会令人困惑。
\.vs\config
在该文件夹中,您将看到您的applicationhost.config文件
或者,您可以在您的解决方案文件夹中搜索 .config 文件并以这种方式找到它。
我个人使用了以下配置:
在我的主机文件中包含以下内容:
127.0.0.1 jam.net
127.0.0.1 www.jam.net
我的 applicationhost.config 文件中的以下内容:
<site name="JBN.Site" id="2">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="C:\Dev\Jam\shoppingcart\src\Web\JBN.Site" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:49707:" />
<binding protocol="http" bindingInformation="*:49707:localhost" />
</bindings>
</site>
请记住以管理员身份运行您的 Visual Studio 2015 实例! 如果你不想每次我推荐这个时都这样做:
我希望这对某人有所帮助,我在尝试升级到 Visual Studio 2015 时遇到了问题,并意识到我的任何配置都没有被结转。
将 Visual Studio 2012 与 IIS Express 一起使用时,更改现有绑定不会永久起作用。 (在您关闭 VS 之前它可能会工作,但在那之后,事情变得非常混乱。)
关键是保留现有的 localhost 绑定并在其后添加新的绑定。
除非您以管理员身份运行,否则您还需要运行netsh add urlacl
(以授予自己以标准用户身份运行非本地主机站点的权限)。
如果要允许任何主机名,完整流程如下:
在管理员提示下,运行以下命令(将portnumber替换为您在 #1 中确定的端口号):
netsh http add urlacl url="http://*:portnumber/" user=everyone netsh http add urlacl url="http://localhost:portnumber/" user=everyone
您也可以使用您的用户名 (DOMAIN\\USER) 而不是每个人,以获得更好的安全性。
使用您想要的主机名(在本例中为 *)再添加一个绑定。 例如:
<site name="MvcApplication1" id="2"> <application path="/" applicationPool="Clr4IntegratedAppPool"> <virtualDirectory path="/" physicalPath="C:\\sites\\MvcApplication1" /> </application> <bindings> <binding protocol="http" bindingInformation="*:12853:localhost" /> <binding protocol="http" bindingInformation="*:12853:*" /> </bindings> </site>
请注意,如果要打开所有主机名 (*),您需要两个 netsh 命令(一个用于 *,一个用于 localhost)。 如果你只想打开一个特定的主机名,你并不严格需要第二个netsh命令(localhost); 仅具有您的特定主机名的那个就足够了。
无效的主机名表示您在 IIS Express 配置文件中配置的实际站点(很可能)未运行。 IIS Express 没有像 IIS 那样的进程模型。
为了让您的站点运行,它需要显式启动(通过从 webmatrix 打开和访问,或从命令行调用 iisexpress.exe(从它的安装目录)使用 /site 参数启动。
通常,允许将完全限定的 DNS 名称用于本地访问的步骤是让我们使用您的 DNS 名称示例 dev.example.com
为您的站点(例如:Site1)配置 IIS Express 绑定以包含 dev.example.com。 使用绑定需要管理权限。 或者,可以使用 http.sys 进行一次性 URL 保留
netsh http add urlacl url=http://dev.example.com:<port>/ user=<user_name>
启动iisexpress /site:Site1
或在 WebMatrix 中打开 Site1
在我的 WebMatrix IIS Express 安装中,从"*:80:localhost"
更改为"*:80:custom.hostname"
不起作用(“Bad Hostname”,即使使用正确的 etc\\hosts 映射),但是"*:80:"
确实有效——而且这里没有其他答案要求的额外步骤。 注意"*:80:*"
不会这样做; 去掉第二个星号。
像上面的 Jessa Flint一样,我不想手动编辑.vs\\config\\applicationhost.config,因为我希望更改保留在源代码管理中。 我也不想有一个单独的批处理文件。 我正在使用VS 2015 。
::The following configures IIS Express to bind to any address at the specified port
::remove binding if it already exists
"%programfiles%\IIS Express\appcmd.exe" set site "MySolution.Web" /-bindings.[protocol='http',bindingInformation='*:1167:'] /apphostconfig:"$(SolutionDir).vs\config\applicationhost.config"
::add the binding
"%programfiles%\IIS Express\appcmd.exe" set site "MySolution.Web" /+bindings.[protocol='http',bindingInformation='*:1167:'] /apphostconfig:"$(SolutionDir).vs\config\applicationhost.config"
只需确保将端口号更改为所需的端口即可。
我试图将公共 IP 地址集成到我的工作流程中,但这些答案没有帮助(我喜欢使用 IDE 作为 IDE)。 但是以上内容让我找到了解决方案(经过大约 2 个小时的敲击墙使其与 Visual Studio 2012 / Windows 8 集成之后)这就是最终对我有用的方法。
对ApplicationHost.config由下的VisualStudio产生C:\\Users\\usr\\Documents\\IISExpress\\config
<site name="MySite" id="1">
<application path="/" applicationPool="Clr4IntegratedAppPool">
<virtualDirectory path="/" physicalPath="C:\Users\usr\Documents\Visual Studio 2012\Projects\MySite" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:8081:localhost" />
<binding protocol="http" bindingInformation="*:8082:localhost" />
<binding protocol="http" bindingInformation="*:8083:192.168.2.102" />
</bindings>
</site>
Administrator
身份运行,以便它可以绑定到外部地址(不是本地主机)Administrator
身份运行 Visual Stuio,以便它可以以管理员身份启动进程,从而允许进行绑定。 最终结果是您可以在我的情况下浏览到192.168.2.102
并进行测试(例如在 Android 模拟器中。我真的希望这对其他人有所帮助,因为这对我来说绝对是一个令人恼火的过程。
显然,这是一项我希望禁用的安全功能。
赞成票的答案是有效的……这些信息对我帮助很大。 我知道之前已经讨论过这个话题,但我想添加一些额外的输入。 人们说您必须“手动编辑”用户 IISExpress/Config 目录中的 application.config 文件。 这对我来说是一个大问题,因为我想通过源代码控制将配置分发给不同的开发人员。
我发现您可以使用“C:\\Program Files\\IIS Express\\appcmd.exe”程序自动更新此文件。 找出控制参数花了一段时间,但我会在这里分享我的发现。 本质上,您可以制作一个同时运行 NETSH 命令和 APPCMD.EXE 的 .bat 文件(如果您愿意,也可以换出一个主机文件),以便使用 IIS Express 轻松配置主机头文件。
您的安装 bat 文件将如下所示:
netsh http add urlacl url=http://yourcustomdomain.com:80/ user=everyone
"C:\Program Files\IIS Express\appcmd.exe" set site "MyApp.Web" /+bindings.[protocol='http',bindingInformation='*:80:yourcustomdomain.com']
我还将制作一个“卸载”bat 文件来清理这些绑定..(因为很多时候我只是伪造 DNS,以便我可以处理对主机名敏感的代码)
netsh http delete urlacl url=http://yourcustomdomain.com:80/
"C:\Program Files\IIS Express\appcmd.exe" set site "MyApp.Web" /-bindings.[protocol='http',bindingInformation='*:80:yourcustomdomain.com']
我希望这些信息对某人有帮助.. 我花了一点时间才发现。
按照 Jaro 的建议,我能够在 Windows XP 和 IIS Express(通过 Web Matrix 安装)下进行一些小的修改,并且不仅限于本地主机。 这只是正确设置绑定的问题。
<sites>
config block to be along the lines of the following:打开%USERPROFILE%\\My Documents\\IISExpress\\config\\applicationhost.config
并编辑<sites>
配置块中的站点定义,使其符合以下内容: <site name="DevExample" id="997005936"> <application path="/" applicationPool="Clr2IntegratedAppPool"> <virtualDirectory path="/" physicalPath="C:\\path\\to\\application\\root" /> </application> <bindings> <binding protocol="http" bindingInformation="*:80:dev.example.com" /> </bindings> <applicationDefaults applicationPool="Clr2IntegratedAppPool" /> </site>
如果运行 MVC,则将 applicationPool 设置为“集成”选项之一。
大卫的解决方案很好。 但是我发现页面中的<script>alert(document.domain);</script>
仍然警告“localhost”,因为项目 URL 仍然是 localhost,即使它已被http://dev.example.com
覆盖。 我遇到的另一个问题是,即使我按照 David Murdoch 的建议使用 80 端口号禁用了 Skype,它也会提醒我 80 端口已被使用。 所以我想出了另一个更容易的解决方案:
http://localhost:33333/
),然后将其从http://localhost:33333/
更改为http://mydomain:333333/
注意:更改后,您不应单击 Project Url 框右侧的“创建虚拟目录”按钮也不要点击 Visual Studio 的 Save 按钮,因为它们不会成功。 您可以在下一步 3 之后保存您的设置。http://mydomain:333333/
下打开,并且页面中的<script>alert(document.domain);</script>
将警告“mydomain”。注意:上面列出的端口号假设为33333,你需要把它改成你的visual studio设置的端口号。
帖子编辑:今天我尝试使用另一个域名并收到以下错误:无法启动 IIS Express Web 服务器。 注册 URL 失败...访问被拒绝。 (0x80070005) 。 我通过右键单击 Windows 任务栏中右上角的 IIS Express 图标退出 IIS Express,然后以管理员身份重新启动我的 Visual Studio,问题就消失了。
.vs\\PROJECTNAME\\config\\applicationhost.config
将“*:44320:localhost”更改为“*:44320:*”。
<bindings>
<binding protocol="http" bindingInformation="*:5737:localhost" />
<binding protocol="https" bindingInformation="*:44320:*" />
</bindings>
两个链接都有效:
现在,如果您希望应用程序使用自定义域,只需将以下行添加到主机文件中:
C:\\Windows\\System32\\drivers\\etc\\hosts
127.0.0.1 customdomain
现在:
注意:如果您的应用程序在没有 SSL 的情况下工作,请更改 protocol="http" 部分。
我尝试了以上所有方法,没有任何效果。 解决该问题的是在主机文件中添加 IPv6 绑定。 在@David Murdochs 回答的第 5 步中,添加两行而不是一行,即:
127.0.0.1 dev.example.com
::1 dev.example.com
我通过从命令行检查$ ping localhost
来解决这个问题,它曾经返回:
Reply from 127.0.0.1: bytes=32 time<1ms TTL=128
相反,它现在返回:
Reply from ::1: time<1ms
我不知道为什么,但出于某种原因,IIS Express 开始使用 IPv6 而不是 IPv4。
以防万一,如果有人可能需要...
我的要求是:
443
在 IISExpress 中设置此 URL: http://my.customdomain.com
://my.customdomain.com
为了设置它,我使用了以下设置:
项目网址: http://localhost:57400
起始网址: http://my.customdomain.com
: http://my.customdomain.com
/.vs/{solution-name}/config/applicationhost.config
设置:
<site ...>
<application>
...
</application>
<bindings>
<binding protocol="http" bindingInformation="*:57400:" />
<binding protocol="https" bindingInformation="*:443:my.customdomain.com" />
</bindings>
</site>
为此,我使用了 iisexpress-proxy(来自 npm)。
把这个留在这里以防万一有人需要......
我需要在 IIS Express 中为 Wordpress Multisite 设置自定义域,但在我以管理员身份运行 Webmatrix/Visual Studio 之前没有任何效果。 然后我能够将子域绑定到同一个应用程序。
<bindings>
<binding protocol="http" bindingInformation="*:12345:localhost" />
<binding protocol="http" bindingInformation="*:12345:whatever.localhost" />
</bindings>
然后转到http://whatever.localhost:12345/将运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.