How to Host a sitemap.xml File When Using Nginx to Proxy Requests to Ghost


Ghost already has a built in sitemap that you can view if you go to yoursite.com/sitemap.xml. If, however, you would like to host your own custom sitemap, we show you how to do that here.

To start, here is what a typical Nginx config file looks like for Ghost:

server {
    listen 80;
    server_name allaboutghost.com;

    location / {
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;
        proxy_pass         http://127.0.0.1:2368;
    }
}

All traffic on port 80 is redirected to port 2368, which will be served by Ghost. What we are going to do is direct traffic to sitemap.xml to a static file we have somewhere else. This example assumes the sitemap is living in /var/www/sitemap.xml so adjust the paths to fit yours:

location ~ ^/(sitemap.xml) {
    root /var/www;
}

Putting it all together, the Nginx configuration file would look like this:

server {
    listen 80;
    server_name allaboutghost.com;

    location ~ ^/(sitemap.xml) {
        root /var/www;
    }

    location / {
        proxy_set_header   X-Real-IP $remote_addr;
        proxy_set_header   Host      $http_host;
        proxy_pass         http://127.0.0.1:2368;
    }
}

With that change in place, you can now restart Nginx with service nginx restart to see your changes take effect.