You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx搭配ASP.NET后端生产环境404错误及发布异常排查

问题排查与解决方案

问题描述

我执行以下命令发布项目:

dotnet publish -o "C:\Temp\Feisser" -r linux-arm --self-contained

发布完成后生成了大量.dll文件和wwwroot文件夹,但手动运行index.html显示空白;通过Nginx反向代理启动dotnet项目时出现404错误,日志如下:

info: Microsoft.AspNetCore.Hosting.Diagnostics[2]
      Request finished HTTP/1.1 GET http://192.168.178.24/ - - - 404 0 - 705.8686ms

未启动dotnet项目时,Nginx返回502错误。项目的.csproj配置如下:

<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>net6.0</TargetFramework>
    <TypeScriptCompileBlocked>true</TypeScriptCompileBlocked>
    <TypeScriptToolsVersion>Latest</TypeScriptToolsVersion>
    <IsPackable>false</IsPackable>
    <SpaRoot>ClientApp\</SpaRoot>
    <DefaultItemExcludes>$(DefaultItemExcludes);$(SpaRoot)node_modules\**</DefaultItemExcludes>
  </PropertyGroup>

  <ItemGroup>
    <Content Remove="$(SpaRoot)**"/>
    <None Remove="$(SpaRoot)**"/>
    <None Include="$(SpaRoot)**" Exclude="$(SpaRoot)node_modules\**"/>
  </ItemGroup>

  <Target Name="DebugEnsureNodeEnv" BeforeTargets="Build" Condition=" '$(Configuration)' == 'Debug' And !Exists('$(SpaRoot)node_modules') ">
    <Exec Command="node --version" ContinueOnError="true">
      <Output TaskParameter="ExitCode" PropertyName="ErrorCode"/>
    </Exec>
    <Error Condition="'$(ErrorCode)' != '0'" Text="Node.js is required to build and run this project. To continue, please install Node.js from https://nodejs.org/, and then restart your command prompt or IDE."/>
    <Message Importance="high" Text="Restoring dependencies using 'npm'. This may take several minutes..."/>
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install"/>
  </Target>

  <Target Name="PublishRunWebpack" AfterTargets="ComputeFilesToPublish">
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm install"/>
    <Exec WorkingDirectory="$(SpaRoot)" Command="npm run build -- --prod"/>
    <ItemGroup>
      <DistFiles Include="$(SpaRoot)build\**"/>
      <DistFiles Include="$(SpaRoot)node_modules\**" Condition="'$(BuildServerSideRenderer)' == 'true'" />
      <ResolvedFileToPublish Include="@(DistFiles->'%(FullPath)')" Exclude="@(ResolvedFileToPublish)">
        <RelativePath>wwwroot\%(RecursiveDir)%(FileName)%(Extension)</RelativePath>
        <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
        <ExcludeFromSingleFile>true</ExcludeFromSingleFile>
      </ResolvedFileToPublish>
    </ItemGroup>
  </Target>
</Project>

修复步骤

1. 解决手动运行index.html空白问题

SPA项目的index.html依赖前端打包后的静态资源与前端路由逻辑,直接打开本地文件会因跨域、资源路径错误导致空白,必须通过dotnet后端服务托管访问。

2. 修复dotnet 404错误

ASP.NET Core需要明确配置静态文件服务与SPA fallback路由,确保前端请求被正确处理:
在Program.cs中添加以下配置:

// 启用静态文件服务,托管wwwroot下的前端资源
app.UseStaticFiles();

// 配置SPA fallback路由,所有前端路由请求都指向index.html
app.MapFallbackToFile("index.html");

3. 修正Nginx配置

502错误说明Nginx无法连接dotnet后端,404说明连接成功但后端无匹配资源,调整Nginx配置如下:

server {
    listen 80;
    server_name 192.168.178.24;

    location / {
        proxy_pass http://localhost:5000; # 与dotnet项目监听端口保持一致
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection keep-alive;
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header X-Forwarded-Proto $scheme;
    }
}

同时确保dotnet项目监听正确端口:

  • 启动时通过命令行指定:dotnet YourProject.dll --urls=http://localhost:5000
  • 或在appsettings.json中配置:
    {
      "Urls": "http://localhost:5000"
    }
    

4. 验证发布产物完整性

检查C:\Temp\Feisser\wwwroot目录下是否包含前端打包后的所有文件(如js、css、index.html),若缺失则重新执行发布命令,添加-c Release确保是生产模式打包:

dotnet publish -c Release -o "C:\Temp\Feisser" -r linux-arm --self-contained

同时检查发布过程中npm install、npm run build是否有报错,优先修复前端项目的依赖或打包问题。

5. 单独测试dotnet服务

在发布目录下直接运行dotnet项目(linux-arm环境下执行./YourProjectName),访问http://localhost:5000确认页面正常加载,排除Nginx的影响。


内容的提问来源于stack exchange,提问作者Feisser

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 16:22:15