amazon-web-services - 使用 Amazon Linux 2 为使用 AWS Elastic Beanstalk 托管的 Java Spring Boot 应用程序添加 SSL 证书

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

我正在使用没有 Loadbalancer 的 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 配置?

最佳答案

以下对我有用。在项目的根目录中,创建如下图所示的目录和文件:
enter image description here
除了 AWS documentation 中提到的步骤将以下内容添加到 Procfile创建于 aws文件夹: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.jar运行以下命令后创建:mvn clean package编辑:您应该在 https-instance.config 末尾添加 nginx 服务器重启命令每次上传新的构建文件时重新启动服务器的文件,如下所示:
container_commands:
      01restart_nginx:
        command: "service nginx restart"

关于amazon-web-services - 使用 Amazon Linux 2 为使用 AWS Elastic Beanstalk 托管的 Java Spring Boot 应用程序添加 SSL 证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63926402/

相关文章:

java - 在 Spring Boot 应用程序中调用 HTTPS SOAP Web 服务

ruby-on-rails-3 - OpenSSL + Passenger + RVM

ssl - 谷歌云 sql 使用错误的 ssl 证书从 kubernetes 建立套接字连接

SSL 证书验证在特定服务器上的 docker 容器内失败

mysql - AWS : Unable to connect Amazon QuickSight to RDS

ios - 如何使用适用于 iOS 的 AWS 设备场将警报自动接受设置为真/假?

amazon-web-services - AWS : How to enable CORS in API Gateway using Cloudformation?

amazon-web-services - s3 :x-amz-acl with a value of "bucket-owner-full-control" do/mean? 是什么

hibernate - 使用 Spring Data JPA 删除 SpringBoot 2.0.5.RELEASE 应用程序中的对象

java - AWS - Ubuntu 用户 session 停止 Spring Boot 应用程序