如何为我的用户启用自定义域?

How do I enable custom domains for my users?

我希望允许我的用户使用他们自己的域名来访问我的网络服务。

例如,如果我有一个用户在示例中有个人资料。com/users/david 我如何允许大卫使用 exampledavid.com 访问他的个人资料页面。

我不希望用户在地址栏中看到 example.com,而是让大卫看到 exampledavid.com。

您可以通过告诉您的客户配置 CNAME 以指向您的域来轻松启用此功能。

因此,如果您的服务器位于 www.example.com,您告诉 'david' 配置 www.exampledavid.com 以拥有指向 www.example.com

的 CNAME 记录

在服务器端,您将有一个配置来检测正在请求和重定向的域,并将适当的内容提供给 'david'

如果您的客户想要使用裸域,即 exampledavid.com 到您的服务器,您需要向他们提供 IP 地址,但是在执行此操作之前,您需要确保您的 IP 地址不是' 将要改变,并且可能与供应它的人签订合同以确保这一点。

您的问题可以通过 URL 重写和 HTTP Header 操作或 reverse-proxy.

来解决
  • 对于 apache http 服务器:使用 ProxyPassReverse 指令

The directive ProxyPassReverse lets Apache adjust the URL in the Location header on HTTP redirect responses. For instance this is essential when Apache is used as a reverse proxy to avoid by-passing the reverse proxy because of HTTP redirects on the backend servers which stay behind the reverse proxy.

Suppose the local server has address http://wibble.org/; then

ProxyPass /mirror/foo/ http://foo.com/
ProxyPassReverse /mirror/foo/ http://foo.com/

will not only cause a local request for the http://wibble.org/mirror/foo/bar to be internally converted into a proxy request to http://foo.com/bar (the functionality ProxyPass provides here). It also takes care of redirects the server foo.com sends: when http://foo.com/bar is redirected by him to http://foo.com/quux Apache adjusts this to http://wibble.org/mirror/foo/quux before forwarding the HTTP redirect response to the client.

  • 对于 MS(R) IIS,使用 Re-Write 模块:

Easily replace Web application URLs to produce user and search engine >friendly results. URL Rewrite permits Web administrators to easily replace the URLs >generated by a Web application in the response HTML with a more user friendly and search engine friendly equivalent. Links can be modified in the HTML markup generated by a Web application behind a reverse proxy. URL Rewrite makes things easier for outbound response content and headers rewriting with outbound rewrite rules that work with HTTP request and response headers and with IIS server variables.

此外,您必须确保 exampledavid(dot)com 设置有 DNS 提供商,以将 所有 请求传递给 example.com。

DNS 记录示例:

NAME                    TYPE   VALUE
--------------------------------------------------
exampleXYZ.com.         CNAME  example.com.
example.com.            A      192.0.2.23

参考:

  1. https://en.wikipedia.org/wiki/CNAME_record

  2. http://www.akadia.com/services/apache_redirect.html

  3. http://httpd.apache.org/docs/2.2/mod/mod_proxy.html

  4. http://www.iis.net/downloads/microsoft/url-rewrite

我看到了不错的答案,但这里没有人给出完整的图片。

如果您的客户只是对您的域进行 CNAME 或为您的 IP 创建 A 记录,而您不为这些自定义域处理 TLS 终止,则您的应用将不支持 HTTPS,如果没有它,您的应用将不会在现代浏览器中处理这些自定义域。

您需要在您的网络服务器前设置一个 TLS 终止反向代理。该代理可以 运行 在单独的机器上,但您可以 运行 它与网络服务器在同一台机器上。

CNAME 与 A 记录

如果您的客户希望将您的应用放在他们的子域中,例如app.customer.com 他们可以创建 CNAME app.customer.com 指向您的代理。

如果他们想将您的应用放在他们的根域中,例如customer.com 然后他们必须在 customer.com 上创建一条 A 记录指向您代理的 IP。确保此 IP 永远不会更改!

如何处理 TLS 终止?

要使 TLS 终止工作,您必须为这些自定义域颁发 TLS 证书。您可以为此使用 Let's Encrypt。您的代理将看到传入请求的 Host header,例如app.customer1.comcustomer2.com 等,然后它会通过检查 SNI 来决定使用哪个 TLS 证书。

可以将代理设置为自动为这些自定义域颁发和续订证书。在来自新自定义域的第一个请求中,代理将看到它没有适当的证书。它会要求 Let's Encrypt 提供新证书。 Let's Encrypt 将首先发出挑战,看看您是否管理该域,并且由于客户已经创建了指向您的代理的 CNAME 或 A 记录,这告诉 Let's Encrypt 您确实管理该域,它会让您颁发证书它。

要自动颁发和续订证书,我建议使用 Caddyserver、greenlock.js、OpenResty (Nginx)。

tl;dr 关于这里发生的事情; Caddyserver 侦听 443 和 80,它接收请求、颁发和自动更新证书,将流量代理到您的后端。

如何在我的后端处理它

您的代理正在终止 TLS 并将请求代理到您的后端。但是,您的后端不知道请求背后的原始客户是谁。这就是为什么您需要告诉代理在代理请求中包含额外的 header 以识别客户。只需添加 X-Serve-For: app.customer.comX-Serve-For: customer2.com 或任何 Host header 是原始请求的内容。

现在当您在后端收到代理请求时,您可以阅读此自定义 header 并且您知道请求背后的客户是谁。您可以基于此实现您的逻辑,显示属于该客户的数据等。

更多

在您的代理队列前面放置一个负载平衡器以获得更高的可用性。您还必须为证书和 Let's Encrypt 挑战使用分布式存储。如果出现故障,请使用 AWS ECS 或 EBS 进行自动恢复,否则,您可能会在半夜醒来并手动重启机器或代理。

如果您需要更多详细信息,可以在 Twitter 上私信我 @dragocrnjac