

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

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