簡體   English   中英

如何使用nginx proxy_pass和Node Express獲得多個端口?

[英]How do you get multiple ports working with nginx proxy_pass and Node Express?

我有多個節點實例在不同的端口上運行。 (8000、8001等)我也在端口80上運行了nginx。我試圖做的是允許用戶通過單個域名進入我的網站,然后通過不同的URL能夠訪問其他節點服務器。 。

目前,一切工作如下

http://example.com:8000/index.html (這是一個運行站點的節點服務器) http://example.com:8001/index.hmtl (這是另一個運行在不同站點的節點服務器)

我想做的是允許用戶鍵入url路徑並將其自動定向到正確的站點,而無需他們指定端口。 因此,例如:

http://example.com/site1/inedx.html (這是一個在端口8000上運行站點的節點服務器) http://example.com/site2/index.html (這是在以下位置上運行另一個站點的另一節點服務器)端口8001)

我一直在嘗試使用nginx proxy_pass規則來實現這一點,但是在使其工作方面遇到了麻煩。 本質上,nginx重新路由到了正確的Express服務器,但是由於缺少端口,我在Express應用程序中的所有路由都中斷了。

因此,當我轉到http://example.com/site1/index.html時,它將帶我到http://example.com/index.html 任何幫助將不勝感激。 我已經粘貼了nginx.conf的相關部分以及下面的示例快遞路線。

nginx.conf

server {
            listen 80;
            server_name 11.11.11.111;
            root /home/ubuntu;

            location /site1/ {
                    proxy_pass http://11.11.11.111:8000/;
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    proxy_set_header Host $proxy_host;
                    proxy_set_header X-NginX-Proxy true;
                    proxy_pass http://11.11.11.111:8000/;
                    proxy_redirect http://11.11.11.111/* http://11.11.11.111:8000/*;
            }
            location /site2/ {
                    proxy_set_header X-Real-IP $remote_addr;
                    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                    proxy_set_header Host $proxy_host;
                    proxy_set_header X-NginX-Proxy true;
                    proxy_pass http://11.11.11.111:8001/;
                    proxy_redirect http://11.11.11.111/* http://11.11.11.111:8001/*;
            }
}

app.js

app.get('/', function(req, res){
    res.redirect('/index.html');
});

根據我對nginxproxy_pass的有限經驗,我發現當您使用proxy_pass時,nginx並沒有真正將請求移交給代理應用程序。 使用服務器塊中指定的確切server_name對精確位置塊路徑的任何請求都將正確傳遞,即:

http://11.11.11.11/site1/
http://11.11.11.11/site1/index.html

但是以下請求將失敗:

http://11.11.11.11/site1
http://example.com/site1
http://example.com/site1/
http://example.com/site1/index.html

因為它們與位置阻止路徑不完全匹配。 另外,除非另有說明,否則該路徑下的任何子目錄都將由nginx提供服務。 例如:

http://11.11.11.11/site1/ <-- Succeeds
http://11.11.11.11/site1/js <-- Fails

您必須對nginx明確。 您需要指定應用程序駐留在服務器上的路徑,使用類似正則表達式的位置路徑規范,並使用重寫,因此nginx知道在代理路徑之后不處理任何內容:

server {
    listen 443 ssl;

    server_name myserver;
    root /var/html/node; # The parent directory of where your apps live

    location ^~ /app1/ {
        rewrite ^/app1(.*) /real/app1/path$1;
        proxy_pass https://127.0.0.1:8000;
        proxy_redirect https://myserver/app1 /real/app1/path;
    }

    location ^~ /app2/ {
        rewrite ^/app2(.*) /real/app2/path$1;
        proxy_pass https://127.0.0.1:8001;
        proxy_redirect https://myserver/app2 /real/app2/path;
    }
}

/ real / app [x] / path應該是/ var / html / node下的真實目錄,並且應該由運行nginx的用戶訪問。 您的配置可能需要進行一些調整才能工作,但這應該能為您提供大部分幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM