Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send subdomain to node.js

My work runs a couple different internal web apps on an ubuntu server (10.10) running apache. I'm currently developing another web app, and am seriously considering developing on top of a custom-built node.js web server. My reasoning for wanting to do this is:

  1. Speed/Scalability
  2. Security - Pages will be served with a switch...case, instead of just serving the (potentially malicious) user whatever they ask for.
  3. Ease of setup - my intentions are for this to be an open-source project, and node.js is much easier for users to set up, rather than dealing with apache/IIS/etc.

My question is, on a server where I've got apache listening to port 80, how can I pass off a certain subdomains to node.js. I've seen a couple articles about using apache virtual hosts to pass it off, but that seems to defeat the purpose of using node.js. If I have to go through apache, then all three of my reasons for avoiding apache/IIS have voided themselves.

I know I could use a different port (:8080?), but from an end-user standpoint, it's pretty confusing having to put in custom ports. Any alternative ideas?

Thanks

like image 559
jwegner Avatar asked Apr 09 '11 16:04

jwegner


4 Answers

<VirtualHost *:80>
ServerName subdomain.yourdomain.com
ProxyPreserveHost on
ProxyPass / http://localhost:8080/
</VirtualHost>

Thanks to http://www.chrisshiplet.com/2013/how-to-use-node-js-with-apache-on-port-80/

like image 166
piotr Avatar answered Nov 18 '22 09:11

piotr


if socket.io node is running, be sure to enable also few apache mods:

  1. a2enmod proxy
  2. a2enmod proxy_balancer
  3. a2enmod proxy_express
  4. a2enmod proxy_http

in file /etc/apache2/sites-available/chat.example.com.conf


<VirtualHost *:80>
    ServerName chat.example.com

    <Location "/">
        ProxyPreserveHost On
        ProxyPass http://localhost:3000/
        ProxyPassReverse http://localhost:3000/
    </Location>
</VirtualHost>

then of course service apache2 reload

like image 28
animaacija Avatar answered Nov 18 '22 09:11

animaacija


How about doing things the other way round : bind node to port 80, handle the traffic targeted at the subdomain and use it as a reverse proxy to apache for everything else ?

like image 4
Adrien Avatar answered Nov 18 '22 07:11

Adrien


Let me start from the ground up:

You have a DNS. And a dns server maps one DNS to one IP!

You then have apache running on your computer that listens for connections on port 80 for http:// and on port 443 for https://. http://example/ is actually a request on http://example:80/.

You can't use node.js to listen on the same machine on the same port as apache. That's why using port 8080 is viable.

You can also map the subdomain to a different IP. The only caveat here is that you need to have a public IP Address.

like image 4
Khez Avatar answered Nov 18 '22 09:11

Khez