[英]How to configure nginx server to forward request to a node server with empty path?
我有一個具有以下配置的Nginx服務器:
server
{
listen 80;
server_name example.ca www.example.ca;
location / {
proxy_pass http://127.0.0.1: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;
}
}
在節點服務器上,使用節點Express監聽端口3000,如下所示:
app.get('*', function (req, res) {
console.log(' request received ', req.headers.host);
});
我有一個域名www.example.com,它指向Nginx服務器IP,測試正在做的是:
http://www.example.com/測試結果===>收到請求www.example.com
http://www.example.com/test/abc結果===>收到請求www.example.com
http://www.example.com結果===>(我沒有結果!!就像沒有觸發app.get一樣)
那么在這個問題上有人可以幫助我嗎? 當我瀏覽沒有任何路徑參數的域時,沒有得到任何結果,因此它就像Nginx一樣沒有將這個請求轉發給節點服務器!
首先,當您使用瀏覽器或curl瀏覽abc.com
時,它將始終以/
發送路徑。 無法發送空白路徑
接下來,您的nginx配置是一個socket.io實現。 應該在下面
server
{
listen 80;
server_name example.ca www.example.ca;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Host $host;
}
}
接下來,您不會結束您的請求,他們只會超時。 將您的代碼更改為以下
app.get('*', function (req, res) {
console.log(' request received ', req.headers.host);
res.status(200).send("all ok");
});
另外,如果您遇到問題,也可以使用curl進行測試,如下所示
curl -v http://www.example.com
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.