Send subdomain to node.js

2019-03-12 10:56发布

问题:

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

回答1:

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 ?



回答2:

<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/



回答3:

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.



回答4:

You can't serve port 80 from both Apache and node.js. Having Apache as a reverse proxy wouldn't be much efficient and that's why nginx is popular in this scenario. Other alternative than nginx based reverse proxy can be as Khez suggested mapping your subdomain to different IP address which will node.js program listen to or maybe use node.js itself as a reverse proxy for Apache.



回答5:

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



回答6:

You could configure a virtual host in apache for your new site and add a permanent redirect within it to the localhost and port used by node.js.

This is how I do it on a server with several other virtual hosts and my node.js application running on port 3000:

NameVirtualHost *:80

[Other virtual hosts omitted for brevity]

...

ServerName mynewsite.com RedirectMatch (.*) http://localhost:3000$1