gpt4 book ai didi

node.js - Node 转发路径请求到另一台服务器

转载 作者:搜寻专家 更新时间:2023-10-31 22:38:26 24 4
gpt4 key购买 nike

目前有一个运行 Node 服务器的网站,该 Node 服务器处理对 example.com 的所有请求,我在一个单独的服务器(运行 apache)上创建了一个完全独立的 wordpress 博客,我希望它在路径上提供服务例如 example.com/blog172.23.23.23 IP 地址。 wordpress 服务器不共享任何代码,甚至不知道除它自己之外的任何东西的存在

node/express 以这种方式转发所有请求的最佳方式是什么?另外,新 wordpress 服务器的 A/CNAME 记录应该指向什么?

最佳答案

这有效:这是您需要放入 Node 应用程序的内容:

var express = require('express');
var app = module.exports = express();

var proxy = require('http-proxy').createProxyServer({
host: 'http://your_blog.com',
// port: 80
});
app.use('/blog', function(req, res, next) {
proxy.web(req, res, {
target: 'http://your_blog.com'
}, next);
});

app.listen(80, function(){
console.log('Listening!');
});

然后在您的 wordpress 应用中,您需要将站点 URL 设置为 http://your_node_app.com/blog

注意:您可能已经有一个可能带有 body-parser 的 Express 应用程序中间件 causes errorsgenerally doesn't play well with POST requests in http-proxy

您会在这些线程中找到几个解决方案,即 1:将此代理放在 主体解析器之前,或 2:使用 connect-restreamer

关于node.js - Node 转发路径请求到另一台服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30800829/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com