如何配置nginx的重定向到一个网址的robots.txt和sitemap.xml的
-
11-09-2019 - |
题
我正在运行的nginx 0.6.32作为代理前端为CouchDB的。我有我的数据库中的robots.txt,作为可达 HTTP://www.example .COM /生产/ _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;
}
此显示为重定向的工作,但是有一个更简单的方法?
解决方案
或者你可以把它简单地在自己的位置 -
location /robots.txt {
alias /Directory-containing-robots-file/robots.txt;
}
其他提示
我想你想设置的位置。的关键部分是所述“=”指令。
location = /robots.txt {
alias /your_full_path/robots.txt ;
}
(当i不包括=,它仍然去默认nginx的根位置)
像下面设置它,将导致要被读出的/ var / FOO的所有/robots.txt*请求。所以/robots.txt.bing尝试阅读/var/foo/robots.txt.bing关闭磁盘。的“^〜”表示它是在请求的开始的正则表达式匹配。
location ^~ /robots.txt {
root /var/foo;
}
重写以http:// ...的目的是做重定向。您可能正在寻找:
rewrite ^/robots.txt /prod/_design/mydesign/robots.txt last;
此也应该工作,并可执行稍好。
location /robots.txt { alias /var/www/django/mysite/static/robots.txt; }
请注意,第一个参数是一个正则表达式,所以你也许应该逃脱。和匹配线的一端。
location / {
rewrite ^/robots\.txt$ /static/robots.txt last;
...
}
考虑到robots.txt
位于/var/www/mysite/static/robots.txt
,为我下列工作:
location = /robots.txt {
root /var/www/mysite/static/;
}
不隶属于 StackOverflow