【问题标题】:Nginx: different robots.txt for alternate domainNginx:备用域的不同 robots.txt
【发布时间】:2014-12-06 04:13:01
【问题描述】:

总结

我有一个带有内部和外部域指向它的 Web 应用程序,我想要一个 robots.txt 来阻止对内部域的所有访问,但允许对外部域的所有访问。

问题详情

我有一个简单的 Nginx 服务器块,用于代理到 Django 应用程序(见下文)。如您所见,此服务器块响应任何域(由于缺少 server_name 参数)。但是,我想知道如何标记特定域,例如 Nginx 将为它们提供自定义 robots.txt 文件。

更具体地说,例如域 example.com 和 www.example.com 将提供来自 htdocs 目录的默认 robots.txt 文件。 (由于设置了“root /sites/mysite/htdocs”并且 robots.txt 文件位于 /sites/mysite/htdocs/robots.txt)

但是,我还希望域“example.internal.com”(指的是与 example.com 相同的服务器)提供自定义 robots.txt 文件;我想创建一个自定义 robots.txt,以便谷歌不会索引该内部域。

我考虑过复制服务器块并在其中一个服务器块中指定以下内容。然后以某种方式覆盖该服务器块中的 robots.txt 查找。

"server_name internal.example.com;"

但是仅仅为了这个目的而复制整个服务器块似乎不是很干。

我还考虑过使用 if 语句来检查主机头是否包含内部域。然后以这种方式提供自定义 robots.txt 文件。但是 Nginx 说的是If Is Evil

为内部域提供自定义 robots.txt 文件的好方法是什么?

感谢您的帮助。

这是我正在使用的服务器块的代码示例。

upstream app_server {
  server unix:/sites/mysite/var/run/wsgi.socket fail_timeout=0;
}

server {
  listen 80;

  root /sites/mysite/htdocs;    

  location / {
      try_files $uri @proxy_to_app;
  }

  location @proxy_to_app {
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header X-Forwarded-Protocol $scheme;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header X-Scheme $scheme;
     proxy_set_header Host $http_host;
     proxy_redirect off;
     proxy_pass   http://app_server;
  }
}

【问题讨论】:

    标签: django nginx dns robots.txt


    【解决方案1】:

    您可以使用map 来定义条件变量。将此外部添加到您的服务器指令:

    map $host $robots_file {
        default robots.txt;
        internal.example.com internal-robots.txt;
    }
    

    然后变量可以像这样与try_files一起使用:

    server_name internal.example.com;
    
    location = /robots.txt {
        try_files /$robots_file =404;
    }
    

    现在您的根目录中可以有两个 robots.txt 文件:

    robots.txt
    internal-robots.txt
    

    【讨论】:

    • 非常感谢您采用这种方法,putnamhill。这看起来正是我想要做的。
    猜你喜欢
    • 2021-12-12
    • 2013-06-06
    • 2021-02-07
    • 1970-01-01
    • 2021-10-12
    • 1970-01-01
    • 2012-07-31
    • 2022-12-12
    • 2012-07-07
    相关资源
    最近更新 更多