gpt4 book ai didi

azure - 为 Nginx docker 容器启用 HTTPS

转载 作者:行者123 更新时间:2023-12-03 04:03:04 27 4
gpt4 key购买 nike

对于开发环境,我需要启用与 Nginx 容器的 SSL 连接。

我正在获取从 Azure Key Vault 生成的证书及其 PFX 格式。

我一直在尝试在我的 nginx 容器中安装 opensll 并从 PFX 生成 crt 和 key 文件,但没有任何运气。

启用与 NGINX 容器的 HTTPS 连接的最简单方法是什么?

这是我现在无法使用的 dockerfile。

FROM nginx:1.17.1-alpine
ARG CERT
RUN apk upgrade --update-cache --available && \
apk add openssl && \
rm -rf /var/cache/apk/*

RUN echo "$CERT"
RUN test -z "$CERT" || echo "$CERT" && echo "no certificates setup"
COPY cert.pfx /opt/certificate.pfx
RUN mkdir /etc/nginx/certs

RUN test -z "$CERT" || openssl pkcs12 -in /certificate.pfx -nocerts -out /etc/nginx/certs/cert.key -password pass:FakePassword && :
RUN test -z "$CERT" || openssl pkcs12 -in /certificate.pfx -clcerts -nokeys -out /etc/nginx/certs/cert.crt -password pass:FakePassword && :
RUN test -z "$CERT" || echo $'server { \n\
\tlisten 443 ssl; \n\
\tserver_name www.yoursite.com; \n\
\tssl_certificate /etc/nginx/certs/cert.crt; \n\
\tssl_certificate_key /etc/nginx/certs/cert.key; \n\
\tlocation / { \n\
\t\tproxy_pass http://frontend:5000/; \n\
\t\terror_log /var/log/front_end_errors.log; \n\
\t} \n\
}' >> /etc/nginx/conf.d/nginx.conf
EXPOSE 80 443

COPY --from=build /app/dist /usr/share/nginx/html/

我的错误是:

Step 18/25 : RUN if [ "x$CERT" = "x" ] ; then : ; else openssl pkcs12 -in /opt/certificate.pfx -nocerts -out /etc/nginx/certs/cert.key ; fi
---> Running in f5d8b255e51c
Enter Import Password:
Can't read Password
The command '/bin/sh -c if [ "x$CERT" = "x" ] ; then : ; else openssl pkcs12 -in /opt/certificate.pfx -nocerts -out /etc/nginx/certs/cert.key ; fi' returned a non-zero code: 1

最佳答案

可能不是您想听到的,但您提出的解决方案可能是危险的,也可能是非常危险的,具体取决于用例。您所做的是将证书烘焙到图像本身中。证书和其他 secret 应作为 Docker 卷安装,因此在您的情况下,制作 /etc/nginx/certs 卷将更好地解决您的问题

有多种方法可以将 SSL 证书放入容器中,具体取决于您编排容器的方式。由于您使用“Azure”标记,我假设您使用 Azure Kubernetes 服务或 Azure 容器实例。在 AKS 中,有多种方法可以执行此操作,但常见的方法是创建一个 init 容器,授予其访问 key 保管库的权限,并将 key 直接从 key 保管库加载到共享 (tmpfs) 卷中。

在容器实例中,您可以通过将容器权限分配给 key 保管库,在运行时直接从 key 保管库读取 key (类似于在 AKS 中执行此操作的方式),然后在 Docker 中创建入口点脚本将证书下载到临时卷然后启动 nginx 的镜像。

无论您选择哪种方式:永远、永远不会将 key 保管库中的 secret 保留到任何类型的存储中。

参见this page有关如何使用 curl 在运行时从容器内的 Azure key 保管库下载 secret 的示例。

关于azure - 为 Nginx docker 容器启用 HTTPS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61331056/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com