2017-02-11 129 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 –

相關問題