【问题标题】:Add SSL certificate for Java Spring Boot App hosted using AWS Elastic Beanstalk using Amazon Linux 2使用 Amazon Linux 2 为使用 AWS Elastic Beanstalk 托管的 Java Spring Boot 应用程序添加 SSL 证书
【发布时间】:2021-01-03 15:50:58
【问题描述】:

我正在使用没有负载均衡器的 AWS Elastic-beanstalk 作为我的 Java Spring Boot 应用程序的服务器。我想添加 SSL 证书文件并更新 nginx 配置以在我的 Web 应用上接受 SSL 流量。

我不希望使用负载平衡器,因为我不想产生额外的每月费用。

我目前的部署过程是在使用以下命令创建 JAR 文件后:

`mvn 清洁包' 我从 AWS 控制台上传 jar 文件。

使用 AWS documentation,我们可以使用以下语法添加自定义文件:

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-----

container_commands:
  01restart_nginx:
    command: "service nginx restart"

并更新 NGINX 配置,在以下路径中添加更新的配置: ebextensions/nginx/conf.d/https.conf

但是,我的证书文件和 Nginx 配置没有更新。

到目前为止我所做的尝试:

点击此链接:

Spring Boot + Elastic Beanstalk .ebextensions in JAR

它会更新 EC2 实例中的证书文件,但不会更新 NGINX,并且部署也会失败并出现以下错误。

Application deployment failed at 2020-09-16T08:43:16Z with exit status 1 and error: Engine execution has encountered an error.
Incorrect application version "system-backend-source-28" (deployment 33). Expected version "system-backend-source-27" (deployment 32).

我使用的是 Amazon Linux 2 因此,我没有将配置文件放在 .ebextension 文件夹中,而是遵循了这个答案并将 NGINX 配置文件放在 .platform 目录中,但部署通过但不更新新的 NGINX 配置。它也不上传证书文件。

How to extend nginx config in elastic beanstalk (Amazon Linux 2)

如果我手动编辑 Nginx 配置并更新证书文件,我的实例使用 HTTPS 成功运行。但正如人们所看到的,这仍然是手动的,实际上这样做消除了使用 Elastic beanstalk 的目的。有没有办法在部署时自动上传证书文件并更新 NGINX 配置?

【问题讨论】:

标签: amazon-web-services spring-boot ssl nginx amazon-elastic-beanstalk


【解决方案1】:

以下内容对我有用。在项目的根目录中,创建如下图所示的目录和文件:

除了AWS documentation 中提到的步骤之外,将以下内容添加到aws 文件夹中创建的Procfile

web: java -jar demo-0.0.1-SNAPSHOT.jar

在您的pom.xml 文件中,更新<configuration>,如下所示:

<configuration>
    <tasks>

       <property name="buildName" value="${project.build.finalName}.jar"/>

        <copy todir="${project.build.directory}/aws-build/" overwrite="false">
           <fileset file="${project.build.directory}/${project.build.finalName}.jar"/>
           <fileset dir="./aws" />
        </copy>

        <replace file="${project.build.directory}/aws-build/Procfile" token="@jarname@" value="${buildName}"/>

        <zip compress="false" destfile="${project.build.directory}/aws-build/app-to-deploy.jar" basedir="${project.build.directory}/aws-build"/>

    </tasks>
</configuration>

这将确保您的证书捆绑在构建中,并在您上传新构建时上传。对于我的特殊情况,然后我上传运行以下命令后创建的app-to-deploy.jarmvn clean package

编辑:您应该在 https-instance.config 文件的末尾添加 nginx server restart 命令,以在每次上传新构建时重新启动服务器,如下所示:

container_commands:
      01restart_nginx:
        command: "service nginx restart"

【讨论】:

  • 你的src文件夹在aws文件夹下吗?
  • 你能帮我解决这个问题吗? stackoverflow.com/questions/69755630/…
  • 我的 aws 文件夹与 src 平行
  • 另外,maven中的配置标签,在我使用spring boot maven插件时应该放在哪里?
猜你喜欢
  • 1970-01-01
  • 2017-08-07
  • 2019-04-20
  • 2017-09-28
  • 2021-04-30
  • 2019-08-13
  • 2021-02-03
  • 2019-03-08
  • 2016-11-20
相关资源
最近更新 更多