How to configure nginx for Yoast SEO sitemaps

Dave Hilditch
Talk to me

If you’ve been following my WordPress fast stack guides, you’ll know I’m a big fan of nginx for the speed and scalability it provides. Some of you asked how to configure nginx with that stack guide to work with Yoast SEO sitemaps. The guide below covers our stacks, but will also work for any nginx users.

Add the Yoast sitemap rewrite rules to your sites-available config file

With our rocket stack guide, the nginx file you need to edit will be:


So – either edit that file through FileZilla, or log into your server using SSH and run the following:

vi /etc/nginx/sites-available/rocketstack.conf

The only thing that really matters in terms of where to place the following code is to make sure it goes inside your server { … } blocks. With our guide there are two of these, one for HTTP and one for HTTPS, so you’ll need to paste this code twice.

# Rewrites for Yoast SEO XML Sitemap
rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;

location ~ ([^/]*)sitemap(.*).x(m|s)l$ {
    ## this rewrites sitemap.xml to /sitemap_index.xml
    rewrite ^/sitemap.xml$ /sitemap_index.xml permanent;
    ## this makes the XML sitemaps work
    rewrite ^/([a-z]+)?-?sitemap.xsl$ /index.php?xsl=$1 last;
    rewrite ^/sitemap_index.xml$ /index.php?sitemap=1 last;
    rewrite ^/([^/]+?)-sitemap([0-9]+)?.xml$ /index.php?sitemap=$1&sitemap_n=$2 last;
    ## The following lines are optional for the premium extensions
    ## News SEO
    rewrite ^/news-sitemap.xml$ /index.php?sitemap=wpseo_news last;
    ## Local SEO
    rewrite ^/locations.kml$ /index.php?sitemap=wpseo_local_kml last;
    rewrite ^/geo-sitemap.xml$ /index.php?sitemap=wpseo_local last;
    ## Video SEO
    rewrite ^/video-sitemap.xsl$ /index.php?xsl=video last;

Once you’ve edited the file, save it, then exit your editor.

Test for errors then restart nginx

On your server (putty, ssh etc), run the following command to double check you didn’t break your nginx config before you restart:

nginx -t

Once it has confirmed the file is ok, you can restart nginx using:

service nginx restart

Test your sitemap

From your wp-admin, visit SEO->General->Features tab then scroll to the sitemap and click the (i) button. It’ll tell you the URL for your sitemap.

Alternatively, just visit or

A tidier way to do this

I’ve added an extra Yoast-specific snippet to our Rocket Stack github repo. So, another way to do this would be to download those snippets and then have just this line inside both your server blocks:

include snippets/yoast-sitemaps.xml

You can find instructions to downloading our repo in our full WordPress Rocket Stack guide.


If you are using other sitemap plugins, there should be nginx-specific instructions that come with it to tell your web-server how to rewrite the pretty permalinks to feed the correct parameters to your sitemap generation plugin.

In the guide above, I’ve shown how to do this for Yoast specifically, but similar steps will work for all sitemap plugins.