如何在nginx中为全部虚拟主机全局设置robots.txt

我正在尝试为nginx http服务器下的所有虚拟主机设置robots.txt 。 我可以通过在主要的httpd.confjoin以下内容来实现:

 <Location "/robots.txt"> SetHandler None </Location> Alias /robots.txt /var/www/html/robots.txt 

我尝试在nginx.conf和(b)中添加下面的(a)行来做类似于nginx的工作,包括conf.d / robots.conf

 location ^~ /robots.txt { alias /var/www/html/robots.txt; } 

我已经尝试了'=',甚至把它放在虚拟主机之一来testing它。 似乎没有任何工作。

我在这里错过了什么? 有没有另一种方法来实现这一目标?

是否有其他规则是定义的? 也许common.conf或其他conf文件包含在你的configuration中。 以下其中一个应该肯定工作。

 location /robots.txt { alias /home/www/html/robots.txt; } location /robots.txt { root /home/www/html/; } 
  1. Nginx以其外观顺序运行所有“正则expression式”位置。 如果任何“正则expression式”位置成功,Nginx将使用此第一个匹配。 如果没有“regexp”位置成功,Nginx会使用上一步find的普通位置。
  2. “正则expression式”位置优先于“前缀”位置

您可以直接在nginxconfiguration中设置robots.txt文件的内容:

  location /robots.txt { return 200 "User-agent: *\nDisallow: /\n"; } 

也可以添加正确的Content-Type:

  location /robots.txt { add_header Content-Type text/plain; return 200 "User-agent: *\nDisallow: /\n"; } 

位置不能在http块内使用。 nginx没有全局别名(即,可以为所有虚拟主机定义的别名)。 保存您的全球定义在一个文件夹中,并包括这些。

 server { listen 80; root /var/www/html; include /etc/nginx/global.d/*.conf; }