AWS Elastic Beanstalk通过单实例配置https

在 AWS Elastic Beanstalk 上提供了多种方式来实现 https,比如通过单实例配置,负载均衡配置等。今天就以 nodejs + express 为例说一下怎么通过单实例配置实现https

创建demo应用

创建一个简单的 nodejs + express 应用 myapp,其中包含两个文件 package.json 和 app.js。

package.json

shell 复制代码
{
  "name": "myapp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node app.js",
    "dev": "nodemon app.js"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "express": "^4.18.2",
    "nodemon": "^3.0.3"
  }
}

app.js

shell 复制代码
const express = require('express');

const app = express();

const port = process.env.PORT || 3000;

app.get("/", (req, res, next) => {
  res.send("Express Server");
});

app.listen(port, () => {
  console.log(`[server]: Server is running at http://localhost:${port}`);
});

配置https

.ebextensions/https-instance.config

在应用目录下创建 ".ebextensions/https-instance.config" 文件,内容如下

shell 复制代码
files:
  # Public certificate
  /etc/pki/tls/certs/server.crt:
    mode: "000400"
    owner: root
    group: root
    content: |
      -----BEGIN CERTIFICATE-----
      ...
      -----END CERTIFICATE-----

  # Private key
  /etc/pki/tls/certs/server.key:
    mode: "000400"
    owner: root
    group: root
    content: |
      -----BEGIN PRIVATE KEY-----
      ...
      -----END PRIVATE KEY-----

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

其中证书和私钥的内容可以从证书和私钥文件获取。

.platform/nginx/conf.d/https.conf

在应用目录下创建 ".platform/nginx/conf.d/https.conf" 文件,内容如下

shell 复制代码
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;
        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;
    }
}

部署

在应用目录下将所有元文件打包,然后按照正常应用部署的方式部署就可以了,只是其中 "Capacity -> Auto scaling group -> Environment type" 要选成 "Single instance"。

然后就可以通过 https://xxx.elasticbeanstalk.com/ 访问了。

相关推荐
BIBI20494 小时前
HTTPS 证书的核心原理解释
网络安全·https·ssl/tls·入门指南·web 开发
Johny_Zhao1 天前
Keepalived与Nginx架构选型
linux·nginx·信息安全·云计算·apache·shell·yum源·keepalive·系统运维
流浪0014 天前
计算机网络篇25:应用层——HTTPS 协议原理:非对称加密协商密钥,CA 证书挡住中间人
网络协议·计算机网络·https·非对称加密·对称加密·ca证书
wdfk_prog5 天前
LWIP教程 33:从 `https_ex_init()` 到 `http_recv()`——altcp、mbedTLS、TLS Handshake 与 HTTPS 数据通路
网络协议·http·https
xbzb7 天前
用 Secret + ConfigMap + PV/PVC 部署 WordPress:一套能跑 https 的最小组合
http·https·wordpress·configmap·secret·pod
66IT6610 天前
Veeam ONE v13.1 发布:全新 HTML5 Web 客户端与 PostgreSQL 报告库
云计算·虚拟化·监控·数据保护·veeam
2601_9679801111 天前
Python 与云计算:AWS、GCP、Azure 实践
python·云计算·azure·aws·gcp
cc57250265312 天前
云计算技术应用专业大学期间考证怎么安排?
云计算
VIP_CQCRE13 天前
AI 子站域名能打开就算上线?一份 DNS、登录与计费验收清单
https·dns·ai应用·网站运维·acedatacloud