2013-04-29 49 views
10

我知道多個node.js和我通過擴展Meteor假設可以使用Nginx在一臺服務器上運行。我已經安裝了Nginx並在Ubuntu服務器上運行,我甚至可以讓它響應請求並將它們代理到我的一個應用程序。然而,當我試圖讓Nginx代理第二個應用程序的流量時,我遇到了障礙。Nginx和多個Meteor/Nodejs應用程序的問題

一些背景資料:

  • 1應用在端口8001
  • 第二個應用程序運行在端口8002
  • Nginx的偵聽端口80
  • 試圖獲取nginx的運行在流量發送/到App one and traffic at/app2/to app two
  • 這兩個應用程序都可以通過轉到域達到:8001和域: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"; 
} 
} 

任何有識之士,以什麼可能會在交通進入/ APP 2 /我會非常感激上走!

回答

26
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文檔

那麼在這裏,我們去:

重量= NUM​​BER - 設置服務器的權重,如果沒有設置權重等於1。失衡默認循環。

max_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; 

壽這就是你需要:

如果u只是想控制虛擬主機之間德負載爲一個應用程序:

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 
    } 

如果u有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; 
      } 

希望它有幫助。

+2

這項權利這裏是一個sweeeeet答案!有一個upvote :) – 2013-04-29 22:47:06

+0

這是一個了不起的答案,謝謝。我唯一的問題是,我不想嘗試循環賽(你部分解決了這個問題)。我會添加正確的上游條目,但我很好奇...是我的位置條目好嗎? – jak119 2013-04-30 15:10:33

+0

我建議下一步:如果app1是核心應用,app2是'app1的孩子'。你的位置是好的。你也可以將它們放在/ app1和/ app2上。 Boll – jmingov 2013-04-30 15:39:44

0

尋找Nginx替代品的用戶:爲每個Meteor應用程序安裝Cluster軟件包,該軟件包將自動處理負載平衡。 https://github.com/meteorhacks/cluster

如何對其進行設置:

# You can use your existing MONGO_URL for this 
export CLUSTER_DISCOVERY_URL=mongodb://host:port/db, 
# this is the direct URL to your server (it could be a private URL) 
export CLUSTER_ENDPOINT_URL=http://ipaddress 
# mark your server as a web service (you can set any name for this) 
export CLUSTER_SERVICE=web 

示例設置:

{ 
    "ip-1": { 
    "endpointUrl": "http://ip-1", 
    "balancerUrl": "https://one.bulletproofmeteor.com" 
    }, 
    "ip-2": { 
    "endpointUrl": "http://ip-2", 
    "balancerUrl": "https://two.bulletproofmeteor.com" 
    }, 
    "ip-3": { 
    "endpointUrl": "http://ip-3", 
    "balancerUrl": "https://three.bulletproofmeteor.com" 
    }, 
    "ip-4": { 
    "endpointUrl": "http://ip-4" 
    } 
} 
相關問題