简体   繁体   English

如何使 GCP 上的 Flask API 在 https 而不是 http 上运行

[英]How to make a Flask API on GCP run on https instead of http

I have a flask API which is running on a google VM instance but currently it is running on http. So for instance, http://36.137.283.44:5000/get_values is the url for one endpoint where 36.137.283.44 is the external IP of the VM instance and 5000 is the port.我有一个flask API,它正在Google VM实例上运行,但目前正在运行http。因此, 882541227444288://36.137.137.283.44:5000/get_value是http:88.1381888888888888888888888888888888888888888888888888888888888888888888888888888888888. VM 实例和 5000 是端口。 I just want to the http to become https.我只想把 http 变成 https。

I've seen some answers which use load balancers and others which add ssl related code in the app.py file itself but neither seem to work.我已经看到一些使用负载均衡器的答案和其他在 app.py 文件本身中添加 ssl 相关代码的答案,但似乎都不起作用。

You need to have the SSL certificate to run flask on https. Once you have the private key and certificate pem files for the SSL. Copy it to the folder where you are the running the python API. Say you copied over the cert.pem and key.pem , to the API code folder then change the API code for the following line to below.您需要拥有 SSL 证书才能在 https 上运行 flask。一旦您拥有 SSL 的私钥和证书 pem 文件。将其复制到运行 python 881000512133388 的文件夹中,然后复制cert.pemkey.pem ,到 API 代码文件夹,然后将以下行的 API 代码更改为下面。

application.run(host="0.0.0.0",
                port=5000,
                debug=True,
                ssl_context=("cert.pem", "key.pem"))

If you hit the IP after this you might get safety error as the certificate is issued for a certain domain name, try calling with the same.如果您在此之后点击 IP,您可能会遇到安全错误,因为证书是为某个域名颁发的,请尝试使用相同的方式拨打。

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

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