【问题标题】:How can I redirect hundreds of hostnames to other hostnames using nginx如何使用 nginx 将数百个主机名重定向到其他主机名
【发布时间】:2016-04-12 05:03:35
【问题描述】:

我有一个系统,其中许多(约 20k)子域使用 nginx 的 default_server,它将工作传递给应用程序。

我还有许多 (~100) 主机名需要重定向到正确的主机名,每个主机名都不同,然后会重定向到 default_server。

one.example.com -> eleven.example.com
two.example.com -> twelve.domain.com
three.example.com -> wibble.example.com
blah.domain.com -> fifteen.example.com

重定向是任意的,即它们没有模式。

与其在需要或更新新的重定向时必须更新 nginx 配置来添加新的服务器块,我更愿意使用 nginx 可以检查重定向的某种映射文件。

遗憾的是,我搜索了很多,但没有找到类似的东西,我发现的所有示例都为每个重定向主机使用新的服务器块或使用正则表达式。我希望能够动态更新 nginx 可以引用的地图文件或数据库。

我目前最好的选择是更新后台应用程序以应用重定向。

【问题讨论】:

  • 这里是a map solution,听起来像您要找的。​​span>
  • @ColeTierney,不得不提的是,地图更改后仍然需要重新加载 nginx。

标签: redirect nginx


【解决方案1】:

我之前确实找到了该地图,但不清楚它是否可以以这种方式使用,并且没有一个示例显示它。说起来很容易。

这就是我所拥有的似乎有效的东西;

map $host $redirect_host {
  hostnames;

  one.david.org     eleven.david.org;
  two.david.org     twelve.steve.org;
  three.steve.org   thirteen.david.org;
  four.steve.org    fourteen.steve.org;

}

server {
  ...
   if ($redirect_host) {
     return 301 $scheme://$redirect_host$request_uri;
   }
  ...
}

可惜这个解决方案需要重启nginx,不过没什么大不了的。

【讨论】:

    猜你喜欢
    • 2011-03-26
    • 2013-07-28
    • 2019-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-03
    • 1970-01-01
    相关资源
    最近更新 更多