简体   繁体   English

NodeJS多个网站

[英]NodeJS multiple websites

I have been searching the Internet for a week now trying to find a good solution for this. 我一直在互联网上搜索一周,试图找到一个很好的解决方案。 First I read about http-master but I have been having issues installing it. 首先我读了一下http-master,但我一直遇到安装问题。 Then I saw a few others that didn't have the features and ease of configurations. 然后我看到了其他一些没有配置功能和易用性的产品。 Or the answers were all outdated 或者答案都已过时了

We want to develop website in NodeJS, currently have three, two for test/dev and one for production. 我们想在NodeJS中开发网站,目前有三个,两个用于测试/开发,一个用于生产。 Each of these are hosted on the same server, currently an Apache server with the same domain name but different ports. 其中每个都托管在同一台服务器上,目前是具有相同域名但端口不同的Apache服务器。 We have another website we are working on as well with a different domain name. 我们还有另一个网站,我们正在使用不同的域名。

Question is, what is the best way to use NodeJS to serve all the sites and more? 问题是,使用NodeJS为所有站点提供服务的最佳方式是什么? Going forward we want to develop all applications in NodeJS. 展望未来,我们希望开发NodeJS中的所有应用程序。

Server Specs: Windows Server 2012 R2 Two Cores 4 GB RAM 服务器规格:Windows Server 2012 R2两核4 GB RAM

You want to use a reverse proxy. 您想使用反向代理。 nginx is a good choice because configuration is so simple. nginx是一个不错的选择,因为配置非常简单。 But the same can also be achieved with apache. 但是使用apache也可以实现同样的目的。

Basically you just run each application under a different port, and based on domain name proxy over requests. 基本上,您只需在不同的端口下运行每个应用程序,并基于请求的域名代理。 Here's an example setup using nginx. 这是使用nginx的示例设置。

## Upstream www.example.com ##
upstream examplelive  {
      server 8081; # nodejs server running on port 8081
}

## Upstream dev.example.com ##
upstream exampledev  {
      server 8082; # nodejs server running on port 8082
}

## www.example.com ##
server {
    listen       80;
    server_name  www.example.com;

    access_log  /var/log/nginx/log/www.example.access.log  main;
    error_log  /var/log/nginx/log/www.example.error.log;

    ## send request back to examplelive ##
    location / {
     proxy_pass  http://examplelive;
     proxy_set_header        Host            $host;
     proxy_set_header        X-Real-IP       $remote_addr;
     proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
   }
}


## dev.example.com ##
server {
   listen      80;
   server_name dev.example.com;

   access_log  /var/log/nginx/log/dev.example.com.access.log  main;
   error_log   /var/log/nginx/log/dev.example.com.error.log;

   location / {
        proxy_pass  http://exampledev;
        proxy_set_header        Host            static.example.com;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

The below apache example should work. 下面的apache示例应该可行。

<VirtualHost *:80>
    ServerAdmin admin@example.com
    ServerName www.example.com

    ProxyRequests off

    <Proxy *>
            Order deny,allow
            Allow from all
    </Proxy>

    <Location />
            ProxyPass http://localhost:8081/
            ProxyPassReverse http://localhost:8081/
    </Location>

</VirtualHost>
<VirtualHost *:80>
    ServerAdmin admin@dev.example.com
    ServerName  dev.example.com    

    ProxyRequests off

    <Proxy *>
            Order deny,allow
            Allow from all
    </Proxy>

    <Location />
            ProxyPass http://localhost:8082/
            ProxyPassReverse http://localhost:8082/
    </Location>

</VirtualHost>

My preferred way of hosting multiple domains has always been to use vhost with Express, which allows you to match different domain names. 托管多个域的首选方式一直是使用带有Express的vhost ,它允许您匹配不同的域名。

var mailapp = connect()
var staticapp = connect()

// create main app
var app = connect()

// add vhost routing to main app for mail
app.use(vhost('mail.example.com', mailapp))
app.use(vhost('assets-*.example.com', staticapp))

If you don't want to go through the hassle of getting multiple domains pointing to this server, I find that using multiple sub domains on one primary name (eg foo1.bar.com, foo2.bar.com and using a CNAME record with your DNS to point to those subdomains is very effective. 如果你不想经历让多个域指向这个服务器的麻烦,我发现在一个主要名称上使用多个子域(例如foo1.bar.com, foo2.bar.com并使用带有CNAME记录的您的DNS指向那些子域名是非常有效的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM