2017-09-30 69 views
0

我有以下配置Nginx上服務器:如何配置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; 
} 
} 

和節點服務器上正在使用的節點明確如下監聽3000端口;

app.get('*', function (req, res) { 
    console.log(' request received ', req.headers.host); 
}); 

和我有一個域www.example.com其指向Nginx的服務器的IP,測試現在做的是:

http://www.example.com/test結果===>請求接收 www.example .COM

http://www.example.com/test/abc結果===>請求接收 www.example.com

http://www.example.com結果===>(ⅰ得到無結果!!它就像 app.get沒有觸發)

所以任何人可以幫助我解決這個問題?當我瀏覽沒有任何路徑參數的域沒有得到任何結果,所以它像Nginx沒有轉發這個請求到節點服務器!

回答

0

首先,當您使用瀏覽器或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 
+0

我已經嘗試這個測試,使用瀏覽器或捲曲時,仍然什麼也沒得到! – devMan

+0

所以你的意思是如果我使用位置/這會將www.example.com重定向到節點服務器呢?在我的情況下,請求只重定向,如果我有路徑中的東西,如ww.example.com/about! – devMan

+0

我發現什麼阻止這個請求,似乎當我使用app.use(express.static(__ dirname +'/ public'));從公共文件夾到服務器靜態文件,然後這個請求不會觸發!我試圖刪除app.use,它的工作原理,但我也需要提供靜態文件! – devMan

相關問題