iis 使用ASP.NET Core的NextJS

fkvaft9z  于 2023-08-05  发布在  .NET
关注(0)|答案(1)|浏览(120)

我有一个NextJS应用程序,后端是用ASP.NETCore编写的。我需要在IIS的同一个网站上托管这两个服务器。
我已经成功托管了NextJS应用程序,并在我的网站下创建了一个应用程序和一个虚拟目录,并添加了重写规则,如下面的web.config所示:

<configuration>
    <system.webServer>
        <webSocket enabled="false" />
        <handlers>
            <!-- Indicates that the server.js file is a node.js site to be handled by the iisnode module -->
            <add name="iisnode" path="server.js" verb="*" modules="iisnode" />
        </handlers>
        <rewrite>
            <!-- Redirect /api requests to the Web API application -->

            <!-- Handle Next.js routing -->
            <rule name="Next.js" 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="/index.html" />
            </rule>
            <rules>
                <!-- Do not interfere with requests for node-inspector debugging -->
                    <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
                        <match url="^server.js\/debug[\/]?" />
                    </rule>

                    <!-- First we consider whether the incoming URL matches a physical file in the /public folder -->
                    <rule name="StaticContent">
                        <action type="Rewrite" url="public{REQUEST_URI}" />
                    </rule>

                    <!-- Handle requests to the Web API -->
                    <rule name="API Proxy" stopProcessing="true">
                        <match url="^api/(.*)" />
                        <action type="Rewrite" url="http://localhost:3005/{R:1}" />
                    </rule>

                    <!-- All other URLs are mapped to the node.js site entry point -->
                    <rule name="DynamicContent">
                        <conditions>
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True" />
                        </conditions>
                        <action type="Rewrite" url="server.js" />
                    </rule>
                </rules>
            </rewrite>

            <!-- 'bin' directory has no special meaning in node.js and apps can be placed in it -->
            <security>
                <requestFiltering>
                    <hiddenSegments>
                        <add segment="node_modules" />
                    </hiddenSegments>
                </requestFiltering>
            </security>

            <!-- Make sure error responses are left untouched -->
            <httpErrors existingResponse="PassThrough" />
            <iisnode node_env="production" />

            <!-- You can control how Node is hosted within IIS using the following options:
                 * watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server
                 * node_env: will be propagated to node as NODE_ENV environment variable
                 * debuggingEnabled - controls whether the built-in debugger is enabled
                 See https://github.com/tjanczuk/iisnode/blob/master/src/samples/configuration/web.config for a full list of options
            -->
            <!--<iisnode watchedFiles="web.config;*.js"/>-->
    </system.webServer>
</configuration>

字符串
然而,每当我访问http://localhost:3005/api时,它都会显示在NextJS中找不到的页面(因此NextJS正在处理路由)。
这个怎么解决?

rqqzpn5f

rqqzpn5f1#

由于Nextjs(v13.4.x)内置了后端/api路由Map到pages/api文件夹,您需要在next.js中添加重写配置以实现服务器端渲染功能:getServerSideProps如果使用SSR。只有前端React端可以命中web.config的API代理,而不是Nextjs处理的服务器端。
您需要在next.config.js文件中添加类似于以下内容的重写:

module.exports = {
  async rewrites() {
    return [
      {
        source: '/api/:path*',
        destination: '<ASP.NET Core API URL>/:path*',
      },
    ]
  },
}

字符串

相关问题