AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1157158
Accepted
Mervin Hemaraju
Mervin Hemaraju
Asked: 2024-03-31 04:17:38 +0800 CST2024-03-31 04:17:38 +0800 CST 2024-03-31 04:17:38 +0800 CST

使用带有 CertBot 证书和域名的 Flask 应用程序对两台服务器进行负载平衡

  • 772

我有两台服务器,我们称它们为ServerA和ServerB。

我在 NameCheap 上购买了一个域名,我们称之为它example.com

每台服务器都有在不同端口上运行的 docker 容器(Flask Web 应用程序)。

例子:

WebApp1 running on port 8080
WebApp2 running on port 8081
.
.

两台服务器的配置相同。

然后,我在端口 443 上使用 nginx 作为反向代理,每个端口都有自己的子域。

例子:

WebApp1 running on port 8080 will be accessible via test1.example.com
WebApp2 running on port 8081 will be accessible via test2.example.com
.
.

我正在使用 CertBot 来获取 SSL 证书。

我的两台服务器托管在 OCI(Oracle 云)上,我在 OCI 上构建了一个网络负载均衡器来平衡服务器之间的流量。

以下是我的配置:

nginx.conf

user nginx;
worker_processes auto;
pid /run/nginx.pid;
include /etc/nginx/modules-enabled/*.conf;

events {
    worker_connections 800;
}

http {

    ##
    # Basic Settings
    ##
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    keepalive_timeout 65;
    types_hash_max_size 2048;

    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    ##
    # SSL Settings
    ##
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; # Dropping SSLv3, ref: POODLE
    ssl_prefer_server_ciphers on;

    ##
    # General Logging Settings
    ##
    access_log /var/log/nginx/access.log;
    error_log /var/log/nginx/error.log;

    ##
    # Gzip Settings
    ##
    # gzip on;
    # gzip_vary on;
    # gzip_min_length 10240;
    # gzip_proxied expired no-cache no-store private auth;
    # gzip_types text/plain text/css text/xml text/javascript application/x-javascript application/xml;
    # gzip_disable "MSIE [1-6]\.";

    ##
    # Web Apps configurations
    ##
    include /etc/nginx/sites-enabled/*;
}

/etc/nginx/sites-enabled/test1.example.com

server {
  server_name   test1.example.com;

  location / {
    access_log  /var/log/nginx/test1/access.log;
    error_log  /var/log/nginx/test1/error.log;
    proxy_pass  http://localhost:8080;
  }

    listen 443 ssl; # managed by Certbot
    ssl_certificate /etc/letsencrypt/live/test1.example.com/fullchain.pem; # managed by Certbot
    ssl_certificate_key /etc/letsencrypt/live/test1.example.com/privkey.pem; # managed by Certbot
    include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot

}

server {
    if ($host = test1.example.com) {
        return 301 https://$host$request_uri;
    } # managed by Certbot

  server_name   test1.example.com;
    listen 80;
    return 404; # managed by Certbot
}

目前,域名 test1.example.com 有一条到 ServerA 的 A 记录,该记录工作正常,我可以访问我的 WebApp。

但我希望域指向我的负载均衡器,以便我可以平衡两台服务器上的流量。但我不能这样做,除非我在两台服务器上颁发 SSL 证书,但我不能。因为在 上发布 test1.example.com 后ServerA,这样做ServerB会导致 certbot 出错,指出证书已分配给ServerA。

有人可以帮助我如何做到这一点吗?

nginx
  • 1 1 个回答
  • 118 Views

1 个回答

  • Voted
  1. Best Answer
    z0le
    2024-04-03T20:56:42+08:002024-04-03T20:56:42+08:00

    为了解决您提到的 SSL 证书和负载平衡问题,有一个可行的解决方案。您可以将在ServerA上创建的SSL证书通过SCP(Secure Copy Protocol)传输到ServerB,以安全地复制证书和私钥。

    使用Certbot在ServerA上创建证书后,您可以手动或自动将相应文件(fullchain.pem和privkey.pem)从ServerA复制到ServerB。可以使用定期运行(例如每天一次)的 cronjob 来实现自动化,以确保两台服务器都具有最新的证书。

    以下是复制证书文件的命令示例:

    scp /etc/letsencrypt/live/test1.example.com/fullchain.pem user@ServerB:/path/to/certificate/
    scp /etc/letsencrypt/live/test1.example.com/privkey.pem user@ServerB:/path/to/certificate/
    

    将“user”替换为您在 ServerB 上的用户名,将“/path/to/certificate/”替换为 ServerB 上应保存证书的相应路径。

    对于自动复制,您可以创建一个执行这些命令的 shell 脚本,并设置一个 cronjob 来定期运行该脚本。

    正确配置 SSH 密钥对于服务器之间的安全和自动化通信非常重要。另外,请确保两台服务器的安全,因为SSL证书的私钥是敏感信息。

    • 2

相关问题

  • Gzip 与反向代理缓存

  • nginx 作为代理的行为

  • Nginx 学习资源 [关闭]

  • 提供 70,000 个静态文件 (jpg) 的最佳方式?

  • 在 Apache、LightTPD 和 Nginx Web 服务器上提供 PHP 5.x 应用程序的现状?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve