

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

# 在執行個體設定 HTTPS 終止
<a name="https-singleinstance"></a>

您可以使用[組態檔案](ebextensions.md)，針對將流量傳入您應用程式的代理伺服器進行設定，以終止 HTTPS 連線。若您希望於單一執行個體環境使用 HTTPS，或者將流量設定為透過負載平衡器傳輸而無須解密，此功能十分實用。

若要啟用 HTTPS，您必須允許連接埠 443 的流量傳入運作您 Elastic Beanstalk 應用程式的 EC2 執行個體。您可於組態檔案中使用 `Resources` 金鑰，於 AWSEBSecurityGroup 安全群組傳入規則新增連接埠 443 的規則，達成此目標。

下列片段將傳入規則新增至 `AWSEBSecurityGroup` 安全群組，於單一執行個體環境開放連接埠 443 的所有流量：

**`.ebextensions/https-instance-securitygroup.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
```

在預設 [Amazon Virtual Private Cloud](https://docs.aws.amazon.com/vpc/latest/userguide/) (Amazon VPC) 的負載平衡環境中，您可以將此政策修改為僅接受來自負載平衡器的流量。如需範例，請參閱[在負載平衡的 Elastic Beanstalk 環境中設定端對端加密](configuring-https-endtoend.md)。

**Topics**
+ [在執行 Docker 的 EC2 執行個體上終止 HTTPS](https-singleinstance-docker.md)
+ [在執行 Go 的 EC2 執行個體上終止 HTTPS](https-singleinstance-go.md)
+ [在執行 Java SE 的 EC2 執行個體上終止 HTTPS](https-singleinstance-java.md)
+ [在執行 Node.js 的 EC2 執行個體上終止 HTTPS](https-singleinstance-nodejs.md)
+ [在執行 PHP 的 EC2 執行個體上終止 HTTPS](https-singleinstance-php.md)
+ [在執行 Python 的 EC2 執行個體上終止 HTTPS](https-singleinstance-python.md)
+ [在執行 Ruby 的 EC2 執行個體上終止 HTTPS](https-singleinstance-ruby.md)
+ [在執行 Tomcat 的 EC2 執行個體上終止 HTTPS](https-singleinstance-tomcat.md)
+ [在執行 .NET Core on Linux 的 Amazon EC2 執行個體上終止 HTTPS](https-singleinstance-dotnet-linux.md)
+ [在執行 .NET 的 Amazon EC2 執行個體上終止 HTTPS](SSLNET.SingleInstance.md)

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

針對 Docker 容器，您可使用[組態檔案](ebextensions.md)來啟用 HTTPS。

在您的組態檔案中加入下列的程式碼片段、依照指示來更換憑證與私有金鑰資料，並儲存於原始碼套件的 `.ebextensions` 目錄中。此組態檔案會執行下列任務：
+ `files` 金鑰會於執行個體上建立下列檔案：  
`/etc/nginx/conf.d/https.conf`  
設定 nginx 伺服器。nginx 服務開始後，即會載入此檔案。  
`/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/nginx/conf.d/https.conf:
    mode: "000644"
    owner: root
    group: root
    content: |
      # 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://docker;
          proxy_http_version 1.1;
          
          proxy_set_header Connection "";
          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;
        }
      }
      
  /etc/pki/tls/certs/server.crt:
    mode: "000400"
    owner: root
    group: root
    content: |
      -----BEGIN CERTIFICATE-----
      certificate file contents
      -----END CERTIFICATE-----
      
  /etc/pki/tls/certs/server.key:
    mode: "000400"
    owner: root
    group: root
    content: |
      -----BEGIN RSA PRIVATE KEY-----
      private key contents # See note below.
      -----END RSA PRIVATE KEY-----
```

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

在單一執行個體環境中，您也必須修改執行個體的安全群組，以允許連接埠 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)。

# 在執行 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)。

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

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

