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

无需修改DNS,将Node.js应用部署为现有网站子目录的方法

将Node.js/Express应用部署为Apache现有网站的子目录

需求与环境

  • 项目资源有限、限制较多,需将Node.js/Express应用部署为现有网站的子目录,访问路径为https://example.com/example,同时保留主站https://example.com的原有配置
  • 服务器为CentOS 7,已配置Apache
  • 无法修改DNS,不确定开启新端口对SSL证书的影响,希望避免相关风险
  • 曾尝试Python部署但遇到服务器问题,也研究过开放端口、基于名称的托管和虚拟主机方案

现有Apache虚拟主机配置(/etc/httpd/conf.d目录下)

<VirtualHost *:80>
        ServerName example.com
        #Redirect / https://example.com/
RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

服务器Hosts文件配置

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

编写的Express脚本(Node.js新手)

import express from 'express';
import logger from 'morgan';

const port = process.env.PORT || 3000;

const app = express();
app.use(logger('dev'));

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

app.listen(port, () => {
    console.log(`Server is running on port ${port}`);
});

正在尝试的反向代理配置

<VirtualHost *:80>
        ServerName example.com
        #Redirect / https://example.com/

        <Location /example>
              ProxyPass http://localhost:3000
              ProxyPassReverse http://localhost:3000
        </Location>

RewriteEngine on
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 02:20:06