【问题标题】:How to set up HTTPS on Amazon EC2 with Elastic Load Balancer如何使用 Elastic Load Balancer 在 Amazon EC2 上设置 HTTPS
【发布时间】:2017-06-29 15:48:03
【问题描述】:

我已在 Amazon Certificate Manager 中申请证书。现在它的状态为“已发布”。 在 EC2 控制台中,我创建了负载均衡器。有 2 个侦听器:HTTP 和 HTTPS。我尝试了 Application Load Balancer 和 Classic Load Balancer,但无法通过 HTTPS 连接到我的站点。

我的 nginx 配置:

server {
    listen 80;
    #listen 443 ssl;

    rewrite ^(.*) https://$host$1 permanent; 

    server_name site.com www.site.com;
    root /home/ubuntu/www/site.com/wordpress;
    index index.php;

    client_max_body_size 20m;
    gzip on;
    gzip_disable "msie6";
    gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript;


    location ~* ^/(\.htaccess|xmlrpc\.php)$ {
        return 404;
    }

    location ~ /\. {
            deny all;
    }

    location ~* /(?:uploads|files)/.*\.php$ {
            deny all;
    }

    location ~* ^.+\.(ogg|ogv|svg|svgz|eot|otf|woff|mp4|ttf|rss|atom|jpg|jpeg|gif|png|ico|zip|tgz|gz|rar|bz2|doc|xls|exe|ppt|tar|mid|midi|wav|bmp|rtf)$ {
            access_log off;
            log_not_found off;
            expires max;
    }

    location / {
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;

        set $is_https 'off';
            if ($http_x_forwarded_proto ~ 'https') {
                set $is_https 'on';
            }
        proxy_set_header HTTPS $is_https;

        proxy_redirect off;
        if (!-f $request_filename) {
            proxy_pass http://app_server;
            break;
     }
            #try_files $uri $uri/ /index.php?$args; # permalinks
    }

    location ~ \.php$ {
            fastcgi_pass unix:/var/run/php/php7.1-fpm.sock;
            fastcgi_index index.php;
            include fastcgi_params;
    }

}

如何手动导入证书?或者有办法用亚马逊证书建立 HTTPS 连接?

【问题讨论】:

  • 您是否将证书设置为 ELB?文档在这里:docs.aws.amazon.com/elasticloadbalancing/latest/classic/…
  • @minamijoyo 是的,我做到了。是否需要配置后端实例认证(可选)?
  • 不,您不需要后端身份验证。 nginx访问或错误日志中有输出吗?
  • 你是打负载均衡器还是直接打服务器?
  • @MarkB 你是什么意思?

标签: amazon-web-services ssl nginx amazon-ec2 ssl-certificate


【解决方案1】:

如果您有 ACM 证书,您只需从 ELB 侦听器中为 HTTPS/443 选择该证书,您无需费心在您的 nginx 实例中设置 SSL 配置。让它在端口 80 上响应。

您只需要 ELB 中的 HTTP/80 和 HTTPS/443 到实例上的 HTTP 端口 80(此示例使用经典 ELB)

如果证书在 ACM 中,当您在 SSL 证书下选择“更改”时,您应该会在下拉列表中看到它一次。

现在您只需确保您的 ELB 安全组设置为允许来自 80/443 的入口和来自 80 的出口。并且您需要确保您的实例允许来自 ELB 安全组的入口。

【讨论】:

  • 是的,我这样做了。但正如@MarkB 所说,我应该配置我的 dns 设置以使用 ELB。在不使用 Route53 的情况下如何做到这一点?在我的 Godaddy 帐户中,我有一条名称为“@”且值为“EC2 ip”的 A 记录。
  • 在 ELB 描述中,您将看到一条 DNS 记录。尝试直接点击它,它应该可以工作。然后只需在 GoDaddy 中使用/创建一个指向该 ELB DNS 的 CNAME 记录。
  • 我更新了我的帖子。请查看我的域配置。 HTTPS 仍然不起作用。当我尝试通过 ELB DNS 名称直接打开 url 时,它会将我重定向到 ELB_DNS 并显示错误“您的连接不是私有的”,因为它的安全证书来自 *.mydomain.com
  • @Alex Pavlov - 您应该首先确认 ELB 工作正常。在此之前不要担心 DNS 配置。在 ELB Instance 选项卡中,确认您的实例是“InService”。如果不是,ELB 将不允许任何流量通过。一旦你有这个工作,然后直接点击 ELB DNS(在描述选项卡中找到)。如果可行,那么您可以开始配置外部 DNS。
【解决方案2】:

根据您的 ELB 设置和端口映射,当您的实例位于 AWS ELB 之后(h/t @Michael - sqlbot)时,$https 变量并不总是有效。您应该改用HTTP_X_FORWARDED_PROTO 标头并将重写规则移到检查HTTP_X_FORWARDED_PROTO 标头的IF 语句中:

location / {
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_redirect off;

  if ($http_x_forwarded_proto != "https") {
    rewrite ^(.*)$ https://$server_name$1 permanent;
  }

  .
  .
  .
}

安全组注意事项

此外,您需要确保您的负载均衡器可以与侦听器端口和运行状况检查端口上的已注册目标进行通信。欲了解更多信息:

Security Groups for Your Application Load Balancer

Security Groups for Your Classic Load Balancer

【讨论】:

  • 它将我重定向到 https,但不起作用。 nginx 日志中没有错误。在负载均衡器中,我有监听器:1) HTTP 80 --> HTTP 80,2) HTTPS 443 --> HTTP 80。在负载均衡器中的选项卡实例中,它的状态为“OutOfService”。
  • 为什么要把 HTTPS 协议的监听端口设置为 80? HTTPS 侦听器应侦听端口 443。
  • @KhalidT。 “HTTPS 在 AWS ELB 后面不起作用” 在这种情况下是合适的建议,但作为参考,it is not always true
  • @Michael-sqlbot:你说得对:)我已经更新了我的答案。谢谢。
猜你喜欢
  • 2017-05-10
  • 2012-11-01
  • 2016-12-03
  • 2016-02-05
  • 2015-04-14
  • 2018-10-11
  • 2022-01-10
  • 2020-01-14
  • 2021-03-04
相关资源
最近更新 更多