Python nginx+gunicorn+flask+supervisor

Source: Internet
Author: User
Tags virtual environment

1. Installing the Pyopenssl module

Pip Install Pyopenssl

If the flask is installed in a python3 environment, remember to switch to the PYTHON3 virtual environment

2, upload the certificate file to the project root directory, I use the public network certificate file, you can also manually produce self-signed certificate, there are many examples on the Internet, do not list.

3, configure HTTPS access, can be implemented in three ways

3.1, directly using the Flask framework of the server, modify the code for HTTPS access

From flask import Flask
App = Flask (name)
@app. Route ('/')
def index ():
Return ' Hello World '
If name = = 'main':
App.run (' 0.0.0.0 ', port=8100,ssl_context= ('./server.pem ', './server.key '))

Start flask

Python myapp.py

    • Running on https://0.0.0.0:8100/(press CTRL + C to quit)

3.2, using Gunicorn to implement HTTPS access, the code can not add a certificate file configuration

From flask import Flask
App = Flask (name)
@app. Route ('/')
def index ():
Return ' Hello World '
If name = = 'main':
App.run ()

To start the service with Gunicorn, add the specified certificate file parameter

gunicorn-w4-b0.0.0.0:8000--certfile=server.pem--keyfile=server.key Myapp:app
[2017-08-22 10:47:34 +0800] [23118] [INFO] starting Gunicorn 19.7.1
[2017-08-22 10:47:34 +0800] [23118] [INFO] Listening at:https://0.0.0.0:8000 (23118)
[2017-08-22 10:47:34 +0800] [23118] [INFO] Using Worker:sync
[2017-08-22 10:47:34 +0800] [23121] [INFO] booting worker with pid:23121
[2017-08-22 10:47:34 +0800] [23122] [INFO] booting worker with pid:23122
[2017-08-22 10:47:34 +0800] [23123] [INFO] booting worker with pid:23123
[2017-08-22 10:47:34 +0800] [23124] [INFO] booting worker with pid:23124

Once the service is started, it can be accessed via HTTPS.

3.3. Add the certificate file on the proxy server via Nginx proxy

server{
Listen 443;
server_name abc.abc.com;
SSL on;
Ssl_certificate Server.pem;
Ssl_certificate_key Server.key;
Ssl_session_timeout 5m;
Ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
Ssl_ciphers high:! rc4:! md5:!anull:!enull:! Null:! Dh:! edh:! Exp:+medium;
Ssl_prefer_server_ciphers on;
Location/{
Proxy_connect_timeout 3;
Proxy_send_timeout 30;
Proxy_read_timeout 30;
Proxy_pass http://*.*.*.*:5000;
Proxy_set_header Host $host;
Proxy_set_header X-real-ip $remote _addr;
Proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;
}
}

This is my implementation of three ways, other small partners have another way, please advise, thank you!

Python nginx+gunicorn+flask+supervisor

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.