很多时候网站的 robots.txt 中包含很多敏感信息,如果直接访问就会泄露这些敏感内容,我们可以在 nginx 中设置禁止用户直接访问 robots.txt 文件,但匹配并排除搜索引擎蜘蛛。

在 nginx 对应站点的配置文件中加入下面的代码即可:

location = /robots.txt {
    if ($http_user_agent !~* "spider|bot|yahoo|sogou") {
        return 404;
    }
}