运行 Nginx Docker 带有 SSL 自签名证书

Running Nginx Docker with SSL self signed certificate

我正在尝试 运行 使用 Docker 的 UI 应用程序使用 nginx 图像 我可以毫无问题地访问端口 80 上的服务但是每当我尝试访问它时通过 443 端口上的 https 我无法访问网站不断加载的应用程序,最终导致无法访问我已经更新了 default.conf 中的 nginx.conf 文件以允许通过端口 443

进行访问

以下是我的nginx.conf

charset utf-8;

server {
    listen 80;
    server_name localhost;
    root /usr/nginx/html;
}

server {
    listen 443;
    server_name localhost;
    root /usr/nginx/html; 
}

我已经在 /usr/nginx 文件夹中添加了 SSL 自签名证书,并通过 Dockerfile

公开了端口 443

以下是我的Docker文件

FROM nginx

COPY dist /usr/nginx/html
RUN chmod -R 777 /usr/nginx/html/*

COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY domain.crt /usr/nginx

EXPOSE 80:443
ENTRYPOINT nginx -g 'daemon off;'

任何人都可以解释一下是端口 443 不允许任何访问

为了使 nginx 服务器允许 SSL 加密,您需要在侦听时提供 ssl 标志 nginx.conf 只有 ssl 证书是不够的,您还需要 ssl 证书密钥和密码,并且必须对其进行配置

charset utf-8;

server {
    listen 80;
    server_name localhost;
    root /usr/share/nginx/html;
}

server {
    listen 443 ssl;
    ssl_certificate /usr/nginx/ssl.crt;
    ssl_certificate_key /usr/nginx/ssl.key;
    ssl_password_file /usr/nginx/ssl.pass;
    server_name localhost;
    root /usr/nginx/html;
}

并且你需要通过卷或通过嵌入 docker 容器来放置 ssl 证书、密钥和密码,如果你是 运行 容器在 kubernetes 集群上,通过 kubernetes secrets 添加它们将是更好的选择 对于 Docker 文件,您可以添加 like

FROM nginx

COPY dist /usr/nginx/html
RUN chmod -R 777 /usr/nginx/html/*

COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY ssl.crt /usr/nginx/
COPY ssl.pass /usr/nginx/
COPY ssl.key /usr/nginx/

EXPOSE 80:443
ENTRYPOINT nginx -g 'daemon off;'

有关更多信息,您可以参考 Nginx Docker 文章 https://medium.com/@agusnavce/nginx-server-with-ssl-certificates-with-lets-encrypt-in-docker-670caefc2e31