在同一个域上托管两个Node.JS应用程序

时间:2015-05-15 00:39:14

标签: node.js node-http-proxy

我有两个节点js应用程序我在同一个盒子上运行,我希望它为所有路由运行第一个节点js app,除非url是www.domain.com/blog去另一个节点js申请。这个设置甚至可以实现,还是我必须设置子域并使用nginx或其他东西?

1 个答案:

答案 0 :(得分:3)

您可以使用nginx作为反向代理来实现此目的。

假设您的博客节点进程在端口3000上运行,而另一个节点进程在3001上运行,则简单的配置看起来像;

upstream blog {
   server 127.0.0.1:3000;
}


upstream other {
   server 127.0.0.1:3001;
}


server {
    listen 80;
    server_name www.domain.com;

    location /blog  {
        proxy_pass          http://blog;
        proxy_http_version  1.1;
        proxy_set_header    Host                $http_host;
        proxy_set_header    Upgrade             $http_upgrade;
        proxy_set_header    Connection          "Upgrade";
        proxy_set_header    X-Real-IP           $proxy_protocol_addr;
        proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto   tcp;
        proxy_set_header    X-NginX-Proxy       true;
    }

    location / {
        proxy_pass          http://other;
        proxy_http_version  1.1;
        proxy_set_header    Host                $http_host;
        proxy_set_header    Upgrade             $http_upgrade;
        proxy_set_header    Connection          "Upgrade";
        proxy_set_header    X-Real-IP           $proxy_protocol_addr;
        proxy_set_header    X-Forwarded-For     $proxy_add_x_forwarded_for;
        proxy_set_header    X-Forwarded-Proto   tcp;
        proxy_set_header    X-NginX-Proxy       true;
    }
  }