You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在IIS上部署包含多个应用的Angular 15项目

Angular IIS部署问题解决及步骤

已做操作

  • 修改index.html的base href:
<base href="/AppName/">
  • 添加IIS路由重写配置(web.config):
<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Angular Routes" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/index.html" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>
  • 查过Stack Overflow,但多数内容没用,问题没解决。

可行的IIS部署步骤

  1. 构建生产包
    在项目根目录执行命令,自动设置base href并生成构建文件:
ng build --prod --base-href "/AppName/"

不用手动改index.html的base标签,这个参数会自动处理,避免手动修改出错。

  1. 部署构建文件
    找到项目下的dist/AppName文件夹(构建生成的产物目录),把里面所有文件复制到IIS站点根目录下的AppName子文件夹中(比如IIS站点物理路径是C:\inetpub\wwwroot,就建C:\inetpub\wwwroot\AppName,把构建文件放进去)。

  2. 修正路由重写配置
    把web.config放在AppName文件夹根目录,注意重写目标要加上应用名,否则会跳错路径,修正后的配置:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Angular Routes" stopProcessing="true">
          <match url=".*" />
          <conditions logicalGrouping="MatchAll">
            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
          </conditions>
          <action type="Rewrite" url="/AppName/index.html" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

先确认IIS装了URL重写模块,没装的话去IIS管理器的“模块”里添加,或者用Web平台安装程序装。

  1. 配置IIS应用程序
    打开IIS管理器,右键你的站点,选“添加应用程序”:
  • 别名填AppName
  • 物理路径指向刚才放构建文件的AppName文件夹
  • 应用程序池选“无托管代码”的池(Angular是静态文件,不需要.NET托管)
  1. 测试验证
    访问http://你的服务器地址/AppName,刷新任意路由页面,确认不会出现404错误。

关于官方文档的疑问

Angular官方文档其实有IIS部署相关内容,但分散在生产构建、服务器配置等章节里,像子应用重写路径这种细节需要结合IIS特性自行调整,容易被忽略,导致看起来像是没提供相关内容。

内容的提问来源于stack exchange,提问作者ertucode

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.01 00:31:01