我正在尝试在 Apache 服务器监听端口 80 上运行 Node.js 应用程序 这是我的虚拟主机文件:
<VirtualHost *:80>
ServerName mikus
ServerAdmin webmaster@localhost
DocumentRoot /var/www/html
ProxyPreserveHost On
ProxyPass /node http://localhost:8000/
ProxyPassReverse /node http://localhost:8000/
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined </VirtualHost>
这是我的 app.js 文件:
var express = require('express');
var app = express();
app.get('/node', function(req, res) {
res.setHeader('Content-Type', 'text/plain');
res.end('Welcome apache');
});
app.listen(8000, 'localhost');
当我尝试在“localhost/node”或 ip_adress/node 运行应用程序时,出现 502 代理错误:代理服务器从上游服务器收到无效响应。 代理服务器无法处理请求 GET/node。
原因:DNS 查找失败:本地主机
Apache/2.4.7 (Ubuntu) 服务器位于 127.0.0.1 端口 80。
谢谢。
最佳答案
在app.js
中启用代理:
app.enable('trust proxy');
并修复node.js中有效路由的uri:
ProxyPass /node http://localhost:8000/node
ProxyPassReverse /node http://localhost:8000/node
关于javascript - 代理错误502,在apache2服务器上运行node js应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39516224/