简体   繁体   English

如何在IIS上正确设置ReactJS应用?

[英]How to set up ReactJS app on IIS properly?

Watched through few topics on same question. 看了几个关于同一问题的话题。 None worked for me. 没有人为我工作。 The thing is: I have ReactJS + Redux app designed from create-react-app. 关键是:我有从create-react-app设计的ReactJS + Redux应用程序。 Actually it works but when app surfing starts with URL different from root is throws out 404. Installed iisnode and tried out different rewrite rules recommended in web, but nothing works. 确实可以,但是当应用程序浏览以不同于root的URL开头时,抛出404。安装了iisnode并尝试了Web中推荐的不同重写规则,但是没有任何效果。 Last was this one: 最后一个是:

    <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>

The only thing I need is to send all incoming requests through root url with passing the rest of the address to it so that SPA could work properly. 我唯一需要做的是通过根url发送所有传入请求,并将其余的地址传递给它,以便SPA可以正常工作。 Hosting: AzureVM IIS: v10 托管:AzureVM IIS:v10

Edit: Another curious thing is thet iisnode can handle redirections with js files, but my application renders in index.html and when I tried to point index.html as handler it actually showed me a blank page. 编辑:另一个奇怪的是iisnode可以处理js文件的重定向,但是我的应用程序在index.html呈现,当我尝试将index.html指向处理程序时,它实际上向我显示了一个空白页。

This web.config works for me. 这个web.config对我web.config Hope it will help someone else. 希望它会帮助别人。

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM