2009-07-07 47 views
18

我正在运行nginx 0.6.32作为couchdb的代理前端。我在数据库中有我的robots.txt,可以达到http://www.example.com/prod/_design/mydesign/robots.txt。我也有我的sitemap.xml是动态生成的,类似的网址。如何配置nginx重定向到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; 
} 

这似乎是一个重定向到工作,但有一个更简单的方法?

回答

49

或者你可以把它简单地在自己的位置 -

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

这看起来是最简单的解决方案,并且对我来说完美无缺。 – Gattster 2010-12-15 02:28:08

5

用http://重写将被执行重定向。您可能正在寻找:

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

需求等于工作: `位置= {/sitemap.xml \t重写^ /网站地图\。 xml $ /index.php?xxxxxxxxx永久; }` – 2016-03-16 04:22:02

1

注意,第一个参数是一个正则表达式,所以你也许应该逃脱。并匹配行尾。

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

NginxHttpRewriteModule

3

这也应该工作,可以执行略胜一筹。

location /robots.txt { alias /var/www/django/mysite/static/robots.txt; } 
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; 
} 
0

考虑到robots.txt位于/var/www/mysite/static/robots.txt,对我下面的工作:

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