【问题标题】:How to reroute traffic coming from a subdomain to a specific port on the server?如何将来自子域的流量重新路由到服务器上的特定端口?
【发布时间】:2019-01-22 13:02:59
【问题描述】:

我有一个使用 pm2 运行 2 个 Web 应用程序的 AWS EC2 实例。服务器地址配置为test.example.com。 (GoDaddy DNS A-record 指向 EC2 的弹性 IP)应用 1 运行在 3000 端口,应用 2 运行在 4000 端口。

我想要实现的是:

  1. 来自subdomain1 的所有流量都流向在端口 3000 上运行的应用程序。
  2. 来自subdomain2 的所有流量都流向端口 4000。

现在棘手的部分来了,最终结果应该是这样的test.sub1.example.comtest.sub2.example.com。 (我是 subdomaining 的菜鸟,所以它可能必须配置为 sub1.test.example.comsub2.test.example.com 我不知道)

我尝试通过在 GoDaddy 上设置 SRV 记录指向特定端口来执行此操作,但它不起作用。

Service  : _something
Protocol : _http
Name     : sub2
Target   : test.example.com
Priotiry : 0
Weight   : 0
Port     : 4000
TTL      : 1 hour

我也读过这个reddit article,但据我所知,它会将所有流向服务器的流量重定向到 1 个特定端口。如果我有 2 个不同的 EC2 实例,每个实例都运行自己的 web 应用程序并以这种方式进行配置,这不会有问题,但这不是一个选项。

额外信息:EC2 实例运行Ubuntu 16.04,EC2 上的网络服务器是nginx 1.14.0

【问题讨论】:

  • SRV 记录确实有效,并被各种应用程序使用,但明显例外:网络浏览器确实使用它们。所以你可以发布 SRV 记录但浏览器根本看不到它们,要么默认直接进入 80/443,要么进入 URL 中写的端口,例如http://www.example.com:4321/。否则,您需要设置一个反向代理,它将获取 80/443 上的查询,并根据传入查询的主机名或 URL 路径将它们重定向或代理到各种相关端口。这也和编程关系不大,所以这里有点题外话。
  • 你能告诉我从哪里开始寻找如何设置这个反向代理的东西吗?另外(更多元)我应该在哪个 stackexchange 上发布这个?

标签: amazon-web-services nginx amazon-ec2 dns port


【解决方案1】:

我似乎找到了一个简单的解决方案:

我在 GoDaddy 的 DNS 上添加了 3 个子域:

A: test.example.com            -> <aws-elastic-IP>
A: subdomain1.test.example.com -> <aws-elastic-IP>
A: subdomain2.test.example.com -> <aws-elastic-IP>

然后在 nginx 的默认配置中,为每个子域添加一个指向正确端口的代理通道

server { # landing page
    server_name  test.example.com;
    index index.html;
    root   /home/user/project-source;
    location / {
        root   /home/user/project-source;
    }
    ...
}

server { # webapp running on port 3000
    server_name subdomain1.test.example.com;
    location / {
        proxy_pass http://localhost:3000;
        ...
    }
    ...
}

server { # webapp running on post 4000
    server_name subsomain2.test.example.com;
    location / {
        proxy_pass http://localhost:4000;
        ...
    }
    ...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-06
    • 1970-01-01
    • 2015-10-08
    相关资源
    最近更新 更多