如何在cpanel中的https上运行next js 13应用程序

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

我想在 cpanel 中的 https 上运行我的 nextjs 应用程序,但它不起作用! 但是http没有问题,可以运行。 此外,SSL 已安装在 cPanel 中的域上并且有效。

这是 server.js,cpanel 中 Node js 应用程序的 stratup 文件:

const https = require("https");
const { parse } = require("url");
const next = require("next");

const dev = process.env.NODE_ENV !== "production";
const hostname = "localhost";
const port = 3000;

// when using middleware `hostname` and `port` must be provided below
const nextApp = next({
  dev
});
const nextHandler = nextApp.getRequestHandler();

nextApp.prepare().then(async () => {
  // Be sure to pass `true` as the second argument to `url.parse`.
  // This tells it to parse the query portion of the URL.
  //const app = express();
  //const server= https.createServer(app);

  const server = https.createServer(async (req, res) => {
    // res.send("Hello World");

    const parsedUrl = parse(req.url, true);
    const { pathname, query } = parsedUrl;

    if (pathname === "/a") {
      await nextApp.render(req, res, "/a", query);
    } else if (pathname === "/b") {
      await nextApp.render(req, res, "/b", query);
    } else {
      await nextHandler(req, res, parsedUrl);
    }
  });

  //app.get('/hello', async (_, res) => {
  //    res.send('Hello World')
  // });

  server.listen(port, (err) => {
    //res.send('Hello World');
    console.log(`> Ready on http://localhost:${port}`);
    console.log(server.address());
  });
});

`

并且运行一段时间后,出现以下错误:

请求超时

node.js next.js cpanel next.js13
1个回答
0
投票

我找到了问题所在。

500错误仅发生在cpanel中的HTTPS模式下,发生在next-intl库中。

我能够通过使用重定向解决问题:

在文件 middleware.ts 中

const locales = ['default','en', 'es', 'fr'];
const defaultLocaleReal = 'en';
const defaultLocaleFake = 'default';

const intlMiddleware = createMiddleware({
    locales,
    localeDetection: false,
    defaultLocale: defaultLocaleFake,    
});

const PUBLIC_FILE = /\.(.*)$/;

export default function middleware(req: NextRequest) {
    const { pathname } = req.nextUrl.clone()

    const pathnameIsMissingLocale = locales.every(
        (locale) => !PUBLIC_FILE.test(pathname) &&
            !pathname.includes('/api/') && !pathname.startsWith(`/${locale}/`) && pathname !== `/${locale}`
    )
    const cookie = req.cookies.get('NEXT_LOCALE')?.value;

    if (pathnameIsMissingLocale) {
        const locale = cookie ? ((cookie == defaultLocaleFake) ? defaultLocaleReal : cookie) : defaultLocaleFake;
        const url = `https://example.com/${locale}${pathname}${req.nextUrl.search ? `${req.nextUrl.search}`:''}`;
        return NextResponse.redirect(
            new URL(url)
        )
    }  
   
    
return intlMiddleware(req);
   
}
© www.soinside.com 2019 - 2024. All rights reserved.