Warm tip: This article is reproduced from serverfault.com, please click

How to redirect every sitemaps sections to another url using NGINX proxy_pass

发布于 2020-12-03 09:49:13

I would like to redirect all our sitemap sections the same way we did for the root one.

        location = /sitemap.xml {
            proxy_pass https://redacted.fr/sitemap.xml;
        }

So I would like to do the same for, let say, sitemap-group1.xml but I can't write it down to NGINX like the root one as it's dynamic.
I think what would help is to have a wildcard and get the path to proxy_pass, but I have no idea how.

EDIT: My only goal is to proxy_pass /sitemap*.xml to https://redacted.fr/sitemap*.xml

Questioner
VivienG
Viewed
0
Ivan Shatsky 2020-12-03 18:06:44

Try the regex matching location block:

location ~ ^/sitemap[^/]*\.xml$ {
    proxy_pass https://redacted.fr;
}

If you have any other regex matching locations (locations with ~ modifier), take into account that the first matched location is used for the request processing, so I suggest to put this one before any other of regex matching locations. If you want case-insensitive matching, use ~* instead of ~.