【问题标题】:Using virtual directory to host SPA and backend API on same Azure Web App使用虚拟目录在同一个 Azure Web App 上托管 SPA 和后端 API
【发布时间】:2020-05-09 18:47:01
【问题描述】:

我正在尝试提供我的 Angular SPA 并在同一个 Azure Web 应用程序上运行 Spring-boot 后端 API,以便它们共享同一个域。

我创建了一个虚拟目录api/ 用于运行我的后端并将根虚拟目录指向角度文件:

我在每个文件夹中使用了默认的web.config。在后台wwwroot/api:

<configuration>
    <system.webServer>
        <handlers>
            <add name="MyhttpPlatformHandler" path="*" verb="*" modules="httpPlatformHandler" resourceType="Unspecified"/>
        </handlers>
        <httpPlatform processPath="%JAVA_HOME%\bin\java.exe"
                      arguments="-Djava.net.preferIPv4Stack=true -Dserver.port=%HTTP_PLATFORM_PORT% -jar &quot;D:\home\site\wwwroot\api\my_packaged_api-1.0-SNAPSHOT.jar&quot; ">
        </httpPlatform>
    </system.webServer>
</configuration>

在 Angular 应用文件夹中 wwwroot/dist :

<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" />
                      <add input="{REQUEST_URI}" pattern="^/assets/(.*)$" ignoreCase="true" negate="true" />
                   </conditions>
                   <action type="Rewrite" url="/" />
                </rule>
             </rules>
          </rewrite>
          <staticContent>
             <mimeMap fileExtension=".json" mimeType="application/json" />
          </staticContent>
       </system.webServer>
</configuration>

问题

API 工作正常,但是当我尝试从我的 Angular 应用程序访问任何文件时,我从 tomcat 服务器得到 404。 查看启动的进程时,我看到在wwwroot/dist 中启动了第二个Java 进程。所有 404 最终都会出现在此进程日志中。

检查请求诊断我可以看到重写规则正常工作并找到我的文件。但似乎请求仍然传递给这个额外的 tomcat 进程并返回 404

我应该在 web.config 中更改什么以便它真正返回我的文件吗?

【问题讨论】:

    标签: angular spring-boot iis azure-web-app-service azure-webapps


    【解决方案1】:

    用wwwroot/dist 的根 web.config 中的两件事修复它:

    • 通过添加inheritInChildApplications="false" 避免将重写规则继承到子 web.config
    • 清除处理程序并在 web.config 开头添加静态文件处理程序:
    <configuration>
     <location path="."  inheritInChildApplications="false">
      <system.webServer>
        <handlers>
            <clear />
             <add name="StaticFile" path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" resourceType="Either" requireAccess="Read" />
        </handlers>
        <rewrite>
        ...
    

    【讨论】:

      【解决方案2】:

      雷米的回答对我有用。但最好记住这样的关闭标签位置是必要的

      <configuration>
       <location path="."  inheritInChildApplications="false">
        <system.webServer>
          <handlers>
              <clear />
               <add name="StaticFile" path="*" verb="*" modules="StaticFileModule,DefaultDocumentModule,DirectoryListingModule" resourceType="Either" requireAccess="Read" />
          </handlers>
          <rewrite>
          ...
       </location>
      </configuration>
      

      我花了几个小时寻找我的 web.config 出了什么问题。

      【讨论】:

        猜你喜欢
        • 2021-08-19
        • 2016-06-07
        • 1970-01-01
        • 2020-08-02
        • 2020-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多