Nginx的robots.txtconfiguration

我似乎无法正确configurationnginx来返回robots.txt内容。 理想情况下,我不需要该文件,只想提供直接在nginx中configuration的文本内容。 这是我的configuration:

server { listen 80 default_server; listen [::]:80 default_server ipv6only=on; root /usr/share/nginx/html; index index.html index.htm; server_name localhost; location = /robots.txt { #return 200 "User-agent: *\nDisallow: /"; #alias /media/ws/crx-apps/eap/welcome-content/robots.txt; alias /usr/share/nginx/html/dir/robots.txt ; } location / { try_files $uri $uri/ =404; } } 

= /robots.txt位置中的任何内容都不起作用,我不明白为什么。 访问http://localhost/robots.txt给出了404。但是, http://localhost/index.html是正确的。

请注意,除了添加新的位置(用于testing)之外,我没有更改从apt-get install nginx获得的nginx的任何默认设置。

首先,我认为你的configuration中的问题是用于匹配的正则expression式。 在你的configuration文件中写下这样的语句是非常有用的,以防止模式匹配可能出现的错误:

  location = /robots.txt { alias /usr/share/nginx/html/dir/robots.txt; } 

其次,你还应该检查/usr/share/nginx/html/dir/robots.txt的权限和有效的所有者