所有 AL2023/AL2 平台皆支援統一的代理組態功能。如需在執行 AL2023/AL2 的平台版本上設定代理伺服器的詳細資訊，請參閱 [反向代理組態](platforms-linux-extend.proxy.md)。

在您的組態檔案中加入下列的程式碼片段、依照指示來更換憑證與私有金鑰佔位符，並儲存於 `.ebextensions` 目錄中。此組態檔案會執行下列任務：
+ `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 埠。關於 Java SE 平台上的這些組態檔案，詳細資訊請參閱 [設定代理伺服器](java-se-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)。

# 在執行 Node.js 的 EC2 執行個體上終止 HTTPS
<a name="https-singleinstance-nodejs"></a>

下列範例組態檔案會[延伸預設 nginx 組態](nodejs-platform-proxy.md)，以透過公有憑證與私密金鑰來接聽連接埠 443 並終止 SSL/TLS 連線。

如果您已設定環境來啟用[增強型運作狀態報告](health-enhanced.md)，則需要設定 nginx 來產生存取日誌。若要這麼做，請移除 `# For enhanced health...` 這行註解下方各行開頭的 `#` 字元，以取消註解整個行區塊。

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

```
files:
  /etc/nginx/conf.d/https.conf:
    mode: "000644"
    owner: root
    group: root
    content: |
      # 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;

          # For enhanced health reporting support, uncomment this block:

          #if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
          #    set $year $1;
          #    set $month $2;
          #    set $day $3;
          #    set $hour $4;
          #}
          #access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
          #access_log  /var/log/nginx/access.log  main;
          
          location / {
              proxy_pass  http://nodejs;
              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;
          }
      }
      
  /etc/pki/tls/certs/server.crt:
    mode: "000400"
    owner: root
    group: root
    content: |
      -----BEGIN CERTIFICATE-----
      certificate file contents
      -----END CERTIFICATE-----
      
  /etc/pki/tls/certs/server.key:
    mode: "000400"
    owner: root
    group: root
    content: |
      -----BEGIN RSA PRIVATE KEY-----
      private key contents # See note below.
      -----END RSA PRIVATE KEY-----
```

`files` 金鑰會於執行個體上建立下列檔案：

`/etc/nginx/conf.d/https.conf`  
設定 nginx 伺服器。nginx 服務開始後，即會載入此檔案。

`/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`  
在執行個體上建立私有金鑰。將*私密金鑰內容*取代為用於建立憑證請求或自我簽署憑證的私密金鑰內容。

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

在單一執行個體環境中，您也必須修改執行個體的安全群組，以允許連接埠 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)。

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

以 PHP 容器類型而言，您可使用[組態檔案](ebextensions.md)，讓 Apache HTTP Server 使用 HTTPS。

在您的組態檔案中加入下列的程式碼片段、依照指示來更換憑證與私有金鑰資料，並儲存於原始碼套件的 `.ebextensions` 目錄中。

此組態檔案會執行下列任務：
+ `packages` 金鑰使用 yum 來安裝 `mod24_ssl`。
+ `files` 金鑰會於執行個體上建立下列檔案：  
`/etc/httpd/conf.d/ssl.conf`  
設定 Apache 伺服器。Apache 服務開始後，即會載入此檔案。  
`/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**  

```
packages:
  yum:
    mod24_ssl : []

