子域的节点应用程序结构

时间:2016-12-20 08:20:23

标签: node.js

我正在寻找使用节点构建网站,但我的想法最好是使用子域名。我想知道的是,我是否需要将每个子域作为单独的应用程序运行并使用nginx指向它们?

当前结构 - 客户 - 资产 --- js --- css --- img - 服务器 - 应用程序 ---家 --- subapp1 --- subapp2 - node_modules - server.js - config.js

上述结构是每个应用程序都会自行运行的想法,但是它们具有应该共享的共同视图。 Server.js将全部运行它们。我只需要知道这是否是正确的结构,是否有更清洁或更好的选择。感谢

1 个答案:

答案 0 :(得分:0)

我建议设置Nginx以将代理转换为2个NodeJS应用程序。例如在端口8081上运行1个应用程序(service1),在8082(service2)上运行另一个应用程序,如下所示:

upstream service1 {
  ip_hash;
  server localhost:8081;
}

upstream service2 {
  ip_hash;
  server localhost:8082;
}

server {
  listen          0.0.0.0:80;
  server_name     service1.mydomain.com;

  location / {
    index index.html index.htm;
    proxy_pass http://service1;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}

server {
  listen          0.0.0.0:80;
  server_name     service2.mydomain.com;

  location / {
    index index.html index.htm;
    proxy_pass http://service2;
    proxy_redirect off;
    proxy_set_header Host $host;
    proxy_set_header X-Real-IP $remote_addr;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
}