在 IIS 上部署 Next.js

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

我一直在尝试在 IIS 上部署 next.js 项目。我做了一些测试,还创建了一些较小的 nextjs 项目来测试不同的策略。我对此完全陌生,但这是一个要求,所以我正在努力使其发挥作用。

到目前为止我所做的:

我已经使用下一个构建构建了项目,然后将 .next 文件夹、node_modules 文件夹以及我在中等指南上在线找到的 web.config 和 server.js 复制到一个新文件夹中。我还安装了模块

iisnode
URLRewrite
。然后,我在“站点”下的“默认网站”下添加了一个应用程序,该应用程序位于 IIS 中的应用程序池下。应用程序的物理路径是包含前面列出的文件的构建文件夹。我已确保用户拥有应用程序池和应用程序的所有权限。

当我启动应用程序时,我会看到 (nextjs)

404 | This page could not be found.
屏幕。路线是
http://localhost/iis-test-project
。我还尝试将
next.config.js
设置为
output: "export"
并复制输出文件而不是
.next file

除了

localhost/iis-test-project
上的 404 之外,我还在 .next 文件夹中的其他内容上看到了一些 404,例如
 http://localhost/_next/static/chunks/main-app-9fe887c4b47d5643.js

Web.config:

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="myapp">
          <match url="/*" />
          <action type="Rewrite" url="server.js" />
        </rule>
      </rules>
    </rewrite>

    <iisnode node_env="production" nodeProcessCommandLine="&quot;C:\Program Files\nodejs\node.exe&quot;" interceptor="&quot;%programfiles%\iisnode\interceptor.js&quot;" />

  </system.webServer>
    <location path="" overrideMode="Deny">
        <system.webServer>
    <handlers>
      <add name="iisnode" path="server.js" verb="*" modules="iisnode" />
    </handlers>
        </system.webServer>
    </location>
</configuration>

服务器.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) => {
        handle(req, res, parsedUrl)
    }).listen(port, (err) => {
        if (err) throw err
        console.log(`> Ready on http://localhost:${port}`)
    })
})
iis next.js deployment
1个回答
0
投票

你可以在 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}`)
  })
})

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