【问题标题】:Deploying React App on IIS8 Windows server (Azure VM)在 IIS8 Windows 服务器 (Azure VM) 上部署 React App
【发布时间】:2019-04-02 14:51:16
【问题描述】:

请帮忙。我有一个从 VS2017 启动时完美运行的反应应用程序。托管在 Azure VM(IIS-8、Windows Server)上的同一个应用程序会出现 404 或 500 错误。

我的托管目录结构适用于我的 .Net 应用程序以及混合的 .net 和 React 应用程序,但不适用于我的新的仅 React 应用程序。

我的目录结构是 wwwroot>Dashboard。我将生产版本复制到此仪表板文件夹。

我的网络配置是:

`<?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <location path="." inheritInChildApplications="false">
          <system.webServer>
            <handlers>
                <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
            </handlers>
            <aspNetCore processPath="dotnet" arguments=".\Dashboard.dll" stdoutLogEnabled="true" stdoutLogFile=".\logs\stdout" />
            <rewrite>
                    <rules>
                        <rule name="React 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="^/(api)" negate="true" />
                                </conditions>
                            <action type="Rewrite" url="/" />
                        </rule>
                    </rules>
                </rewrite>
            </system.webServer>
        </location>
    </configuration>`

enter image description here

【问题讨论】:

  • 你能分享一下你的 IIS Web 应用程序结构图吗?您的 Web 应用程序是否包含 netcore/react 应用程序和其他反应应用程序? url重写规则似乎让所有请求都转到默认页面,除了三个特殊条件。
  • 请看上面链接的截图。它不允许我嵌入图像。是的,我们的应用是 netcore/react 应用。

标签: reactjs iis deployment react-router window-server


【解决方案1】:

据我所知,asp.net 核心应用程序使用 services.AddSpaStaticFiles 来服务 React 应用程序,而不是使用 url 重写。

我建议您可以检查 Startup.cs 的 Configure 和 ConfigureServices 方法,以确保您已添加 SPA 设置。

代码如下:

     public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        // In production, the React files will be served from this directory
        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/build";
        });
    }

    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseSpaStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller}/{action=Index}/{id?}");
        });

        app.UseSpa(spa =>
        {
            //the source path of the react application
            spa.Options.SourcePath = "ClientApp";

            if (env.IsDevelopment())
            {
                spa.UseReactDevelopmentServer(npmScript: "start");
            }
        });
    }

【讨论】:

  • 如果使用此设置,则无需添加url重写规则。我建议您可以尝试删除该规则。如果显示 500 错误,请发布有关 500 错误的详细错误消息。
猜你喜欢
  • 1970-01-01
  • 2016-08-24
  • 1970-01-01
  • 2020-09-21
  • 1970-01-01
  • 2020-12-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多