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

如何解决cPanel中Next.js 13 HTTPS应用请求超时问题?

解决cPanel中Next.js HTTPS版本请求超时问题

问题背景

在cPanel中部署Next.js应用时,HTTP版本运行正常,但HTTPS版本无法访问,等待后出现以下错误:

Request Timeout

域名已安装有效的SSL证书,当前使用的启动文件server.js代码如下:

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());
  });
});

问题原因

cPanel的Node.js部署采用反向代理架构:cPanel自身已完成SSL证书的终止处理,会将外部的HTTPS请求转发到Node应用监听的HTTP端口上。你直接用https.createServer创建HTTPS服务器,会导致端口监听冲突,请求无法被正确转发,最终触发超时。

解决方案

1. 修改启动文件为HTTP模式

将代码中的https模块替换为http模块,让应用以HTTP模式监听端口,由cPanel处理HTTPS相关逻辑:

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

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

const nextApp = next({
  dev
});
const nextHandler = nextApp.getRequestHandler();

nextApp.prepare().then(async () => {
  const server = http.createServer(async (req, res) => {
    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);
    }
  });

  server.listen(port, (err) => {
    if (err) throw err;
    console.log(`> Ready on http://localhost:${port}`);
  });
});

2. 配置强制HTTPS跳转(可选)

如果需要将所有HTTP请求自动跳转到HTTPS,可在网站根目录的.htaccess文件中添加以下规则:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

3. 验证cPanel配置

确保cPanel的Node.js应用配置中,设置的端口与代码中的port(此处为3000)完全一致,且应用已重新启动。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.11 21:12:49