node.js - Nginx 和多个 Meteor/Nodejs 应用程序的问题

标签 node.js nginx meteor

我了解多个 node.js,我假设通过扩展 Meteor,可以使用 Nginx 在一台服务器上运行。我已经设置好 Nginx 并在 Ubuntu 服务器上运行得很好,我什至可以让它响应请求并将它们代理到我的一个应用程序。然而,当我试图让 Nginx 将流量代理到第二个应用程序时,我遇到了障碍。

一些背景:

  • 在端口 8001 上运行的第一个应用
  • 在端口 8002 上运行的第二个应用
  • Nginx 监听 80 端口
  • 试图让 nginx 将/的流量发送到应用一,并将/app2/的流量发送到应用二
  • 可以通过转到 domain:8001 和 domain:8002 访问这两个应用程序

我的 Nginx 配置:

upstream mydomain.com {
server 127.0.0.1:8001;
server 127.0.0.1:8002;
}

# the nginx server instance
server {
listen 0.0.0.0:80 default_server;
access_log /var/log/nginx/mydomain.log;

location /app2 {
  rewrite /app2/(.*) /$1 break;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_set_header X-NginX-Proxy true;
  proxy_pass http://127.0.0.1:8002;
  proxy_redirect off;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "upgrade";
}

location / {
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_set_header X-NginX-Proxy true;
  proxy_pass http://127.0.0.1:8001;
  proxy_redirect off;
  proxy_http_version 1.1;
  proxy_set_header Upgrade $http_upgrade;
  proxy_set_header Connection "upgrade";
}
}

任何有关当流量转到/app2/时可能发生的情况的见解,我将不胜感激!

最佳答案

proxy_pass http://127.0.0.1:8002/1;  <-- these should be 
proxy_pass http://**my_upstream_name**;  <--these

那么

upstream my_upstream_name {  

//Ngixn do a round robin load balance, some users will conect to / and othes to /app2

server 127.0.0.1:8001;

server 127.0.0.1:8002;

}

一些提示控制代理:

看看here @nginx 文档

那么我们开始吧:

weight = NUM​​BER - 设置服务器的权重,如果没有设置权重等于一。不平衡默认循环。

ma​​x_fails = NUM​​BER - 尝试与服务器通信失败的次数 在被考虑的时间段内(由参数fail_timeout分配) 无效。如果未设置,则尝试次数为 1。 值 0 将关闭此检查。什么被认为是失败由 proxy_next_upstream 或 fastcgi_next_upstream 定义(不计入 max_fails 的 http_404 错误除外)。

fail_timeout = TIME - 必须发生的时间 *max_fails* 与服务器通信的不成功尝试次数,这将导致服务器被视为不工作,以及服务器的时间将被视为无效(在进行另一次尝试之前)。 如果未设置时间为 10 秒。 fail_timeout 与上游响应时间无关,使用 proxy_connect_timeout 和 proxy_read_timeout 来控制。

down - 将服务器标记为永久离线,与指令 ip_hash 一起使用。

备份 -(0.6.7 或更高版本)仅在非备份服务器全部关闭或忙碌时使用此服务器(不能与指令 ip_hash 一起使用)

EXAMPLE generic

    upstream  my_upstream_name  {
      server   backend1.example.com    weight=5;
      server   127.0.0.1:8080          max_fails=3  fail_timeout=30s;
      server   unix:/tmp/backend3;
    }
//   proxy_pass http://my_upstream_name; 

这些正是您所需要的:

如果您只想控制一个应用程序的虚拟主机之间的卸载:

 upstream  my_upstream_name{
          server   127.0.0.1:8080          max_fails=3  fail_timeout=30s;
          server   127.0.0.1:8081          max_fails=3  fail_timeout=30s;
          server   127.0.0.1:8082          max_fails=3  fail_timeout=30s;
          server   127.0.0.1:8083 backup;
//  proxy_pass http://my_upstream_name; 
// amazingness no.1, the keyword "backup" means that this server should only be used when the rest are non-responsive
    } 

如果您有 2 个或更多应用:每个应用 1 个上游,例如:

upstream  my_upstream_name{
              server   127.0.0.1:8080          max_fails=3  fail_timeout=30s;
              server   127.0.0.1:8081          max_fails=3  fail_timeout=30s;
              server   127.0.0.1:8082          max_fails=3  fail_timeout=30s;
              server   127.0.0.1:8083 backup;  
            } 
upstream  my_upstream_name_app2  {
              server   127.0.0.1:8084          max_fails=3  fail_timeout=30s;
              server   127.0.0.1:8085          max_fails=3  fail_timeout=30s;
              server   127.0.0.1:8086          max_fails=3  fail_timeout=30s;
              server   127.0.0.1:8087 backup; 
            } 
upstream  my_upstream_name_app3  {
              server   127.0.0.1:8088          max_fails=3  fail_timeout=30s;
              server   127.0.0.1:8089          max_fails=3  fail_timeout=30s;
              server   127.0.0.1:8090          max_fails=3  fail_timeout=30s;
              server   127.0.0.1:8091 backup;  
            } 

希望对您有所帮助。

关于node.js - Nginx 和多个 Meteor/Nodejs 应用程序的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16283045/

相关文章:

node.js - 在 node.js 应用程序和 lua 应用程序之间共享数据

templates - 如何使用ansible循环包含模板变量的数组?

javascript - Meteor + Bootstrap - 警报已关闭。bs.alert 未触发

meteor - 既然 Meteor 0.6.0+ 支持 npm 包,Meteorite 是否仍然相关?

reactjs - 如何使用 React Router v4 History.push()

node.js - 如何在Koa中为静态文件设置base?

javascript - 发出多个并行 REST 调用并等待 Node.js 中的所有结果的推荐方法

node.js - Sails connect-redis 适配器

apache - 将域从nginx作为前端传递到 Varnish 作为后端

django - 生产 Django 应用程序根据 Debug = Value 抛出/不抛出 500 错误