files:
  /etc/httpd/conf.d/ssl.conf:
    mode: "000644"
    owner: root
    group: root
    content: |
      LoadModule ssl_module modules/mod_ssl.so
      Listen 443
      <VirtualHost *:443>
        <Proxy *>
          Order deny,allow
          Allow from all
        </Proxy>

        SSLEngine             on
        SSLCertificateFile    "/etc/pki/tls/certs/server.crt"
        SSLCertificateKeyFile "/etc/pki/tls/certs/server.key"
        SSLCipherSuite        EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH
        SSLProtocol           All -SSLv2 -SSLv3
        SSLHonorCipherOrder   On
        SSLSessionTickets     Off
        
        Header always set Strict-Transport-Security "max-age=63072000; includeSubdomains; preload"
        Header always set X-Frame-Options DENY
        Header always set X-Content-Type-Options nosniff
        
        ProxyPass / http://localhost:80/ retry=0
        ProxyPassReverse / http://localhost:80/
        ProxyPreserveHost on
        RequestHeader set X-Forwarded-Proto "https" early
        
      </VirtualHost>
      
  /etc/pki/tls/certs/server.crt:
    mode: "000400"
    owner: root
    group: root
    content: |
      -----BEGIN CERTIFICATE-----
      certificate file contents
      -----END CERTIFICATE-----
      
  /etc/pki/tls/certs/server.key:
    mode: "000400"
    owner: root
    group: root
    content: |
      -----BEGIN RSA PRIVATE KEY-----
      private key contents # See note below.
      -----END RSA PRIVATE KEY-----
```

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

在單一執行個體環境中，您也必須修改執行個體的安全群組，以允許連接埠 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)。

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

透過 Web 伺服器閘道界面 (WSGI) 使用 Apache HTTP 伺服器的 Python 容器類型，您可使用[組態檔案](ebextensions.md)，讓 Apache HTTP 伺服器能夠使用 HTTPS。

在您的[組態檔案](ebextensions.md)中加入下列的程式碼片段、依照指示來更換憑證與私密金鑰資料，並儲存於原始碼套件的 `.ebextensions` 目錄中。此組態檔案會執行下列任務：
+ `packages` 金鑰使用 yum 來安裝 `mod_ssl`。
+ `files` 金鑰會於執行個體上建立下列檔案：  
`/etc/httpd/conf.d/ssl.conf`  
設定 Apache 伺服器。若您的應用程式名稱不是 `application.py`，請將 `WSGIScriptAlias` 值中反白顯示的文字取代為應用程式的本機路徑。例如，django 應用程式的路徑可能為 `django/wsgi.py`。此位置應與您為環境設定的 `WSGIPath` 選項的值相符。  
依據您的應用程式需求，可能需要將其他目錄新增至 **python-path** 參數。  
`/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`  
在執行個體上建立私有金鑰。將*私密金鑰內容*取代為用於建立憑證請求或自我簽署憑證的私密金鑰內容。
+ `container_commands` 金鑰會在所有項目設定完成後停止 httpd 服務，讓服務能夠使用新的 `https.conf` 檔案和憑證。

**注意**  
這個範例只能使用 [Python](create-deploy-python-container.md) 平台在環境中運作。

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

```
packages:
  yum:
    mod_ssl: []
    
files:
  /etc/httpd/conf.d/ssl.conf:
    mode: "000644"
    owner: root
    group: root
    content: |
      LoadModule wsgi_module modules/mod_wsgi.so
      WSGIPythonHome /var/app/venv/staging-LQM1lest
      WSGISocketPrefix run/wsgi
      WSGIRestrictEmbedded On
      Listen 443
      <VirtualHost *:443>
        SSLEngine on
        SSLCertificateFile "/etc/pki/tls/certs/server.crt"
        SSLCertificateKeyFile "/etc/pki/tls/certs/server.key"
        
        Alias /static/ /var/app/current/static/
        <Directory /var/app/current/static>
        Order allow,deny
        Allow from all
        </Directory>
        
        WSGIScriptAlias / /var/app/current/application.py
        
        <Directory /var/app/current>
        Require all granted
        </Directory>
        
        WSGIDaemonProcess wsgi-ssl processes=1 threads=15 display-name=%{GROUP} \
          python-path=/var/app/current \
          python-home=/var/app/venv/staging-LQM1lest \
          home=/var/app/current \
          user=webapp \
          group=webapp
        WSGIProcessGroup wsgi-ssl
        
      </VirtualHost>
      
  /etc/pki/tls/certs/server.crt:
    mode: "000400"
    owner: root
    group: root
    content: |
      -----BEGIN CERTIFICATE-----
      certificate file contents
      -----END CERTIFICATE-----
       
  /etc/pki/tls/certs/server.key:
    mode: "000400"
    owner: root
    group: root
    content: |
      -----BEGIN RSA PRIVATE KEY-----
      private key contents # See note below.
      -----END RSA PRIVATE KEY-----
      
