28

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

user1203334
  • 283
  • 1
  • 3
  • 5
  • You can proxy node.js traffic [through Apache](http://arguments.callee.info/2010/04/20/running-apache-and-node-js-together/). – Douglas Jun 23 '12 at 19:39
  • 1
    @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 Jul 21 '12 at 17:35
  • 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 Jul 22 '12 at 00:53
  • Nowadays I do it like this: Nginx:80 -> proxy depending on hostname -> node/apache/? from port 8000 onwards. – Matej Sep 18 '13 at 17:48

7 Answers7

23

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.

Matej
  • 9,548
  • 8
  • 49
  • 66
  • 2
    This reply is old, but the best one I found. `http-proxy` works in completely different ways now. But the results are same — and amazing. I have Apache running on 8000 and the node.js proxy script on 80 and added to init.d and it works like a charm. – Zia Ur Rehman Jul 23 '14 at 07:58
  • 1
    @ZiaUrRehman I found nginx to be a better solution, because its more stable, starts on boot and is probably faster.. Both work however ;). If you want performance checkout HAProxy/Varnish – Matej Jul 23 '14 at 09:43
  • I tried nginx but couldn't get it to work as intended. It always pointed to the default nginx html. Unfortunately I'm not familiar with nginx at all. If nginx is what I should be using, then that's what I should be using. However, can you guide me to some sort of easy step-by-step tutorial that accomplishes exactly that? – Zia Ur Rehman Jul 23 '14 at 20:27
  • @ZiaUrRehman Check out https://www.digitalocean.com/community/tutorials/how-to-set-up-nginx-virtual-hosts-server-blocks-on-ubuntu-12-04-lts--3 Step 5.. – Matej Jul 23 '14 at 20:30
  • @ZiaUrRehman you probably didn't reload the configuration (e.g. `/etc/init.d/nginx reload`) – Matej Jul 23 '14 at 20:34
  • That was the first one I read. And I took care to make links in sites-enabled and then reload the config and even restart the server - all to no avail. Also, I followed [this](https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-as-a-front-end-proxy-for-apache) one, same site, more specific. Doesn't seem to work. – Zia Ur Rehman Jul 23 '14 at 20:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/57874/discussion-between-matejkramny-and-zia-ur-rehman). – Matej Jul 23 '14 at 20:38
  • But you are still trying to listen on port 80, which is the original problem – Nikolay Dyankov Nov 02 '15 at 19:51
  • Yes but i'm creating a vhost proxy – Matej Nov 02 '15 at 23:09
11

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

Harat
  • 1,340
  • 1
  • 17
  • 20
Synexis
  • 1,255
  • 14
  • 14
9

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.

meloncholy
  • 2,122
  • 18
  • 16
  • More popular, stable is to use NGinx. Even the (original but stopped developing) creator Ryan Dahl proposed this because node.js is still pretty young project. – Alfred Jun 25 '12 at 14:43
  • 1
    @Alfred Fair enough. I understand that nginx is non-blocking, so wouldn't suffer the same issues there as Apache, and it certainly has a good reputation for speed. For me, Node seems like a good solution as it's pretty easy on memory and the software's already installed, but for bigger & busier sites it's probably the way to go right now. – meloncholy Jun 25 '12 at 15:35
  • It sounds like an apache proxy would be ok for low traffic sites or for development, and when performance is needed later on you would go for a dedicated pure node.js server. – snez Jan 09 '14 at 01:20
  • 1
    @snez Sure, it should be fine, though in my experience just running a Node server (on a different port) is extremely easy for dev. That said, I have Node everywhere and don't use Apache much at all these days. – meloncholy Jan 09 '14 at 10:13
  • @meloncholy yes if you are getting paid to work exclusively on node projects :). Another solution for development is to run both apache and node on separate ports and proxy requests with pow (http://pow.cx) which is very easy. And with node on production I'd probably configure node cluster (http://rowanmanning.com/posts/node-cluster-and-express/). – snez Jan 10 '14 at 10:48
  • There's a tutorial on how to use NodeJS along with apache. https://www.devopinion.com/run-apache-and-nodejs-on-the-same-port/ – Amarjit Singh Jul 25 '21 at 21:10
0

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/

liammclennan
  • 5,295
  • 3
  • 34
  • 30
  • This seems to be a dead link. A copy is at https://github.com/davybrion/companysite-dotnet/blob/master/content/blog/2012-01-hosting-a-node-js-site-through-apache.md – Steven Green Jun 26 '17 at 22:31
0

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

Ask Bjørn Hansen
  • 6,784
  • 2
  • 26
  • 40
0

for httpd.conf

activiate the module , proxy_module and proxy_http

if you are using virtual host

    <virtualhost ...>
    ServerName api.domain.com
    ........

ProxyPreserveHost On
    
    ProxyPass / http://localhost:8080/
    
    ProxyPassReverse / http://localhost:8080/
    
    
    </virtualhost>

assume you are running nodejs server at 8080 , you don't need to take care ssl in nodejs , all should be done in apache

then try https://api.domain.com/

chings228
  • 1,859
  • 24
  • 24
-2

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.

Nur Rony
  • 7,823
  • 7
  • 38
  • 45
  • 1
    It needs additional IP which comes at extra cost in many machines. Questioner mentioned that he is doing a university project. That means most likely he need a solution to run both node and apache on same ip address. – Ali Azhar May 27 '17 at 17:40
  • I back @AliAzhar 's opinion. I cannot simply buy another IP for such a project. Alternative case: Running both on my local machine as test environment. Where do I get another IP if I just have one NIC? – DBX12 Nov 20 '17 at 07:33