简体   繁体   English

如何使用 https 而不是 Z80791B3AE7002CB88FZ88FA?

[英]How to start Dockerized Python Flask app with https instead of http?

I have a REST api server developed with Python & Flask framework and deployed in the main server with Docker . I have a REST api server developed with Python & Flask framework and deployed in the main server with Docker . When I build & start the docker container by sudo docker-compose build & sudo docker-compose up , the docker container starts at http://0.0.0.0:5000/ or localhost and using port 5000 . When I build & start the docker container by sudo docker-compose build & sudo docker-compose up , the docker container starts at http://0.0.0.0:5000/ or localhost and using port 5000 .

This works fine in non-ssl environment or browser or domain.这在non-ssl环境或浏览器或域中运行良好。 But recently my main website (suppose www.example.com ) started using ssl certificates .但最近我的主要网站(假设www.example.com )开始使用ssl 证书 To communicate with main site I have to serve my apis in https instead of http .要与主站点通信,我必须在https而不是http中提供我的api

Now I am trying to use this code to start server in https mode, but getting some errors.现在我正在尝试使用此代码以 https 模式启动服务器,但出现一些错误。

My code:我的代码:

import ssl
ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
ctx.load_cert_chain('certificate.pem', 'privateKey.pem')

.
.

if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0',ssl_context=ctx)

Here is my error:这是我的错误:

在此处输入图像描述

What will be the proper procedure to start Docker supported Python Flask app in https mode?在 Z5E056C500A5001C4B7BADEB7180 模式下启动 Docker 支持的 Python Flask 应用程序的正确程序是什么?

After 2 days of tiresome work I found the proper solution.经过 2 天的繁琐工作,我找到了正确的解决方案。 My domain got 2 files when implemented ssl or https .实施sslhttps时,我的域有 2 个文件。 One is mydomain_com.crt file, another is private.key一个是mydomain_com.crt文件,另一个是private.key

I found these 2 files in my server's main directory.我在服务器的主目录中找到了这两个文件。 As I am using Apache, then I found these files in /etc/var/www/html folder.当我使用 Apache 时,我在/etc/var/www/html文件夹中找到了这些文件。

I copied 2 files as follows:我复制了2个文件如下:

  1. copied the license from mydomain_com.crt file and paste in a text file then renamed it certificate.pemmydomain_com.crt文件复制许可证并粘贴到文本文件中,然后将其重命名为certificate.pem

  2. copied the license from private.key and paste in a text file then renamed it privateKey.pemprivate.key复制许可证并粘贴到文本文件中,然后将其重命名为privateKey.pem

Then I used my domain's default ssl certificate into my Flask app like this:然后我在我的 Flask 应用程序中使用了我的域的默认 ssl 证书,如下所示:

import ssl
ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
ctx.load_cert_chain('certificate.pem', 'privateKey.pem')
.
.
.
if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0', ssl_context=ctx)

And exposed the 2 ports in my Dockerfile like this:并像这样暴露了我的Dockerfile中的2 个端口

FROM python:3
WORKDIR /usr/src/app
COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD [ "python", "app.py" ]
EXPOSE 80
EXPOSE 443

After restart, my Dockerized Flask app now working with my domain's default ssl certificate and serving the APIs correctly.重新启动后,我的 Dockerized Flask 应用程序现在可以使用我域的默认 ssl 证书并正确提供 API。

Example: https://example.com:5000/getUsers示例: https://example.com:5000/getUsers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM