NGINX 配置:如何将 URL 数组重定向到主页

NGINX Config: How to redirect array of URLs to home page

我想要一组 URL,假设 ['/about','/supported-software', '/the-team', ...] 只是重定向到 /

我需要写多个 location { } 块吗?

我是 NGINX 配置的新手,因此非常感谢任何指导!

如果数组条目完全匹配,则以下位置应该会为您提供最佳性能:

location = /about { return 301 $scheme://$host; }
location = /supported-software { return 301 $scheme://$host; }
location = /the-team { return 301 $scheme://$host; }
# ... or put these in an included file

如果它们不是完全匹配,地图可能会更好:

map $uri $send_home {
    ~^/about/?  1;
    ~^/supported-software/? 1;
    ~^/the-team/?   1;
    # ... or put these in an included file
}

server {
    # ...

    if ($send_home) {
        return 301 $scheme://$host;
    }

    # ...
}

该地图将允许更灵活的重定向,例如:

/about
/about/
/about/stuff