【问题标题】:Redirect http to https nginx in docker container将http重定向到docker容器中的https nginx
【发布时间】:2022-01-20 03:40:27
【问题描述】:

我正在尝试使用 docker 容器中的 nginx 将所有 HTTP 流量重定向到 HTTPS。我正在使用docker-compose up 构建 NGINX 容器。在我运行docker-compose up 后,我收到一个错误:

[emerg] 1#1: cannot load certificate "/etc/nginx/etc/nginx/nginx/files/localhost.crt": BIO_new_file() failed (SSL: error:02001002:system library:fopen:No such file or directory:fopen('/etc/nginx/etc/nginx/nginx/files/localhost.crt','r') error:2006D080:BIO routines:BIO_new_file:no such file)

下面是我的 nginx.conf

NGINX.conf

http {
  upstream flask {
    server app:8000;
  }
  server {
    listen 80;
    server_name localhost;
  }

  server {
    listen 443 ssl;
    server_name localhost;

    ssl_certificate etc/nginx/nginx/files/localhost.crt; // this is location of where my certificate is on my local machine
    ssl_certificate_key nginx/files/localhost.key;

    location / {
      proxy_pass http://flask;
      proxy_set_header Host "localhost";
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-Proto $scheme;
    }
  }
}

我不知道为什么我会收到“没有这样的文件”的错误。下面是我正在构建和使用的 NGINX 映像的Dockerfile。任何帮助将不胜感激。

NGINX dockerfile

FROM nginx:1.19.2-alpine
COPY nginx.conf /etc/nginx/conf.d/default.conf
COPY /files/localhost.crt /etc/nginx/nginx/files/localhost.crt

【问题讨论】:

  • 关键在您的错误信息中。您在证书和密钥的这些路径的开头缺少用于 root 的斜杠 /

标签: docker nginx


【解决方案1】:

您正在寻找这样的东西。

http {
  upstream flask {
    server app:8000;
  }
  server {
    listen 80;
    server_name localhost;
    return 301 https://$server_name$request_uri;
  }

  server {
    listen 443 ssl;
    server_name localhost;

    ssl_certificate /etc/nginx/nginx/files/localhost.crt; // this is location of where my certificate is on my local machine
    ssl_certificate_key /etc/nginx/nginx/files/localhost.key;

    location / {
      proxy_pass http://flask;
      proxy_set_header Host "localhost";
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-Proto $scheme;
    }
  }
}

您还需要复制密钥。

【讨论】:

  • 谢谢!这有很大帮助。我能够使重定向工作,但是现在当 curl HTTP 端口时,我得到了预期的“永久移动”。但是,如果我卷曲 HTTPS 端口,则会拒绝连接。有什么想法吗?
  • @DaveMichaels - 我猜你没有正确转发443,但我需要查看你的 docker-compose.yaml 文件才能确定。
猜你喜欢
  • 2017-02-25
  • 1970-01-01
  • 2022-01-20
  • 2011-03-29
  • 2018-01-05
  • 1970-01-01
  • 1970-01-01
  • 2017-02-04
  • 2014-10-11
相关资源
最近更新 更多