how to put nodejs and apache in the same port 80

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.


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