There are no special rules needed for XML Sitemap & Google News sitemaps.
The basic Nginx rules as discussed on https://wordpress.org/support/article/nginx/ “just work”. When there are no other rules that target the sitemap request, they will simply be passed to the main index.php file and thus be treated by WordPress like any other dynamic request.
The main Nginx rules responsible for passing sitemap requests to WordPress (via index.php, note the third and fourth location rule) will look like this:
server {
## Your website name goes here.
server_name domain.tld;
## Your only path reference.
root /var/www/wordpress;
## This should be in your http block and if it is, it's not needed here.
index index.php;
location = /favicon.ico {
log_not_found off;
access_log off;
}
location = /robots.txt {
allow all;
log_not_found off;
access_log off;
}
location / {
# This is cool because no php is touched for static content.
# include the "?$args" part so non-default permalinks doesn't break when using query string
try_files $uri $uri/ /index.php?$args;
}
location ~ \.php$ {
#NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
include fastcgi.conf;
fastcgi_intercept_errors on;
fastcgi_pass php;
}
location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
expires max;
log_not_found off;
}
}
However, your setup may include other rules or subtle differences that can interfere with the dynamic sitemap. So if your sitemaps are failing oin Nginx, continue reading…
Static file caching rules
There may be rules in your config that are meant to regulate static file caching. These may include the xml file extension and thus interfere with the dynamic requests reaching WordPress.
For example:
# Cache static files
location ~* \.(txt|xml|css|rss|js|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|exe|ppt|tar|mid|midi|wav|bmp|rtf|swf)$ {
add_header "Access-Control-Allow-Origin" "*";
access_log off;
log_not_found off;
expires max;
}
Make sure to remove the XML file extension!
Don’t forget to do a nginx -t && service nginx reload
to apply the changes to the running Nginx instance.
Dedicated sitemap rules
Remove all dedicated sitemap rules that you may find in your nginx config files. Like these for example, created by the EasyEngine script:
# Yoast sitemap
location ~ ([^/]*)sitemap(.*)\.x(m|s)l$ {
rewrite ^/sitemap\.xml$ /sitemap_index.xml permanent;
rewrite ^/([a-z]+)?-?sitemap\.xsl$ /index.php?xsl=$1 last;
# Rules for yoast sitemap with wp|wpsubdir|wpsubdomain
rewrite ^.*/sitemap_index\.xml$ /index.php?sitemap=1 last;
rewrite ^.*/([^/]+?)-sitemap([0-9]+)?\.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
# Following lines are options. Needed for WordPress seo addons
rewrite ^/news_sitemap\.xml$ /index.php?sitemap=wpseo_news last;
rewrite ^/locations\.kml$ /index.php?sitemap=wpseo_local_kml last;
rewrite ^/geo_sitemap\.xml$ /index.php?sitemap=wpseo_local last;
rewrite ^/video-sitemap\.xsl$ /index.php?xsl=video last;
access_log off;
}
Simply remove the whole block!
These rules are not needed even for Yoast sitemaps and will interfere with all sitemap requests. But if you wish to keep them, make sure to at least comment out the first two lines in the block:
#rewrite ^/sitemap\.xml$ /sitemap_index.xml permanent;
#rewrite ^/([a-z]+)?-?sitemap\.xsl$ /index.php?xsl=$1 last;
Don’t forget to do a nginx -t && service nginx reload
to apply the changes to the running Nginx instance.