首页 > 解决方案 > nginx 不提供站点地图文件

问题描述

我正在尝试服务器静态站点地图文件,我的 nginx 配置是:-

location /sitemap.xml/ {
    autoindex on;
    root /var/www/html;
}

所以现在当我尝试做的时候,

www.mysite.com/sitemap.xml

我得到404 not found错误。

所以当我检查 nginx 错误日志文件时,我看到 nginx 正在尝试搜索站点地图文件

/var/www/html/sitemap.xml/index.html

我不明白为什么它搜索 index.html 文件而不是 sitemap.xml。

标签: nginx

解决方案


root指令的情况下,完整路径被附加到包含位置部分的根,而在alias指令的情况下,只有不包括位置部分的路径部分被附加到别名。

因此对于:

 location = /sitemap.xml/ {
       autoindex on;
       root /var/www/html;
    }

nginx 将创建最终路径为:

/var/www/html/sitemap.xml/*

假设您在sitemap.xml目录中没有.../www/html/目录,这将导致 404 错误。如果是这种情况,您应该使用该alias指令。在所述位置查找文件之前放置位置路径的位置。

试试这个:

location = /intern/ {
       autoindex on;
       alias /var/www/html/;  # <----- trailing slash is important here
    }

这应该导致路径:

/var/www/html/*

推荐阅读