使用IIS Express自定义域

时间:2011-01-17 00:16:33

标签: iis iis-7 iis-express

传统上我使用自己的localhost开发服务器自定义域。有点像:

dev.example.com
dev.api.example.com

在使用Facebook等外部API时,这为我提供了很大的灵活性。这在过去使用内置的Visual Studio开发服务器时效果很好,因为我需要做的就是为那些指向127.0.0.1的DNS记录添加CNAME。

但是我无法使用IIS Express。我尝试的一切似乎都失败了。我甚至在IIS Express的applicationHost.config文件中添加了正确的XML配置,但它似乎并不认为这些条目与IIS的真正安装一样有效。

<binding protocol="http" bindingInformation="*:1288:dev.example.com" />

每当我输入此行并尝试请求http://dev.example.com:1288时,我会收到以下消息:

  

错误请求 - 主机名无效

有人知道我是否遗漏了一些明显的东西吗?或者,IIS Express团队是否真的缺乏远见才能看到这种用途?

15 个答案:

答案 0 :(得分:350)

这对我有用(针对VS 2013更新,请参阅2010年的修订历史记录, VS 2015 请参阅:https://stackoverflow.com/a/32744234/218971):

  1. 右键单击您的Web应用程序项目▶PropertiesWeb,然后按如下方式配置Servers部分:
    • 从下拉列表中选择 IIS Express▼
    • 项目网址:http://localhost
    • 覆盖应用程序根URL:http://dev.example.com
    • 单击创建虚拟目录(如果此处出现错误,您可能需要禁用IIS 5/6/7/8,将IIS的Default Site更改为除端口{{1}之外的任何内容},确保Skype isn't using port 80等。)
  2. 可选:将:80设置为Start URL
  3. 打开http://dev.example.com(Windows XP,Vista和7)并在%USERPROFILE%\My Documents\IISExpress\config\applicationhost.config配置块中编辑网站定义,使其符合以下几行:

    <sites>
  4. 如果运行MVC:确保将<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> 设置为“Integrated”选项之一(例如“Clr2IntegratedAppPool”)。

  5. 打开hosts file并添加第applicationPool行。
  6. ►开始您的申请!
  7. 评论中提出了一些很好的建议:

      
        
    • 您可能需要以管理员身份运行Visual Studio。
    •   
    • 如果您想让其他开发人员看到您的IIS运行127.0.0.1 dev.example.com
    •   
    • 如果您希望网站解析所有设置为netsh http add urlacl url=http://dev.example.com:80/ user=everyone的主机。   使用你想要的任何端口,80只是方便。
    •   

答案 1 :(得分:139)

对于 Visual Studio 2015 ,上述答案中的步骤适用,但applicationhost.config文件位于新位置。在你的&#34;解决方案&#34;文件夹遵循路径,如果您升级并且在您的计算机上有applicationhost.config的两个版本,这会让人感到困惑。

\.vs\config

在该文件夹中,您将看到 applicationhost.config 文件

或者你可以在你的解决方案文件夹中搜索.config文件并找到它。

我个人使用以下配置:

enter image description here

在我的主机文件中包含以下内容:

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实例!如果您不希望每次我推荐此操作时都这样做:

How to Run Visual Studio as Administrator by default

我希望这对某人有所帮助,我在尝试升级到Visual Studio 2015时遇到了问题,并意识到我的配置都没有被转移。

答案 2 :(得分:44)

将Visual Studio 2012与IIS Express一起使用时,更改现有绑定不会永久有效。 (它可能会工作,直到你关闭VS,但在那之后,事情变得非常混乱。)

关键是保持现有的localhost绑定,然后添加一个新的绑定。

除非您以管理员身份运行,否则您还需要运行netsh add urlacl(授予您自己以标准用户身份运行非本地站点的权限)。

如果要允许任何主机名,完整过程如下:

  1. 创建您的Web应用程序,并找出它正在使用的端口(请参阅项目属性,Web选项卡,Project Url)。
  2. 从管理员提示符处,运行以下命令(将 portnumber 替换为您在#1中找到的端口号):

    netsh http add urlacl url="http://*:portnumber/" user=everyone
    netsh http add urlacl url="http://localhost:portnumber/" user=everyone
    
  3. 您还可以使用您的用户名(DOMAIN \ USER)代替所有人,以提高安全性。

    1. 打开applicationhost.config(通常位于My Documents \ IIS Express \ config下),找到包含端口号的元素。
    2. 使用您想要的主机名添加一个绑定(在本例中为*)。例如:

      <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>
      
    3. 请注意,如果要打开所有主机名(*),则需要两个netsh命令(一个用于*,一个用于localhost)。如果您只想打开特定的主机名,则不需要第二个netsh命令(localhost);只有具有您特定主机名的那个就足够了。

答案 3 :(得分:23)

无效的主机名表示您在IIS Express配置文件中配置的实际站点(很可能)未运行。 IIS Express没有像IIS那样的进程模型。


要使您的站点运行,需要显式启动(通过打开和访问webmatrix,或从命令行调用iisexpress.exe(来自它的安装目录)和/ site参数。


通常,允许使用完全限定的DNS名称进行本地访问的步骤是 让我们使用您的DNS名称dev.example.com

的示例
  1. 编辑%windows%\ system32 \ drivers \ etc \ hosts文件,将dev.example.com映射到127.0.0.1(需要管理员权限)。如果您控制DNS服务器(如在Nick的情况下),则DNS条目就足够了,因为不需要此步骤。
  2. 如果您通过代理访问互联网,请确保dev.example.com不会被要求代理(您必须在浏览器中插入例外列表(对于IE,它将是工具/ Internet选项/连接/ Lan Settings,然后转到Proxy Server / Advanced并将dev.example.com放在exeption列表中。
  3. 为您的站点(例如:Site1)配置IIS Express绑定以包含dev.example.com。使用绑定需要管理权限。或者,可以使用

    使用http.sys进行一次性URL保留

    netsh http add urlacl url=http://dev.example.com:<port>/ user=<user_name>

  4. 启动iisexpress /site:Site1或在WebMatrix中打开Site1

答案 4 :(得分:18)

在我的WebMatrix上,IIS Express安装从"*:80:localhost"更改为"*:80:custom.hostname"无法正常工作(&#34;错误的主机名&#34;,即使有正确的etc \ hosts映射),但{ {1}} 执行工作 - 此处没有其他答案所需的其他步骤。请注意"*:80:"不会这样做;留下第二个星号。

答案 5 :(得分:7)

我试图将公共IP地址集成到我的工作流程中,这些答案没有帮助(我喜欢使用IDE作为IDE)。但上面引导我找到解决方案 (大约2个小时的时间将我的头撞到墙上以使其与Visual Studio 2012 / Windows 8集成)以下是最终为我工作的内容。

VisualStudio在C:\Users\usr\Documents\IISExpress\config

下生成的

applicationhost.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>
  • 将IISExpress设置为以Administrator运行,以便它可以绑定到外部地址(而不是本地主机)
  • 将Visual Stuio作为Administrator运行,以便它可以作为管理员启动该过程,从而允许绑定发生。

最终结果是你可以在我的案例中浏览192.168.2.102并进行测试(例如在Android模拟器中。我真的希望这可以帮助别人,因为这对我来说绝对是一个令人恼火的过程。

显然,这是一项安全功能,我很乐意看到它被禁用。

答案 6 :(得分:4)

根据Jaro的建议,我能够在Windows XP和IIS Express(通过Web Matrix安装)下进行小修改,并且不仅限于localhost。这只是正确设置绑定的问题。

     
  1. 使用WebMatrix从Web应用程序根目录中的文件夹创建新网站。
  2.  
  3. 关闭WebMatrix。
  4.  
  5. 打开%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”选项之一。

答案 7 :(得分:4)

最高投票的答案是有效的......这些信息对我有所帮助。我知道之前讨论过这个话题,但我想补充一些额外的输入。 人们说你必须在Users IISExpress / Config目录中“手动编辑”application.config文件。这对我来说是一个大问题,因为我想通过Source控件将配置分发给各种开发人员。

我发现您可以使用“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']

我希望这些信息对某人有帮助。我花了一点时间才发现。

答案 8 :(得分:3)

与上面的Jessa Flint一样,我不想手动编辑 .vs \ config \ applicationhost.config ,因为我希望更改在源代码管理中保留。我也不想有一个单独的批处理文件。我正在使用 VS 2015

项目属性→构建事件→预构建事件命令行: Screenshot of project properties


::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"

只需确保将端口号更改为所需的端口。

答案 9 :(得分:3)

大卫的解决方案很好。但是我发现页面中的<script>alert(document.domain);</script>仍警告“localhost”,因为Project Url仍然是localhost,即使它已被http://dev.example.com覆盖。我遇到的另一个问题是,即使我使用David Murdoch建议的80端口号禁用Skype,它也会提醒我端口80已经被使用。所以我想出了另一个更容易的解决方案:

  1. 以管理员身份运行记事本,然后打开C:\ Windows \ System32 \ drivers \ etc \ hosts,添加127.0.0.1 mydomain,并保存文件;
  2. 使用Visual Studio 2013打开Web项目(注意:还必须以管理员身份运行),右键单击项目 - &gt;属性 - &gt; Web,(假设“IIS Express”选项下的Project Url为http://localhost:33333/),然后将其从http://localhost:33333/更改为http://mydomain:333333/ 注意:完成此更改后,您既不应单击“项目URL”框右侧的“创建虚拟目录”按钮,也不应单击Visual Studio的“保存”按钮,因为它们将不会成功。您可以在下一步3之后保存设置。
  3. 打开%USERPROFILE%\ My Documents \ IISExpress \ config \ applicationhost.config,搜索“33333:localhost”,然后将其更新为“33333:mydomain”并保存文件。
  4. 按照步骤2中的说明保存您的设置。
  5. 右键单击visual studio中的网页,然后单击“在浏览器中查看”。现在,该页面将在http://mydomain:333333/下打开,页面中的<script>alert(document.domain);</script>将提醒“mydomain”。
  6. 注意:上面列出的端口号假定为33333.您需要将其更改为Visual Studio设置的端口号。

    发布编辑:今天我尝试使用其他域名并出现以下错误:无法启动IIS Express Web服务器。无法注册URL ...访问被拒绝。 (0X80070005)。我通过右键单击Windows任务栏右角的IIS Express图标退出IIS Express,然后以管理员身份重新启动我的visual studio,问题就消失了。

答案 10 :(得分:1)

我尝试了以上所有方法,但没有任何效果。解决此问题的方法是在hosts文件中添加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。

答案 11 :(得分:1)

该方法已经过测试并与ASP.NET Core 3.1和Visual Studio 2019一起使用。

.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

现在:

  • https:// customdomain:44320

注意:如果您的应用程序不支持SSL,请更改protocol =“ http”部分。

答案 12 :(得分:1)

以防万一有人可能需要...

我的要求是:

  • 启用 SSL
  • 自定义域
  • 在(默认)端口运行:443

在 IISExpress 中设置此 URL:http://my.customdomain.com

为了进行设置,我使用了以下设置:

项目网址:http://localhost:57400

起始网址: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>

答案 13 :(得分:0)

我正在使用iisexpress-proxy(来自npm)。

https://github.com/icflorescu/iisexpress-proxy

答案 14 :(得分:0)

离开这里以防万一有人需要...

我需要在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/将会运行。