

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 在執行 Go 的 EC2 執行個體上終止 HTTPS
<a name="https-singleinstance-go"></a>

如果是 Go 容器類型，您可使用[組態檔案](ebextensions.md)來啟用 HTTPS，以及使用 nginx 組態檔案來設定 nginx 伺服器使用 HTTPS。

在您的組態檔案中加入下列的程式碼片段、依照指示來更換憑證與私有金鑰佔位符，並儲存於原始碼套件的 `.ebextensions` 目錄中。此組態檔案會執行下列任務：
+ `Resources` 金鑰可於連接埠 443 上啟用安全群組，供您環境的執行個體使用。
+ `files` 金鑰會於執行個體上建立下列檔案：  
`/etc/pki/tls/certs/server.crt`  
在執行個體上建立憑證檔案。將*憑證檔案內容*取代為您的憑證內容。  
YAML 憑藉一致的縮排。請在取代範例組態檔中的內容時，讓縮排層級一致，並確認您的文字編輯器使用空格而非定位字元進行縮排。
如果您有中繼憑證，請在網站憑證後將其納入 `server.crt` 中。  

  ```
        -----BEGIN CERTIFICATE-----
    certificate file contents
    -----END CERTIFICATE-----
    -----BEGIN CERTIFICATE-----
    first intermediate certificate
    -----END CERTIFICATE-----
    -----BEGIN CERTIFICATE-----
    second intermediate certificate
    -----END CERTIFICATE-----
  ```  
`/etc/pki/tls/certs/server.key`  
在執行個體上建立私有金鑰。將*私密金鑰內容*取代為用於建立憑證請求或自我簽署憑證的私密金鑰內容。

**Example .ebextensions/https-instance.config**  

```
files:
  /etc/pki/tls/certs/server.crt:
    content: |
      -----BEGIN CERTIFICATE-----
      certificate file contents
      -----END CERTIFICATE-----
      
  /etc/pki/tls/certs/server.key:
    content: |      
      -----BEGIN RSA PRIVATE KEY-----
      private key contents # See note below.
      -----END RSA PRIVATE KEY-----
```

**注意**  
請避免將包含您私有金鑰的組態檔遞交到原始檔控制。當您測試好組態並確認其正常運作後，請將您的私有金鑰儲存在 Amazon S3，然後修改組態，以在部署期間予以下載。如需說明，請參閱[將私有金鑰安全地儲存於 Amazon S3 中](https-storingprivatekeys.md)。

將下列內容加入您原始碼套件 `.conf` 目錄中的檔案，該檔案的副檔名為 `.ebextensions/nginx/conf.d/` (例如 `.ebextensions/nginx/conf.d/https.conf`)。將 *app\$1port* 換成您應用程式接聽的埠號。本範例設定 nginx 伺服器使用 SSL 來接聽 443 埠。關於 Go 平台上的這些組態檔案，詳細資訊請參閱 [設定代理伺服器](go-nginx.md)。

**Example .ebextensions/nginx/conf.d/https.conf**  

```
# HTTPS server

server {
    listen       443;
    server_name  localhost;
    
    ssl                  on;
    ssl_certificate      /etc/pki/tls/certs/server.crt;
    ssl_certificate_key  /etc/pki/tls/certs/server.key;
    
    ssl_session_timeout  5m;
    
    ssl_protocols  TLSv1 TLSv1.1 TLSv1.2;
    ssl_prefer_server_ciphers   on;
    
    location / {
        proxy_pass  http://localhost:app_port;
        proxy_set_header   Connection "";
        proxy_http_version 1.1;
        proxy_set_header        Host            $host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header        X-Forwarded-Proto https;
    }
}
```

在單一執行個體環境中，您也必須修改執行個體的安全群組，以允許連接埠 443 上的流量。下列組態檔案會使用 CloudFormation [函數](ebextensions-functions.md)擷取安全群組的 ID，並將規則新增至其中。

**Example .ebextensions/https-instance-single.config**  

```
Resources:
  sslSecurityGroupIngress: 
    Type: AWS::EC2::SecurityGroupIngress
    Properties:
      GroupId: {"Fn::GetAtt" : ["AWSEBSecurityGroup", "GroupId"]}
      IpProtocol: tcp
      ToPort: 443
      FromPort: 443
      CidrIp: 0.0.0.0/0
```

針對使用負載平衡的環境，您應設定負載平衡器，使其[以未處理之方式通過安全流量](https-tcp-passthrough.md)，或針對端點對端點加密進行[解密與重新加密](configuring-https-endtoend.md)。