使用负载平衡器与数字海洋液滴,指向Cloudflare DNS

发布于 2025-02-08 07:54:43 字数 584 浏览 3 评论 0原文

使用数字海洋,我创建了一个与2滴相连的负载平衡器。这两个液滴都已经安装了NGINX,当我在浏览器上测试负载平衡器IP地址时,它在液滴1和2之间正确切换。

现在,我想将负载平衡器指向我在CloudFlare上创建的子域。假设我的域是示例。我想创建一个名为api.example.com的子域(这是Cloudflare上的一个记录)。这应该很简单。我在CloudFlare上创建了A唱片,并将其链接到数字海洋负载平衡器IP地址。

当我尝试查看api.example.com在浏览器上是否有效时,我会在连接时间出现522错误。

由于我的名字服务器在Cloudflare上进行了管理,而我的液滴(虚拟机)都在数字海洋上,我是否需要设置从HTTP转发到HTTPS的端口?我在这里没有太多经验。

有人曾经遇到过这一点,并知道使子域工作的步骤吗?
我是否需要在每个液滴中使用服务器块创建Config Nginx站点文件并使用Lets Encrypt和Certbot?

如果是这样,负载均衡器IP地址适合在哪里。由于我在每个液滴上进行此操作,所以我很难将自己的头缠绕着如何合并负载平衡器的配置,因为它不是技术上的VM我可以登录并安装NGINX(据我所知)。

任何帮助将不胜感激。

Using digital Ocean, I created a load balancer which is linked to 2 droplets. Both droplets have NGINX installed and when I test the load balancer ip address on a browser, it correctly toggles between droplet 1 and 2.

The issue now is I want to point the load balancer to a subdomain I created on CloudFlare. Lets say my domain is example.com. I wanted to create a subdomain called api.example.com (which is an A record on Cloudflare). This should be simple. I created the A record on Cloudflare and linked it to the digital ocean load balancer ip address.

When I try to see if api.example.com works on the browser, I get a 522 error where the connection times out.

Since my name servers are being managed on Cloudflare and my droplets(virtual machines) are on Digital Ocean, do I need to set up port forwarding from http to https? I don't have much experience here.

Anyone ever encounter this and know the steps to get the subdomain to work?
Do I need to create config nginx site files with server blocks in each droplet and use Lets Encrypt and Certbot?

If so, where does the load balancer ip address fit in. Since I'm doing this on each individual droplet, I'm having a hard time wrapping my head around how to incorporate config for a load balancer since it technically isn't a vm I can log into and install NGINX (as far as I know).

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑梦风尘 2025-02-15 07:54:43

我发现的解决方案是在数字海洋的负载平衡器设置中。为端口443设置新的端口转发规则,并设置SSL通过。完成此操作后,您将需要进入/etc/nginx/stite-dobailable,然后更新默认值或创建定义新服务器块的新.conf文件。在下面的示例中,我在 /var /www文件夹中创建了一个自定义的HTML目录和页面。

server {

        root /var/www/example.com/html;
        index index.html index.htm index.nginx-debian.html;

        server_name example.com www.example.com;

        location / {
                try_files $uri $uri/ =404;
        }

完成此操作后,您需要在启用站点的文件夹之间创建一个链接。有关如何创建此类文件的完整详细信息,我将其用作指南(它有点过时,但仍然有效)。

create&将Express REST API部署到数字海洋服务器

创建Conf文件并设置了服务器块后,您可以按照此链接进行SSL PassThrough。

从本质上讲,要正确工作,您可以使用Certbot创建SSL证书,以便HTTPS可以使用。您可以在每台虚拟机上执行此操作,也可以在数字海洋上可以创建液滴的图像。因此,我的建议是在一个液滴上进行所有配置,并使HTTP正确工作(这意味着您可以转到网站地址而不是IP,并且可以使用)。另一种方法是记录您在VM上的所有步骤,并使用DevOps工具(例如Ansible)来自动化此步骤,以便您不必手动自定义每台计算机。

两个站点都可以使用(您可以针对每个液滴测试子域进行测试,然后再切换子域指向负载平衡器),然后您可以将负载平衡器IP地址设置回CloudFlare,以获取域或子域的记录。

SSL通过我的理解通过,使负载平衡器可以将任何SSL证书检查传递给与负载平衡器相关的每个单独的液滴。因此,请注意,如果您要为每个液滴管理一个或多个证书,并且如果有过期,那液滴就无法正常工作。希望这会有所帮助。

The solution I found to work is in the load balancer settings in Digital Ocean. Set a new port forwarding rule for port 443 and set the SSL to pass through. Once that is done, you will need to go into the /etc/nginx/sites-available and either update the default or create a new .conf file where you define a new server block. In my example below, I created a custom html directory and page in the /var/www folder.

server {

        root /var/www/example.com/html;
        index index.html index.htm index.nginx-debian.html;

        server_name example.com www.example.com;

        location / {
                try_files $uri $uri/ =404;
        }

Once this is done, you need to create a link between sites-enabled folder. For full details on how to create this type of file, I used this as a guide (its a bit outdated but it still works).

Create & Deploy an Express Rest API to a Digital Ocean Server

Once the conf file is created and the server blocks are set up, you can follow this link for SSL passthrough.

How to Configure SSL Passthrough - Digital Ocean

Essentially for this to work correctly, you can use Certbot to create a SSL certificate so Https can work. You can either do this on each Virtual Machine or Digital Ocean has a way to create an image of a droplet. So my advice is to do all your configuration on one droplet and get the https to work correctly (meaning you can go to the site address and not the ip and it works). Another way to do this is document all your steps one on VM and use a devOps tool like Ansible to automate this so you dont have to manually customize each machine.

Once both sites work ( you can test the subdomain against each droplet to test before switching the subdomain to point to the load balancer), you can then set your load balancer ip address back to the cloudflare A record for your domain or subdomain.

SSL pass through from my understanding lets the load balancer pass any SSL certificate checks to each individual droplet associated with the load balancer. So do keep mind that if you are managing one or many certificates for each droplet and if any expire, that droplet wont work. Hopefully this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文