how do I route multiple domains to multiple node a

2019-03-05 11:32发布

问题:

I'm used to the typical lamp web hosting environment where you just click a few buttons in cpanel and your domain gets zoned and mapped to a folder within htdocs. I've been using node.js a lot and it doesn't seem as simple to do the same thing. If I had multiple node apps and I wanted to route domain1.com:80 and domain2.com:80 each to its own node app and port, how do I go about doing that? where do I start?

回答1:

This is typically done with nginx. Nginx is a reverse proxy, a piece of software you put infront node.js.

server {
    listen      80;
    server_name www.domain1.com;
    root /var/www/domain1;

    location / {
        proxy_pass http://localhost:1337; # this is where your node.js app_domain1 is listening
    }
}

server {
    listen       80;
    server_name www.domain2.com;
    root /var/www/domain2;

    location / {
        proxy_pass http://localhost:1338; # this is where your node.js app_domain2 is listening
    }
}

From here: Nginx Different Domains on Same IP



回答2:

I dont recomend apache to do these, nginx fits better with nodejs.

You can run the apps for example at the port 3000 and 3001,

then proxy it to mydomain1:80, and mydomain2:80.

To get mydomain1 and mydomain2 unther the port 80, these is all about DNS not apache.

Theres no way to run apache/nginx and your node httpserver on the same port. ull get a error.

p.s. Im not sure in u can do these @tipical lamp webhost

hope it helps



回答3:

You can setup virtual domains in Node if you're using Express.

The code you would use to start your server with would look something like this.

var sys = require('sys'),
  express = require('express');

var app = express.createServer();

app.configure(function() {
  app.use(express.vhost('subdomain1.local', require('./subdomain1/app').app));
  app.use(express.vhost('subdomain2.local', require('./subdomain2/app').app));
  app.listen(3000);
});

Then you would export app in each subdomain.

var app = express.createServer();
exports.app = app;

Here's a post to read more about vhost in Express.js.