nginx not serving the sitemap file

Viewed 769

I am trying to server static sitemap file and my nginx configuration is:-

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

so now when i try to do,

www.mysite.com/sitemap.xml

i get 404 not found error.

so when i check the nginx error log file i see nginx is trying to search sitemap file on

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

i don't understand why it is searching index.html file instead of sitemap.xml.

1 Answers

In case of the root directive, full path is appended to the root including the location part, whereas in case of the alias directive, only the portion of the path NOT including the location part is appended to the alias.

So for:

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

nginx will create the final path to be:

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

Assuming you don't have the sitemap.xml directory inside the .../www/html/ directory, this will result in a 404 error. If that's the case, you should use the alias directive. Where the location path is dropped before looking for the file at the said location.

try this:

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

This should result in the path:

/var/www/html/*
Related