【问题标题】:NGINX Config: How to redirect array of URLs to home pageNGINX Config:如何将 URL 数组重定向到主页
【发布时间】:2016-05-11 20:53:22
【问题描述】:

我想要一个 URL 数组,假设 ['/about','/supported-software', '/the-team', ...] 简单地重定向到 /

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

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

【问题讨论】:

  • 我会使用多个前缀位置。如果您有很多,您可以尝试使用包含 URL 的包含文件的地图。
  • 嗨@ColeTierney,非常感谢您的回复。你介意给我举个例子吗?
  • 我添加了一个带有几个想法的答案。

标签: redirect nginx configuration config


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2015-10-19
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 2017-01-29
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 2014-12-17
    相关资源
    最近更新 更多