robots.txt 未显示通过 node.js 路由发送纯文本

robots.txt not showing via node.js routes send plain text

这让我抓狂...我在路由器定义中添加了一个路由来提供纯文本 robots.txt 文件。我不认为我在 /robots.txt 路线之前有一个包罗万象的路线,因为到 /mobile 和 /map 的其他路线按预期工作。在我的本地机器上 /robots.txt 工作正常。只有部署到服务器上,这条路由才生效。

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

编辑:

这是我的第 28 行 app.js

这是因为nginx配置设置为拦截静态文件请求.

像这样的部分:

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 配置中排除文件以允许路由处理它。

应用程序在 PM2 中 15 次失败重启后崩溃,缓存版本仍在提供。 robots.txt 路由不在缓存中。很抱歉浪费了你的时间。至少我现在对如何使用 PM2 有了更好的了解。

D