2016-09-20 69 views
1

這讓我瘋狂......我在路由器定義中添加了一個路由來提供純文本robots.txt文件。我不認爲我在/robots.txt路由之前有一個catchall,因爲到/ mobile和/ map的其他路由按預期工作。在我的本地機器上,/robots.txt工作正常。只有在部署到服務器時,這條路線才能工作。robots.txt沒有通過node.js顯示路由發送純文本

router.get('/', function (req, res) { 
    res.render('index'); 
}); 

router.get('/mobile', function (req, res) { 
    res.render('mobile'); 
}); 

router.get('/map', function (req, res) { 
    res.render('map'); 
}); 

router.get('/robots.txt', function (req, res) { 
    res.type('text/plain'); 
    res.send('User-agent: *'); 
}); 

這種運作良好,我的本地計算機上,但不是在部署到http://geolytix.co.uk/robots.txt

Error: Not Found 
    at /usr/share/geolytix/app.js:28:13 
    at Layer.handle [as handle_request] (/usr/share/geolytix/node_modules/express/lib/router/layer.js:95:5) 
    at trim_prefix (/usr/share/geolytix/node_modules/express/lib/router/index.js:312:13) 
    at /usr/share/geolytix/node_modules/express/lib/router/index.js:280:7 
    at Function.process_params (/usr/share/geolytix/node_modules/express/lib/router/index.js:330:12) 
    at next (/usr/share/geolytix/node_modules/express/lib/router/index.js:271:10) 
    at /usr/share/geolytix/node_modules/express/lib/router/index.js:618:15 
    at next (/usr/share/geolytix/node_modules/express/lib/router/index.js:256:14) 
    at Function.handle (/usr/share/geolytix/node_modules/express/lib/router/index.js:176:3) 
    at router (/usr/share/geolytix/node_modules/express/lib/router/index.js:46:12) 

我最初的猜測是,這事做與代理從nginx的我的節點服務器,但我可以訪問該網站直接轉到IP地址。然而,robots.txt路線失敗。

http://139.59.161.58:3000/robots.txt

編輯:

這是我app.js enter image description here

+0

您確定在添加該路由後重新啓動了您的節點服務器嗎?嘗試從Express直接(在您的服務器上)請求'/ robots.txt',而不是通過nginx。 – robertklep

+0

我想這是靜態文件攔截問題。你可以在'server'中顯示nginx配置,特別是'location'部分嗎? – Nevertheless

+0

這不是nginx設置。我可以從IP地址打開網站,但robots.txt未打開。 http://139.59.161.58:3000/robots.txt –

回答

2

在PM2中15次重啓失敗後,應用程序崩潰,緩存版本仍在服務中。 robots.txt路由不在緩存中。我很抱歉浪費你的時間。至少我現在更好地瞭解如何使用PM2。

D

0

這是因爲nginx的配置設置爲截取靜態文件的請求線28。

這樣一些部分:

http { 
    ... 
    server { 
     ... 
     location ~ ^/(images/|img/|javascript/|js/|css/|stylesheets/|flash/|media/|static/|robots.txt|humans.txt|favicon.ico) { 
      root /usr/local/.../public; 
      access_log off; 
      expires max; 
     } 
     ... 
    } 
} 

正在處理robots.txt甚至在它到達Express.js路由中間件服務。

您應該從nginx配置中排除文件以允許路由處理它。

+0

這不是我害怕的nginx配置。如果我直接轉到IP地址,我仍然無法獲取robots.txt。 –

+0

也沒有意義,因爲錯誤消息是Express的,所以請求被代理。 – robertklep

+0

我覺得這是唯一可能導致此問題的原因 - 服務器如何處理此路徑。代碼非常好(它在localhost上工作)。無論如何,你想說如果你通過IP訪問網站,nginx不再處理請求了嗎?這取決於你如何配置它。 – Nevertheless