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 proxysudo a2enmod proxy_httpsudo a2enmod proxy_balancersudo a2enmod proxy_balancersudo 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).
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.