【问题标题】:how to configure ssl certificate for both flask api and client如何为flask api和客户端配置ssl证书
【发布时间】:2020-11-15 04:48:34
【问题描述】:

我已经制作了电子邮件联系表,用于发送邮件。 前端是用 HTML/CSS 和 JS 制作的。 index.html后面的JS如下:

contactForm = document.getElementById("contact_form");
contactForm.onsubmit = (element) => {
  element.preventDefault();

  name = document.getElementById("form_name").value;
  email = document.getElementById("form_email").value;
  subject = document.getElementById("form_subject").value;
  message = document.getElementById("form_message").value;

  raw = JSON.stringify({
  name: name,
  email: email,
  subject: subject,
 message: message,
});
console.log(raw);

fetch("http://example.com:5000/contact", {
method: "POST",
headers: {
  "Content-Type": "application/json",
},
body: raw,
redirect: "follow",
})
.then((response) => response.text())
.then((result) => {
  console.log(result);
  if (result == "received") {
    alert("Your message has been sent");
  } else {
    alert("Sorry we couldn't send your message, please try again");
  }
})
.catch((error) => console.log("error", error));
 };

烧瓶应用:

from flask import Flask, request
from pprint import pprint
from flask_cors import CORS
import smtplib
from email.message import EmailMessage



app = Flask(__name__)
CORS(app)

email_add = 'USER'
email_pass = 'PASS'


@app.route('/')
def verify():
    return "app is running"


@app.route('/contact', methods=['POST'])
def post_email():
    request_data = request.json
    pprint(request_data)

    if 'email' and 'message' and 'name' and 'subject' in request_data:
        msg = EmailMessage()
        msg['Subject'] = request_data['subject']
        msg['From'] = request_data['email']
        msg['To'] = email_add
        name = request_data['name']
        msg.set_content("   From " + name + ", " + "\n\n" + "  " + request_data['message'] + "\n\n" + 
"My email: " + request_data['email'])
        pprint(request_data['subject'])

        smtp = smtplib.SMTP_SSL('smtp.gmail.com', 465)
        smtp.login(email_add, email_pass)
        smtp.send_message(msg)
        return "received"
    else:
        return "sorry"


if __name__ == '__main__':
    app.run()

应用程序托管在 linux server 上,客户端文件使用 Nginx 提供,并且烧瓶 API 使用 uwsgi 提供 gunicorn。我在 systemd 中创建了一个服务文件,它负责在 0.0.0.0:5000 运行烧瓶应用程序。一切工作正常,但是当我从letsencrypt安装SSL证书时,客户端文件工作正常,但我的flask API不再工作了。它一直说无法获取,使用了混合协议。我应该如何解决这个问题。我试过 pyOpenSSL 但它不起作用。无论如何我可以在我的 API 和客户端服务器中使用 SSL。谢谢你。 这就是我为我的应用提供服务的方式。

server {
server_name www.example.com;
root /home/jimrahman/dark_second;
index index.html;


location /contact {
    include proxy_params;
    proxy_pass https://0.0.0.0:5000;
}


 

【问题讨论】:

    标签: ssl nginx flask gunicorn uwsgi


    【解决方案1】:
    1. 使用 Let's Encrypt 或任何其他 SSL 证书提供程序为您的域(例如 example.com)生成 SSL 证书,或自签名
    2. 配置您的 nginx 站点 (https://nginx.org/en/docs/http/configuring_https_servers.html):
    server {
        listen              443 ssl;
        server_name         www.example.com;
        ssl_certificate     /path/to/www.example.com.crt;
        ssl_certificate_key /path/to/www.example.com.key;
    
        root /home/jimrahman/dark_second;
        index index.html;
    
    
        location /api {
            include proxy_params;
            proxy_pass https://0.0.0.0:5000;
        }
    }
    

    您可能需要配置一些其他的东西才能拥有可靠的“好”ssl。

    1. 使用www.example.com/api 或简单的/api,如果在同一服务器上作为您的 api 的基本路径运行,因此如果您使用 fetch 来调用它:
    fetch("/api/contact", {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
      },
      body: raw,
      redirect: "follow",
    })
    
    1. 如果您在不同的服务器上运行这两种服务,您可能需要考虑 cors。但请注意如何配置 CORS。只向所有 Origin 开放您的 api 可能很危险。 https://flask-cors.readthedocs.io/en/latest/

    【讨论】:

    • 您好,感谢您的回复,但是当我获取(“/contact”)时,我收到了一个错误的网关错误。关于我应该如何处理它的任何想法。
    • 如果你到目前为止使用你的配置,这将是对“/contact/contact”的调用,在我提供的配置示例中,你需要使用“/api/contact”。如果不知道您的确切配置,很难说出问题所在。
    猜你喜欢
    • 2019-12-05
    • 2010-10-16
    • 1970-01-01
    • 2011-03-12
    • 2017-06-01
    • 2012-07-19
    • 1970-01-01
    • 2013-06-20
    • 2013-08-04
    相关资源
    最近更新 更多