【问题标题】:How to set up ReactJS app on IIS properly?如何在 IIS 上正确设置 ReactJS 应用程序?
【发布时间】:2017-03-13 20:54:31
【问题描述】:

浏览了同一问题的几个主题。没有一个对我有用。 事情是: 我有从 create-react-app 设计的 ReactJS + Redux 应用程序。实际上它可以工作,但是当应用程序冲浪以不同于 root 的 URL 开始时会抛出 404。 安装iisnode 并尝试了网络中推荐的不同重写规则,但没有任何效果。 最后是这个:

    <rule name="Redirect HTTP to HTTPS">
      <match url="(.*)" />
      <conditions>
        <add input="{HTTPS}" pattern="off" ignoreCase="true" />
      </conditions>
      <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent"/>
    </rule> -->

    <rule name="Redirect non-existent paths to root">
      <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="^/initialLogin/*" ignoreCase="true" negate="true" />            
      </conditions>
      <action type="Redirect" url="/" appendQueryString="false" />
    </rule>
  </rules>
</rewrite>

我唯一需要的是通过根 url 发送所有传入请求,并将其余地址传递给它,以便 SPA 可以正常工作。 托管:AzureVM IIS:v10

编辑:另一个奇怪的事情是 iisnode 可以处理带有 js 文件的重定向,但是我的应用程序呈现在 index.html 中,当我试图将 index.html 指向作为处理程序时,它实际上向我显示了一个空白页。

【问题讨论】:

  • 我认为我在 IISNode 上写的另一个答案会对您有所帮助,请查看here
  • 感谢@peteb,部分帮助找到了解决方案

标签: node.js reactjs iis iis-10


【解决方案1】:

这个web.config 为我工作。希望它会帮助别人。

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <!-- This part is responsible for URL rewrites -->
        <rewrite>
          <rules>
            <rule name="ReactJS 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>
        <!-- This allows CORS for testing purposes -->
        <httpProtocol>
         <customHeaders>
           <add name="Access-Control-Allow-Origin" value="*" />
         </customHeaders>
       </httpProtocol>
  </system.webServer>
</configuration>

【讨论】:

  • 在客户端的情况下会起作用吗?如果是,那么 {REQUEST_FILENAME}REQUEST_URI 是什么
  • 这在 2021 年 3 月 24 日对我有效,使用 IIS 10 和 iisnode 0.2.26.0
猜你喜欢
  • 2013-03-18
  • 2011-11-02
  • 2016-08-12
  • 1970-01-01
  • 2011-09-16
  • 2020-08-04
  • 1970-01-01
  • 2018-01-12
  • 1970-01-01
相关资源
最近更新 更多