[英]How to configure nginx to redirect to same app from multiple ports?
我有一個node.js應用程序,並且正在將nginx作為反向代理服務器運行。 我想通過多種方式訪問同一應用程序。
設機器的IP地址為12.16.12.113
。 然后,我希望可以通過以下方式訪問同一應用:
12.16.12.113:3000
test.example.com
我在sites-available
目錄中有2個nginx的配置文件:
文件1:
server {
listen 80;
server_name test.example.com;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
文件2:
server {
listen 3000;
server_name default_server;
location / {
proxy_pass http://localhost:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection 'upgrade';
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
}
問題是,如果我同時鏈接了sites-enabled
目錄中的兩個文件,則只能通過12.16.12.113:3000
訪問該應用程序。 如果我刪除file2 ,則可以通過test.example.com
訪問它。
但是我希望這兩種方法都能起作用。
編輯1:根據此答案的建議,我進行了以下測試:
看來nginx沒有監聽端口80。為什么會發生這種情況?
有幾種方法可以做到這一點。
您可以使Node應用程序在12.16.12.113:3000上偵聽,而不是在localhost:3000上偵聽,然后僅需要80端口的nginx。
您可以讓Node應用程序在localhost:3000上偵聽(但只能在localhost上),然后通過nginx代理12.16.12.113:3000和端口80。
您可以使Node應用程序在任何接口上的localhost:4000或0.0.0.0:4000上偵聽,並使用nginx代理對端口80和3000的請求。
但是,您將無法使它們都在同一端口和同一接口上偵聽而不會出現問題。
考慮到您沒有包含任何有關Node app如何綁定到端口的代碼示例的事實,這是可以給出的唯一建議,這將是此處唯一的相關信息。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.