2017-02-11 139 views
-2

我已经在亚马逊证书管理器中申请了证书。现在它具有“已发布”状态。 在EC2控制台中,我创建了Load Balancer。有2个听众:HTTP和HTTPS。我尝试了应用程序负载平衡器和经典负载平衡器,但无法通过HTTPS连接到我的站点。如何使用Elastic Load Balancer在Amazon EC2上设置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; 
    } 

}

如何导入手动证书?或者有一种方法可以设置与Amazon证书的HTTPS连接? ( - sqlbot H/T @迈克尔)

Godaddy domain DNS settings

+0

您是否将证书设置为ELB? Doc在这里:http://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb-add-or-delete-listeners.html – minamijoyo

+0

@minamijoyo是的,我做到了。我需要配置后端实例身份验证(可选)吗? –

+0

不,您不需要后端身份验证。在nginx访问或错误日志中是否有输出? – minamijoyo

回答

0

根据您的ELB设置和端口映射,$https变量不会永远当你的实例背后的AWS ELB工作。您应该使用HTTP_X_FORWARDED_PROTO头而不是和移动你的重写规则的IF语句来检查的HTTP_X_FORWARDED_PROTO水箱内:

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

+0

它将我重定向到https,但不起作用。 nginx日志中没有错误。在负载平衡器中,我有监听器:1)HTTP 80 - > HTTP 80,2)HTTPS 443 - > HTTP 80.在负载平衡器实例选项卡上,它具有状态'OutOfService'。 –

+0

为什么将HTTPS协议的侦听端口设置为80? HTTPS侦听器应该侦听端口443. –

+0

@KhalidT。 *“HTTPS不会在AWS ELB后面工作”*在这种情况下是适当的建议,但作为参考,[它并不总是如此](http://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb -create-HTTPS-SSL-负载balancer.html)。 –

1

如果你有一个ACM证书,你可以选择从ELB监听的HTTPS/443证书和你不需要费心建立您的nginx实例中的SSL配置。只要有它在端口响应80

你只需要两个HTTP/80和HTTPS在ELB/443实例上的HTTP端口80(此示例使用经典ELB)

enter image description here

如果Cert位于ACM中,当您在SSL证书下选择“更改”时,您应该在下拉菜单中看到它。

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

+0

是的,我做到了。但是@MarkB说我应该配置我的dns设置来使用ELB。我如何在不使用Route53的情况下做到这一点?在我的Godaddy帐户中,我有一个名为'@'并且值'EC2 ip'的A记录。 –

+0

在ELB描述中,您将看到一个DNS记录。尝试直接打它,它应该工作。然后在GoDaddy中使用/创建一个CNAME记录,指向ELB DNS。 –

+0

我已更新我的帖子。请参阅我的域配置。 HTTPS仍然不起作用。当我尝试直接通过ELB DNS名称打开网址时,它将我重定向到https:// ELB_DNS,并显示错误“您的连接不是私人的”,因为它的安全证书来自* .mydomain.com –

相关问题