在同一 Azure Web App 上使用虚拟目录来托管 SPA 和后端 API。

问题描述 投票:0回答:1

我试图在同一个Azure Web应用上为我的Angular SPA和运行Spring-boot后端API提供服务,以便它们共享同一个域。

我已经创建了一个虚拟目录 api/ 用于运行我的后台,并将根虚拟目录指向angular文件。Virtual directory configuration

我使用了默认的 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 app文件夹中 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. 所有的404最后都会出现在这个进程日志中。

lonely java process in spa folder

检查请求诊断,我可以看到重写规则正确地工作并找到了我的文件。但似乎请求还是被传递给了这个额外的Tomcat进程,并返回404。

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

angular spring-boot iis azure-web-app-service azure-webapps
1个回答
0
投票

固定它与两个东西在根web.config中的 wwwroot/dist :

  • 通过在子web.config中添加以下内容避免重写规则的继承 inheritInChildApplications="false"
  • 清除处理程序并在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>
    ...
© www.soinside.com 2019 - 2024. All rights reserved.