将 nextjs 应用程序作为子应用程序托管到 IIS

问题描述 投票:0回答:2

我想将 nextjs 应用程序作为子应用程序托管在 IIS 网站下,按照下面的视频,我可以将 nextjs 应用程序作为 IIS 中的网站运行 https://www.youtube.com/watch?v=HLsx0iraA-Y。我需要帮助将其作为子应用程序托管。 我想将其托管为子应用程序的原因是我已经有 5-6 个 asp.net 网站作为子应用程序在父站点下运行,现在我想在同一站点下托管 nextjs 应用程序。

这里是 serverjs 和 web.config 文件

server.js 文件

const { createServer } = require('http')
const { parse } = require('url')
const next = require('next')

const dev = process.env.NODE_ENV !== 'production'
const port = process.env.PORT || 3000
const app = next({ dev })
const handle = app.getRequestHandler()

app.prepare().then(() => {
  createServer((req, res) => {
    // Be sure to pass `true` as the second argument to `url.parse`.
    // This tells it to parse the query portion of the URL.
    const parsedUrl = parse(req.url, true)
    const { pathname, query } = parsedUrl

    if (pathname === '/a') {
      app.render(req, res, '/a', query)
    } else if (pathname === '/b') {
      app.render(req, res, '/b', query)
    } else {
      handle(req, res, parsedUrl)
    }
  }).listen(port, (err) => {
    if (err) throw err
    console.log(`> Ready on http://localhost:{port}`)
  })
})

web.config 文件

<configuration>
  <system.webServer>
    <!-- Visit http://blogs.msdn.com/b/windowsazure/archive/2013/11/14/introduction-to-websockets-on-windows-azure-web-sites.aspx for more information on WebSocket support -->
    <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>
      <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>

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

非常感谢任何帮助。

node.js reactjs iis next.js webserver
2个回答
3
投票

我必须在 next.config.js 文件中添加基本路径才能使其正常工作。 现在它的工作方式与根应用程序相同。请求将在域之后有一个子应用程序

 module.exports = {
                basePath: '/subapplication',
                images: {
                    path: `subapplication/_next/image`,
                  },
                async rewrites() {
                    return [
                      {
                        source: '/subapplication/',
                        destination: '/subapplication/index'           
                      },
                    ]
                  },
            
              } ```
         
        
         

0
投票

按预期工作。谢谢分享

© www.soinside.com 2019 - 2024. All rights reserved.