【问题标题】:Dynamic Virtual host in apache/nginx - proxy requests based on servername prefixapache/nginx 中的动态虚拟主机 - 基于服务器名前缀的代理请求
【发布时间】:2016-02-27 17:40:56
【问题描述】:

想为代理网络请求添加动态虚拟主机和规则,如下所示

subdomain1.example.com/contexta  => subdomain1.appservera.internal.host
subdomain1.example.com/contextb  => appserverb.internal.host
subdomain1.example.com/contextc  => appserverc.internal.host
subdomain2.example.com/contexta  => subdomain2.appservera.internal.host
subdomain2.example.com/contextb  => appserverb.internal.host
subdomain2.example.com/contextc  => appserverc.internal.host
subdomain3.example.com/contexta  => subdomain3.appservera.internal.host
subdomain3.example.com/contextb  => appserverb.internal.host
subdomain3.example.com/contextc  => appserverc.internal.host

广义的路由模式如下图所示。请求将被路由到的下游服务器是从服务器名称的前缀中获得的(尽管仅适用于某些上下文根)。

*.example.com/contexta  => *.appservera.internal.host
*.example.com/contextb  => appserverb.internal.host
*.example.com/contextc  => appserverc.internal.host

理想情况下,当添加新的子域时,我们希望 Apache 配置保持不变。那可能吗?

我查看了Mass Virtual Hosting,但文档不清楚。

如果这不是一个简单的用例,nginx 会更有能力吗?

【问题讨论】:

    标签: apache nginx


    【解决方案1】:

    这样的东西可以与nginx一起使用:

    server {
      ...
      server_name "~^(?<name>.+)\.example\.com$";
      ...
      location /contexta {
        proxy_pass http://$name.appservera.internal.host/;
        ...
      }
      location /contextb {
        proxy_pass http://appserverb.internal.host/;
        ...
      }
      ...
    }
    

    请参阅this documentationthis documentation 了解更多信息。我相信使用 Apache 也同样容易实现。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-08
    • 2012-01-22
    • 2018-05-04
    • 2021-08-20
    • 1970-01-01
    • 2018-08-24
    • 2015-03-06
    相关资源
    最近更新 更多