【问题标题】:Docker nginx self-signed certificate - can't connect to httpsDocker nginx自签名证书-无法连接到https
【发布时间】:2023-03-02 21:56:02
【问题描述】:

我一直在学习一些教程来尝试让我的 SSL 证书与我的 docker 环境一起使用。我决定采用letsencrypt的自签名证书的路线。我已经使用以下命令生成了证书

certbot certonly --manual \
  --preferred-challenges=dns \
  --email {email_address} \
  --server https://acme-v02.api.letsencrypt.org/directory \
  --agree-tos \
  --manual-public-ip-logging-ok \
  -d "*.servee.co.uk"

注意:我正在使用多租户,因此我需要域中的通配符

这可行,证书已在我的服务器上生成。我现在正在尝试将它与我的 docker nginx 容器一起使用。

我的 docker-compose.yml 文件看起来像这样

...
services:
  nginx:
    build:
      context: docker/nginx
      dockerfile: Dockerfile
    ports:
      - 433:433
      - 80:80
    volumes:
      - ./src:/var/www/html:delegated
    depends_on:
      - app
      - mysql
    networks:
      - laravel
...

这是我的 Dockerfile

FROM nginx:stable-alpine

COPY ./fullchain.pem /etc/nginx/fullchain.pem
COPY ./privkey.pem /etc/nginx/privkey.pem

ADD nginx.conf /etc/nginx/nginx.conf
ADD default.conf /etc/nginx/conf.d/default.conf

RUN mkdir -p /var/www/html

RUN addgroup -g 1000 laravel && adduser -G laravel -g laravel -s /bin/sh -D laravel

RUN chown laravel:laravel /var/www/html

我正在将 pem 文件复制到 nginx 容器中,以便我可以使用它们。

这是我的 default.conf 文件,应该加载我的证书

server {
    listen 80;
    index index.php index.html;
    server_name servee.co.uk;
    root /var/www/html/public;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }  

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }  
}

server {
    listen 443 ssl;
    server_name servee.co.uk;

    ssl_certificate /etc/nginx/fullchain.pem;
    ssl_certificate_key /etc/nginx/privkey.pem;

    index index.php index.html;
    
    location / {
        proxy_pass http://servee.co.uk; #for demo purposes
    }
}

nginx 容器构建成功,当我 bash 进入它时,我可以找到 pem 文件。问题是当我转到https://servee.co.uk 时,我只是得到无法连接错误。如果我去http://servee.co.uk 它工作正常。

我不确定我错过了什么,这真的让我对 docker 感到厌烦,因为让 SSL 工作非常痛苦,所以希望它是一个简单的修复。

【问题讨论】:

  • HTTPS 默认使用端口 443,但您打开(仅)433。433 不是 443。顺便说一句,来自 LetsEncrypt 的证书不是自签名的。在 PKI 中,自签名是一个艺术术语,表示使用与证书中相同的密钥(对)进行签名;它与您请求甚至指导 CA颁发无关。
  • @dave_thompson_085 非常感谢,我不敢相信我制作了这种类型!感谢您还解释了什么是 sef 签名的证书真的很有帮助

标签: docker nginx ssl lets-encrypt


【解决方案1】:

您需要更新 docker-compose.yml 文件以使用端口 443 而不是 433 来匹配您的 nginx.conf。试试下面的 docker-compose.yml 文件。

...
services:
  nginx:
    build:
      context: docker/nginx
      dockerfile: Dockerfile
    ports:
      - 443:443
      - 80:80
    volumes:
      - ./src:/var/www/html:delegated
    depends_on:
      - app
      - mysql
    networks:
      - laravel
...

【讨论】: