【问题标题】:route different proxy based on subdomain request in nginx根据 nginx 中的子域请求路由不同的代理
【发布时间】:2014-01-30 14:40:53
【问题描述】:

我在该服务器中有一台专用服务器,我部署了 5 个 nodejs 应用程序。

domain name: www.nnd.com
dedicated server ip: xxx.xx.x.60

我有一个指向我的专用服务器 ip 的域。

子域是:

app1.nnd.com pointed to xxx.xx.x.60
app2.nnd.com pointed to xxx.xx.x.60
app3.nnd.com pointed to xxx.xx.x.60
app4.nnd.com pointed to xxx.xx.x.60
app5.nnd.com pointed to xxx.xx.x.60

现在在基于子域的 nginx 配置文件中,我需要路由代理。 示例:

{
    listen:80;
    server_name:xxx.xx.x.60
    location / {
        #here based on subdomain of the request I need to create proxy_pass for my node application 
    }
}

有什么条件,如何从代理头中获取原始域名?

【问题讨论】:

    标签: nginx proxy


    【解决方案1】:

    为每个虚拟主机创建一个

    server {
      server_name sub1.example.com;
      location / {
        proxy_pass http://127.0.0.1:xxxx;
      }
    }
    server {
      server_name sub2.example.com;
      location / {
        proxy_pass http://127.0.0.1:xxxx;
      }
    }
    

    然后继续,更改端口号以匹配正确的端口。

    【讨论】:

    • 我只是将专用 ip 映射到我的 sub-domain.so 域名我将它托管在外面,并在该设置中我用我的专用 ip 映射。
    • 那么我怎样才能得到原始代理主机名
    • 我想我错过了一些东西,不是所有的皮肤子域都指向同一个 IP 吗?原来的主机名是什么意思
    • 查看我何时 ping app1.nnd.com 地址请求将到达我的专用服务器 xxx.xx.x.60 现在 nginx $http_host 变量包含我的专用服务器 ip xxx.xx.x .60,所以现在哪里可以得到 app1.nnd.com 哪个 nginx 变量可能包含该信息
    • 你在使用负载平衡服务器块吗?子域在不同的服务器上吗?
    【解决方案2】:

    您可以使用 RegExp 来获取这样的主机名

    server {
        server_name   ~^(www\.)?(?<domain>.+)$;
    
        location / {
            root   /sites/$domain;
        }
    }
    

    【讨论】:

      【解决方案3】:

      您可以为每个子域创建虚拟主机。

      Ex 你有 2 个子域 abc.xyz.com 和 abcd.xyz.com ,你想通过 proxy_pass 在 nginx 单个实例上托管它,那么你可以简单地为每个子域

      server {
        server_name abc.xyz.com;
        location / {
          proxy_pass http://127.0.0.1:8000;
        }
      }
      server {
        server_name abcd.xyz.com;
        location / {
          proxy_pass http://127.0.0.1:8000;
        }
      }
      

      更多信息可以参考here

      【讨论】:

        猜你喜欢
        • 2020-04-16
        • 2012-04-09
        • 2022-07-02
        • 2017-01-13
        • 1970-01-01
        • 2021-03-01
        • 1970-01-01
        • 2015-04-17
        • 1970-01-01
        相关资源
        最近更新 更多