26
votes

I have to put nodejs in port 80, but apache is already using it. How can I put both (nodejs and apache) on the same port 80? I need it because in my university all the ports are blocked except for PORT 80. (This is a realtime application with nodejs and socket.io (websockets) and in the other side a php application). Thanks a lot

6
You can proxy node.js traffic through Apache.Douglas
@Douglas that is slow.. Then there is no purpose of using node.js because apache will slow it down. Better do it the other way around to be efficient.Matej
Hmm, I'd not heard of using node.js for performance before. For some reason, I'd assumed that he couldn't change the Apache setup, though I see now that the question doesn't say anything like that.Douglas
Nowadays I do it like this: Nginx:80 -> proxy depending on hostname -> node/apache/? from port 8000 onwards.Matej

6 Answers

23
votes

I do this via node.js proxy..

Install http-proxy with npm or official page

Example:

var http = require('http'),
httpProxy = require('http-proxy'),
proxyServer = httpProxy.createServer ({
    hostnameOnly: true,
    router: {
        'domain.com':       '127.0.0.1:81',
        'domain.co.uk':     '127.0.0.1:82',
        '127.0.0.1':        '127.0.0.1:83'
    }
});

proxyServer.listen(80);

This creates a node process listening to port 80, and forwarding requests for domains which go to :81,82,83 etc. I recommend running this with forever and adding an entry to init.d so your proxy is up in case system shuts down.

10
votes

You can also use Apache 2's mod_proxy and mod_proxy_http, which might be more reliable or perform better depending on your system.

Here's an example:

Firstly run below command to proxy to allow

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

# Use Apache for requests to http://example.com/
# but use Node.js for requests to http://example.com/node/
<VirtualHost *:80>
    ServerName example.com
    DocumentRoot /var/www/example/
    <Location /node>
        ProxyPass http://127.0.0.1:8124/
        ProxyPassReverse http://127.0.0.1:8124/
    </Location>
</VirtualHost>

And of course you can modify the directives to your needs, such as using a different port for your virtual host (e.g., 443), different port for Node.js, or set up the proxy under a different block, such as for a subdomain (e.g., node.example.com).

9
votes

I've personally done this the other way round from @liammclennan. Some suggest that proxying through Apache defeats some of the performance and scalability advantages of Node (don't have experience myself as my server doesn't get that much traffic, but from @liammclennan's link: "Every request that comes in through Apache will cause an Apache thread to wait/block until the response is returned from your Node.js process.", which obviously doesn't mesh well with Node's architecture.)

I used node-http-proxy to set up a Node proxy server roughly as described in the first link (my Node proxy runs on port 80; Apache and my other Node services don't). Seems to be working well so far, though I have had occasional stability problems that I've 'solved' through checking the proxy's still running with a cron job (edit: it seems a lot more stable these days). The proxy's pretty lightweight, taking up about 30MB memory.

0
votes

You can't. You have to run node.js on another port and then proxy requests through apache. You can do this using mod_proxy

http://davybrion.com/blog/2012/01/hosting-a-node-js-site-through-apache/

0
votes

I usually use haproxy as the front-end in situations like that and have that proxy to the appropriate backend server. (Though making your node.js process a proxy server is a valid approach too depending on your needs).

-2
votes

I found a cool gist Run apache and nodejs on port 80. did not try it yet but will do of course

Step 1

Get a VPS that offers 2 or more IP addresses.

Step 2

From the WHM cPanel, find the menu item Service Configuration, select Apache Configuration and then click on Reserved IPs Editor.

Step 3

Tick the IP address you DON'T WANT Apache to listen to, and write it down so you can use it in the next step. Click Save.

Step 4

Install Node.js, and create a server like this:

var http = require('http');

var server = http.createServer(function(req, res) {
  res.writeHead(200);
  res.end('Hello, world!');
});

server.listen(80, '111.111.111.111');

Replacing 111.111.111.111 with the IP address you previously reserved from the WHM cPanel.

Step 5

Stop wasting your time and never listen to those telling you to use mod_rewrite to proxy Node.js again.

Update:

We can solve a problem in many different ways and IMHO, we should at least know each possible way 😉. We can do it without buying a new IP of course putting a proxy in front of both Apache and NodeJS server each running other ports except 80.