React + .NET Web API部署IIS后公网访问出现重定向过多(ERR_TOO_MANY_REDIRECTS)问题求助
React + .NET Web API部署IIS后公网访问出现重定向过多(ERR_TOO_MANY_REDIRECTS)问题求助
我第一次尝试把React应用嵌套在.NET API里部署到IIS,本地运行完全没问题,但通过公网URL访问时就出现了ERR_TOO_MANY_REDIRECTS错误,折腾了好久还没解决,想请各位帮忙看看。
先说说目前的情况:
- 本地用localhost运行时,React和API的集成完全正常,没有任何问题
- 用同一个公网URL部署纯静态网站是可以正常访问的,说明DNS配置没问题
- 检查过SSL设置和主站点配置,没找到问题所在
- 后来我换了部署方式:把React作为IIS主站点,.NET API作为子应用部署,页面能正常加载,但所有API请求都返回重定向过多错误
我配置的URL Rewrite规则如下:
<rewrite> <rules> <!-- Redirect HTTP to HTTPS --> <rule name="HTTP to HTTPS Redirect" stopProcessing="true"> <match url="(.*)" /> <conditions> <add input="{HTTPS}" pattern="off" /> </conditions> <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" /> </rule> <!-- Handle React Router client-side routing --> <rule name="React Router Routes" stopProcessing="true"> <match url=".*" /> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" /> </conditions> <action type="Rewrite" url="/" /> </rule> </rules> </rewrite>
Program.cs里的相关配置:
// Enable CORS builder.Services.AddCors(options => options.AddPolicy("corspolicy", build => { build.WithOrigins("*").AllowAnyMethod().AllowAnyHeader(); })); // Register React app builder.Services.AddSpaStaticFiles(options => { options.RootPath = "ClientApp/build"; }); app.UseCors("corspolicy"); app.UseHttpsRedirection(); app.UseSpaStaticFiles(); app.UseStaticFiles(); app.UseRouting(); app.UseRouting(); // 这里注意有重复调用,不知道会不会有影响? app.UseSpa(spa => { spa.Options.SourcePath = "ClientApp"; //if (app.Environment.IsDevelopment()) //{ // spa.UseReactDevelopmentServer(npmScript: "start"); //} });
排查过程中发现的线索:
- 查看IIS日志时,发现我自己设备的IP访问主机名时返回了500状态码,但本地访问却没有这个问题
- 启用了Failed Request Tracing,截取了部分日志片段如下:
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event"> <System> <Provider Name="WWW Server" Guid="{3A2A4E84-4C21-4981-AE10-3FDA0D9B0F83}"/> <EventID>0</EventID> <Version>1</Version> <Level>5</Level> <Opcode>1</Opcode> <Keywords>0x100</Keywords> <TimeCreated SystemTime="2023-08-08T11:10:59.811Z"/> <Correlation ActivityID="{80000447-0005-FD00-B63F-84710C7967BB}"/> <Execution ProcessID="23840" ThreadID="15608"/> <Computer>NHIC-WEB-01</Computer> </System> <EventData> <Data Name="ContextId">{80000447-0005-FD00-B63F-84710C7967BB}</Data> <Data Name="ModuleName">RewriteModule</Data> <Data Name="Notification">1</Data> <Data Name="fIsPostNotification">false</Data> </EventData> <RenderingInfo Culture="en-US"> <Opcode>NOTIFY_MODULE_START</Opcode> <Keywords> <Keyword>RequestNotifications</Keyword> </Keywords> <freb:Description Data="Notification">BEGIN_REQUEST</freb:Description> </RenderingInfo> <ExtendedTracingInfo xmlns="http://schemas.microsoft.com/win/2004/08/events/trace"> <EventGuid>{002E91E3-E7AE-44AB-8E07-99230FFA6ADE}</EventGuid> </ExtendedTracingInfo> </Event> <Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event"> <System> <Provider Name="WWW Server" Guid="{3A2A4E84-4C21-4981-AE10-3FDA0D9B0F83}"/> <EventID>0</EventID> <Version>1</Version> <Level>5</Level> <Opcode>1</Opcode> <Keywords>0x100</Keywords> <TimeCreated SystemTime="2023-08-08T11:10:59.811Z"/> <Correlation ActivityID="{80000447-0005-FD00-B63F-84710C7967BB}"/> <Execution ProcessID="23840" ThreadID="15608"/> <Computer>NHIC-WEB-01</Computer> </System> <EventData> <Data Name="ContextId">{80000447-0005-FD00-B63F-84710C7967BB}</Data> <Data Name="ModuleName">ConfigurationValidationModule</Data> <Data Name="Notification">1</Data> <Data Name="fIsPostNotification">false</Data> </EventData> <RenderingInfo Culture="en-US"> <Opcode>NOTIFY_MODULE_START</Opcode> <Keywords> <Keyword>RequestNotifications</Keyword> </Keywords> <freb:Description Data="Notification">BEGIN_REQUEST</freb:Description> </RenderingInfo> <ExtendedTracingInfo xmlns="http://schemas.microsoft.com/win/2004/08/events/trace"> <EventGuid>{002E91E3-E7AE-44AB-8E07-99230FFA6ADE}</EventGuid> </ExtendedTracingInfo> </Event>
因为是第一次做这种部署,完全没头绪,也想顺便搞清楚这类部署的标准流程,方便以后的项目参考,麻烦各位帮忙分析下问题出在哪了?
备注:内容来源于stack exchange,提问作者Mohammed Abed
相关产品推荐
相关产品推荐

