我如何配置nginx重定向到robots.txt&的网址? sitemap.xml的

时间:2009-07-07 07:25:11

标签: nginx robots.txt

我正在运行nginx 0.6.32作为couchdb的代理前端。我在数据库中有robots.txt,可以http://www.example.com/prod/_design/mydesign/robots.txt访问。我还有一个动态生成的sitemap.xml,位于类似的URL上。

我尝试过以下配置:

server {
  listen 80;
  server_name example.com;
  location / {
  if ($request_method = DELETE) {
    return 444;
  }
  if ($request_uri ~* "^/robots.txt") {
    rewrite ^/robots.txt http://www.example.com/prod/_design/mydesign/robots.txt permanent;
  }

  proxy-pass http://localhost:5984;
  proxy_redirect off;
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}

这似乎可以作为重定向,但有更简单的方法吗?

6 个答案:

答案 0 :(得分:55)

或者你可以把它放在自己的位置 -

location /robots.txt {
    alias /Directory-containing-robots-file/robots.txt;
} 

答案 1 :(得分:18)

我想你想设置一个位置。关键部分是“=”指令。

location = /robots.txt {
    alias /your_full_path/robots.txt ;
}

(当我没有包含=时,它仍然会转到默认的nginx根位置)

如下设置,会导致所有/robots.txt*请求从/ var / foo中读出。所以/robots.txt.bing试图从磁盘上读取/var/foo/robots.txt.bing。 “^〜”表示它是请求开头的正则表达式匹配。

location ^~ /robots.txt {
    root /var/foo;
}

答案 2 :(得分:5)

使用http://重写...旨在进行重定向。您可能正在寻找:

rewrite ^/robots.txt /prod/_design/mydesign/robots.txt last;

答案 3 :(得分:3)

这也应该有用,可能表现稍好。

location /robots.txt { alias /var/www/django/mysite/static/robots.txt; }

答案 4 :(得分:1)

请注意,第一个参数是正则表达式,因此您可能应该转义。并匹配行尾。

location / {
    rewrite ^/robots\.txt$ /static/robots.txt last;
    ...
}

NginxHttpRewriteModule

答案 5 :(得分:0)

考虑到robots.txt位于/var/www/mysite/static/robots.txt,以下内容对我有用:

location = /robots.txt {
    root /var/www/mysite/static/;
}