container_commands:
  01killhttpd:
    command: "killall httpd"
  02waitforhttpddeath:
    command: "sleep 3"
```

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

**Amazon Linux 2023 環境的注意事項**  
在 Amazon Linux 2023 上，`mod_wsgi`必須單獨安裝，因為它在套件儲存庫中不可用。如需安裝說明，請參閱 PyPI 上的 [mod\$1wsgi](https://pypi.org/project/mod-wsgi/)。

在單一執行個體環境中，您也必須修改執行個體的安全群組，以允許連接埠 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)。

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

以 Ruby 容器類型而言，您啟用 HTTPS 的方式取決於使用的應用程式伺服器類型。

**Topics**
+ [針對搭配 Puma 的 Ruby 設定 HTTPS](#Puma)
+ [針對搭配 Passenger 的 Ruby 設定 HTTPS](#Passenger)

## 針對搭配 Puma 的 Ruby 設定 HTTPS
<a name="Puma"></a>

以使用 Puma 做為應用程式伺服器的 Ruby 容器類型而言，您可使用[組態檔案](ebextensions.md)來啟用 HTTPS。

在您的組態檔案中加入下列的程式碼片段、依照指示來更換憑證與私有金鑰資料，並儲存於原始碼套件的 `.ebextensions` 目錄中。此組態檔案會執行下列任務：
+ `files` 金鑰會於執行個體上建立下列檔案：  
`/etc/nginx/conf.d/https.conf`  
設定 nginx 伺服器。nginx 服務開始後，即會載入此檔案。  
`/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/nginx/conf.d/https.conf:
    content: |
      # 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://my_app;
              proxy_set_header        Host            $host;
              proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
              proxy_set_header        X-Forwarded-Proto https;
          }

          location /assets {
            alias /var/app/current/public/assets;
            gzip_static on;
            gzip on;
            expires max;
            add_header Cache-Control public;
          }

          location /public {
            alias /var/app/current/public;
            gzip_static on;
            gzip on;
            expires max;
            add_header Cache-Control public;
          }
      }

  /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)。

在單一執行個體環境中，您也必須修改執行個體的安全群組，以允許連接埠 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)。

## 針對搭配 Passenger 的 Ruby 設定 HTTPS
<a name="Passenger"></a>

以使用 Passenger 做為應用程式伺服器的 Ruby 容器類型而言，您可同時使用組態檔案和 JSON 檔案來啟用 HTTPS。

**欲針對搭配 Passenger 的 Ruby 設定 HTTPS**

1. 在您的組態檔案中加入下列的程式碼片段、依照指示來更換憑證與私有金鑰資料，並儲存於原始碼套件的 `.ebextensions` 目錄中。此組態檔案會執行下列任務：
   + `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 針對搭配 Passenger 之 Ruby 設定 HTTPS 的 .Ebextensions 程式碼片段**  

   ```
   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)。

1. 建立一個文字檔案，並將下列 JSON 新增至檔案。將其儲存於您原始碼套件的根目錄，並取名為 `passenger-standalone.json`。此 JSON 檔案會將 Passenger 設定為使用 HTTPS。
**重要**  
此 JSON 檔案不得包含位元組順序記號 (BOM)。否則，Passenger JSON 程式庫將無法正確讀取檔案，且 Passenger 服務不會啟動。  
**Example passenger-standalone.json**  

   ```
   {
     "ssl" : true,
     "ssl_port" : 443,
     "ssl_certificate" : "/etc/pki/tls/certs/server.crt",
     "ssl_certificate_key" : "/etc/pki/tls/certs/server.key"
   }
   ```

在單一執行個體環境中，您也必須修改執行個體的安全群組，以允許連接埠 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)。

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

