Score:0

Nginx: rewrite file from directory to root

um flag

I have nginx server running over linux environment, and it is handling the domains www.example1.com and www.example2.com. each domain has its own sitemap, so I need the correct sitemap loaded for each domain as it is in the root directory, for example:

www.example1.com/sitemap.xml is actually loaded from www.example1.com/sitemaps/1/sitemap.xml

And:

www.example2.com/sitemap.xml is actually loaded from www.example2.com/sitemaps/2/sitemap.xml

To achieve this, I've tried to assign a value for each domain and rewrite it based on the variable value like following:

in nginx.conf:

map $http_host $domain {
    www.example1.com      1;
    www.example2.com      2;
}

And in sitemap.conf:

if($domain=1){
  rewrite sitemaps/1/sitemap(.*)$ /sitemap last;
}
if($domain=2){
  rewrite sitemaps/2/sitemap(.*)$ /sitemap last;
}

But for some reason this configuration is returning 404.

Any advise?

sv flag
`sitemap.conf` could be simplified like `rewrite sitemaps/$domain/sitemap(.*)$ /sitemap last;`. This doesn't prevent 404, though. 404 was likely due to other reasons (than the snippets shared in OP). Kindly share the full configuration to troubleshoot.
Score:2
gr flag

I don't see your full config and don't know, how and where do you include that sitemap.conf file, but I'd rather did it in a complete different way. With your existing map block it would look like

location = /sitemap.xml {
    # use '$domain' variable as a part of the full path to 'sitemap.xml' file
    root /var/www/domain/sitemaps/$domain; # no trailing slash here!
}

or even get the full path to /sitemaps/N/ folder with the map directive like

map $http_host $sitemap_path {
    www.example1.com      /var/www/example1.com/sitemaps/1;
    www.example2.com      /var/www/example1.com/sitemaps/2;
}

and

location = /sitemap.xml {
    # use '$sitemap_path' variable as the full path to 'sitemap.xml' file
    root $sitemap_path; # no trailing slash here!
}

If you still want to use the rewrite directive for this task, you are using it incorrectly. You can try this one:

if ($domain=1) {
    rewrite ^/sitemap\.xml$ /sitemaps/1/sitemap.xml last;
}
if ($domain=2) {
    rewrite ^/sitemap\.xml$ /sitemaps/2/sitemap.xml last;
}

or even more optimized:

if ($domain) {
    rewrite ^/sitemap\.xml$ /sitemaps/$domain/sitemap.xml last;
}
Eng7 avatar
um flag
Full configs will be confusing, that's why I've added only the related part of the configuration.
Eng7 avatar
um flag
Changing the root dir for the sitemap.xml location worked for me. For the rewrite part, what is the "last" is used for?
Ivan Shatsky avatar
gr flag
You can consider it as copy-paste from your original config. Generally, `last` flag does mean the whole URI procession should take place again, but this time for the altered `$uri` value.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.