我在Google云平台上创建了计算引擎实例并安装了CentOS 7,apache和nodejs。我在服务器上设置了反向代理,这样每当http://[external_ip]或domain_name / api /命中浏览器时它就会命中nodejs服务器。以下是我的反向代理配置
/etc/httpd/conf.d/default-site.com
ProxyPreserveHost On
ProxyPass /api/ http://127.0.0.1:8080/
ProxyPassReverse /api/ http://127.0.0.1:8080/
以上配置工作正常。以下是我的目录结构:
var / www / html / domain_name / public_html / index.html - >当我们直接在浏览器上点击域名时,它将执行此文件
var / www / html / domain_name / public / html / api / - >这是我的nodejs应用程序
我已经安装了hapi js框架。我在/ api /目录下创建了以下server.js文件。
'use strict';
const Hapi = require('hapi');
// Create a server with a host and port
const server = new Hapi.Server();
server.connection({
host: '127.0.0.1',
port: 8080
});
server.route({
method: 'GET',
path:'/',
handler: function (request, reply) {
return reply('hello world');
}
});
// Add the route
server.route({
method: 'GET',
path:'/hello',
handler: function (request, reply) {
return reply('hello world');
}
});
// Start the server
server.start((err) => {
if (err) {
throw err;
}
console.log('Server running at:', server.info.uri);
});
我在下面创建了两个端点: 1. /(这条路线在我访问http:/// api /时有效 2. / hello(当我访问http:/// api / hello /
时,此路由无效当我们使用带有apache和nodejs的反向代理时,是否还需要其他配置?
答案 0 :(得分:1)
我已经想通了。问题是使用apache进行反向代理配置。我做了以下更改(删除了斜杠/来自ProxyPass和ProxyPassReverse的文件夹)
ProxyPreserveHost On
ProxyPass /api http://127.0.0.1:8080/
ProxyPassReverse /api http://127.0.0.1:8080/