以 Tomcat 容器類型而言，您須使用[組態檔案](ebextensions.md)，讓 Apache HTTP Server 做為 Tomcat 的反向代理程式時能夠使用 HTTPS。

在您的組態檔案中加入下列的程式碼片段、依照指示來更換憑證與私有金鑰資料，並儲存於原始碼套件的 `.ebextensions` 目錄中。此組態檔案會執行下列任務：
+ `files` 金鑰會於執行個體上建立下列檔案：  
`/etc/pki/tls/certs/server.crt`  
在執行個體上建立憑證檔案。將*憑證檔案內容*取代為您的憑證內容。  
YAML 憑藉一致的縮排。請在取代範例組態檔中的內容時，讓縮排層級一致，並確認您的文字編輯器使用空格而非定位字元進行縮排。  
`/etc/pki/tls/certs/server.key`  
在執行個體上建立私有金鑰。將*私密金鑰內容*取代為用於建立憑證請求或自我簽署憑證的私密金鑰內容。  
`/opt/elasticbeanstalk/hooks/appdeploy/post/99_start_httpd.sh`  
建立後部署勾點指令碼來重新啟動 httpd 服務。

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

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

  /opt/elasticbeanstalk/hooks/appdeploy/post/99_start_httpd.sh:
    mode: "000755"
    owner: root
    group: root
    content: |
      #!/usr/bin/env bash
      sudo service httpd restart
```

您還必須設定您環境的代理伺服器以監聽連接埠 443。下列 Apache 2.4 組態會在 443 埠上新增接聽程式。如需進一步了解，請參閱[設定代理伺服器](java-tomcat-proxy.md)。

**Example .ebextensions/httpd/conf.d/ssl.conf**  

```
Listen 443
<VirtualHost *:443> 
  ServerName server-name
  SSLEngine on 
  SSLCertificateFile "/etc/pki/tls/certs/server.crt" 
  SSLCertificateKeyFile "/etc/pki/tls/certs/server.key" 

  <Proxy *> 
    Require all granted 
  </Proxy> 
  ProxyPass / http://localhost:8080/ retry=0 
  ProxyPassReverse / http://localhost:8080/ 
  ProxyPreserveHost on 

  ErrorLog /var/log/httpd/elasticbeanstalk-ssl-error_log 

</VirtualHost>
```

您的憑證廠商可能包括您可安裝的中繼憑證，提升行動用戶端的相容性。請將下列新增至您的 SSL 組態檔案 (參閱 [擴展和覆寫預設的 Apache 組態 — Amazon Linux AMI (AL1)](java-tomcat-proxy.md#java-tomcat-proxy-apache) 了解位置)，藉此透過中繼憑證授權機構 (CA) 套件組合設定 Apache：
+ 在 `ssl.conf` 檔案內容指定鏈檔案：

  ```
  SSLCertificateKeyFile "/etc/pki/tls/certs/server.key"
  SSLCertificateChainFile "/etc/pki/tls/certs/gd_bundle.crt"
  SSLCipherSuite        EECDH+AESGCM:EDH+AESGCM:AES256+EECDH:AES256+EDH
  ```
+ 使用中繼憑證的內容在 `files` 金鑰新增項目：

  ```
  files:
    /etc/pki/tls/certs/gd_bundle.crt:
      mode: "000400"
      owner: root
      group: root
      content: |
        -----BEGIN CERTIFICATE-----
        First intermediate certificate
        -----END CERTIFICATE-----
        -----BEGIN CERTIFICATE-----
        Second intermediate certificate
        -----END CERTIFICATE-----
  ```

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

在單一執行個體環境中，您也必須修改執行個體的安全群組，以允許連接埠 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)。

# 在執行 .NET Core on Linux 的 Amazon EC2 執行個體上終止 HTTPS
<a name="https-singleinstance-dotnet-linux"></a>

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

在您的組態檔案中加入下列的程式碼片段、依照指示來更換憑證與私有金鑰佔位符，並儲存於 `.ebextensions` 目錄中。此組態檔案會執行下列任務：
+ `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` 目錄中的檔案，該檔案的副檔名為 `.platform/nginx/conf.d/` (例如 `.platform/nginx/conf.d/https.conf`)。將 *app\$1port* 換成您應用程式接聽的埠號。本範例設定 nginx 伺服器使用 SSL 來接聽 443 埠。如需 .NET Core on Linux 平台上這些組態檔案的詳細資訊，請參閱[設定代理伺服器](dotnet-linux-platform-nginx.md)。

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

```
# HTTPS server

