简体   繁体   中英

Proxy error 502 , run node js app on apache2 server

I'm trying to run a Node.js app on an Apache server listening port 80 here is my Virtualhost file :

    <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>

Here is my app.js file :

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');

And when im trying to run the app at 'localhost/node' or ip_adress/node, I have a 502 Proxy error : The proxy server received an invalid response from an upstream server. The proxy server could not handle the request GET /node.

Reason: DNS lookup failure for: localhost

Apache/2.4.7 (Ubuntu) Server at 127.0.0.1 Port 80.

Thanks.

Enable proxy in app.js :

app.enable('trust proxy');

And fix the uri for the valid route in node.js:

ProxyPass /node http://localhost:8000/node
ProxyPassReverse /node  http://localhost:8000/node

本地主机可能是ipv6地址,请尝试使用127.0.0.1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM