Deploy the flask application in nginx and tornado

Source: Internet
Author: User

I searched the internet for how to deploy the flask application. Most of them were deployed on nginx using wsgi. after a long time of deployment, the deployment was unsuccessful. It may be that my comprehension ability was too poor, however, the environment on the server is also messy, including python2 and python3. Finally, it was really hard to solve the problem. Replace uwsgi with tornado, which is very simple. Record the steps for future reference:

You don't have to talk about how to install the software. There are several key points:

1. The flask entry program is run. py. The Code is as follows:

# Coding = UTF-8
#! /Usr/bin/Python

From somewhere import app # Somewhere indicates an instance containing flask, for example, APP = flask (_ name __)

If _ name _ = "_ main __":

App. Run (DEBUG = true)

2. Add tornado application tornado_server.py to the directory of run. py to host run. py. The Code is as follows:

# Coding = UTF-8
#! /Usr/bin/Python

From tornado. wsgi import wsgicontainer
From tornado. httpserver import httpserver
From tornado. ioloop import ioloop
From run import app

Http_server = httpserver (wsgicontainer (APP ))
Http_server.listen (5000) # default flask Port
Ioloop. instance (). Start ()

3. If Python tornado_server.py is run directly, the output is as follows:

* Running on http: // 127.0.0.1: 5000/
* Restarting with reloader

But deploy it in the production environment to nginx with good performance, so I tested it briefly. The corresponding nginx configuration file is pasted below:

Server {
Listen 80;
SERVER_NAME abc.com;
Rewrite ^ (. *) http://www.abc.com $1 permanent;
}

Server {
Listen 80;

# Listen [:]: 80 default_server;
# Access_log/var/log/nginx/win2003_access.log main;
# Include header_proxy.inc;

SERVER_NAME www.abc.com;

# Root/var/www/ABC;

Location /{
# Index index.html index.htm index. php;
# Include uwsgi_params;
# Uwsgi_pass Unix:/tmp/uwsgi. Sock;
Proxy_pass http: // localhost: 5000; # The key point here is that all access to http://www.abc.com: 80 will be redirected to port 5000 on the local machine
}

}

This is just a test. Some other optimizations, such as static files, have not been used by the nginx proxy.

After configuration, you only need to reload nginx to make it take effect:

# Nginx-s reload

It seems that tornado is still very good and has always known its excellent performance. You have to study it later.

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.