server {
    listen       443 ssl;
    server_name  localhost;
    
    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)。

# 在執行 .NET 的 Amazon EC2 執行個體上終止 HTTPS
<a name="SSLNET.SingleInstance"></a>

下列的[組態檔案](ebextensions.md)會建立和執行 Windows PowerShell 指令碼，此指令碼會執行下列任務：
+ 檢查現有的 HTTPS 憑證是否繫結至連接埠 443。
+ 從 Amazon S3 儲存貯體取得 [PFX 憑證](configuring-https-ssl.md)。
**注意**  
將 `AmazonS3ReadOnlyAccess`政策新增至 `aws-elasticbeanstalk-ec2-role`，以存取 Amazon S3 儲存貯體中的 SSL 憑證。
+ 從 取得密碼 AWS Secrets Manager。
**注意**  
在 中新增陳述式`aws-elasticbeanstalk-ec2-role`，允許包含憑證密碼之秘密`secretsmanager:GetSecretValue`的動作
+ 安裝憑證。
+ 將憑證繫結至連接埠 443。
**注意**  
若要移除 HTTP 端點 (連接埠 80)，請將 `Remove-WebBinding` 命令納入此範例 **Remove the HTTP binding (移除 HTTP 綁定)** 部分之下。

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

```
files:
  "C:\\certs\\install-cert.ps1":
    content: |
      import-module webadministration
      ## Settings - replace the following values with your own
      $bucket = "amzn-s3-demo-bucket"  ## S3 bucket name
      $certkey = "example.com.pfx"    ## S3 object key for your PFX certificate
      $secretname = "example_secret"  ## AWS Secrets Manager name for a secret that contains the certificate's password
      ##

      # Set variables
      $certfile = "C:\cert.pfx"
      $pwd = Get-SECSecretValue -SecretId $secretname | select -expand SecretString

      # Clean up existing binding
      if ( Get-WebBinding "Default Web Site" -Port 443 ) {
        Echo "Removing WebBinding"
        Remove-WebBinding -Name "Default Web Site" -BindingInformation *:443:
      }
      if ( Get-Item -path IIS:\SslBindings\0.0.0.0!443 ) {
        Echo "Deregistering WebBinding from IIS"
        Remove-Item -path IIS:\SslBindings\0.0.0.0!443
      }

      # Download certificate from S3
      Read-S3Object -BucketName $bucket -Key $certkey -File $certfile
      
      # Install certificate
      Echo "Installing cert..."
      $securepwd = ConvertTo-SecureString -String $pwd -Force -AsPlainText
      $cert = Import-PfxCertificate -FilePath $certfile cert:\localMachine\my -Password $securepwd
      
      # Create site binding
      Echo "Creating and registering WebBinding"
      New-WebBinding -Name "Default Web Site" -IP "*" -Port 443 -Protocol https
      New-Item -path IIS:\SslBindings\0.0.0.0!443 -value $cert -Force
      
      ## Remove the HTTP binding
      ## (optional) Uncomment the following line to unbind port 80
      # Remove-WebBinding -Name "Default Web Site" -BindingInformation *:80:
      ##
      
      # Update firewall
      netsh advfirewall firewall add rule name="Open port 443" protocol=TCP localport=443 action=allow dir=OUT

commands:
  00_install_ssl:
    command: powershell -NoProfile -ExecutionPolicy Bypass -file C:\\certs\\install-cert.ps1
```

在單一執行個體環境中，您也必須修改執行個體的安全群組，以允許連接埠 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)。