How To Use Apache With Node.js - Reverse Proxy

Words
113
Reading
1 min
Listen
Play
9y

Just playing around with Node.js on my ubuntu VM server, created a test app :

#!/usr/bin/env nodejs
var http = require('http');
http.createServer(function (req, res) {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World\n');
}).listen(8080, 'localhost');
console.log('Server running at http://localhost:8080/');

Now outside the VM there is a real problem to access it.
There is a need in nginx or apache.

Because I have already apache2, I found a way to configure it to allow me access the node.js app from outisde

Step1: Install dependencies

sudo a2enmod proxy
sudo a2enmod proxy_http
sudo a2enmod proxy_balancer
sudo a2enmod lbmethod_byrequests

Step2: change apache2.conf

Add at the end

ProxyPass "/node" "http://localhost:8080/"
ProxyPassReverse "/node" "http://localhost:8080/"

Step3: apache restart

sudo systemctl restart apache2

Step4: test