Nginx+gunicorn+django

Source: Internet
Author: User
Tags php tutorial
Official address: http://gunicorn.org/
                  Http://docs.gunicorn.org/en/19.2/

Reference Address: http://www.cnblogs.com/ArtsCrafts/p/gunicorn.html

http://rfyiamcool.blog.51cto.com/1030776/1276364

Introduction of Preface

What WSGI server has:
For example, flask, Webpy, Django, and cherrypy all bring their own wsgi Server. Of course, the performance is not good, the self-brought Web server is more testing purposes, when the online release, the use of high-performance WSGI server or joint nginx do UWSGI.

Second, installation Gunicorn

~$ sudo pip install Gunicorn

Install three Python packages if you want Gunicorn to support asynchronous workers

~$ sudo pip install Greenlet

~$ sudo pip install Eventlet

~$ sudo pip install gevent

Description: If installing Greenlet fails, you need to install Python headers

~$ sudo apt-get install Python-dev

We can take a look at the Gunicorn version.

~$ Gunicorn--version
Gunicorn (version 19.1.1)

Second, Gunicorn command use

After successfully installing Gunicorn, you have the following three instructions you can use directly to start Gunicorn run WSGI application or WSGI frameworks.

1. Gunicorn

Gunicorn server is the most basic command, directly used to run the most basic WSGI application.

Usage: gunicorn [OPTIONS] App_module

Options Optional Parameters run the configuration option for Gunicorn, which is described later.

App_module Specify WSGI application file, write format $ (module_name): $ (variable_name). Where module_name is used to specify the WSGI application file that will be run, but a full dotted name.

For example, the current directory in the MyApp directory has a python package Gunicorn_app, Gunicorn_app package has a WSGI application file test.py.

Then module_name can be written directly to Gunicorn_app.test.

Variable_name represents the name of the object to be called in the Module_name file (which is a WSGI callable, which can be a function).

In the example above, the current directory is/home/workspace/myapp,myapp with a package gunicorn_app,test.py code as follows:

def app (environ, start_response): "" "    simplest possible Application object" ""    data = ' Hello, world!\n '    Status = ' OK '    response_headers = [        (' Content-type ', ' Text/plain '),        (' Content-length ', str (len data))    ]    Start_response (status, Response_headers)    return iter ([data])

We're going to run the app in test.py

Gunicorn Gunicorn_app.test:app

2. Gunicorn_django

The Guniorn_django command is used to deploy the Django app to the Gunicorn server.

In fact it is very simple, the same principle and gunicorn, just gunicorn_django do a special treatment, making it more suitable for Django.

Basic usage: Gunicorn_django [OPTIONS] [Settings_path]

The OPTIONS have been said before.
Settings_path the directory where the settings.py file is located in the Django app, the default is to find it in the current directory, if not written.

However, this usage applies to django1.4 before, and it is strongly recommended to use the Gunicorn command after the Django1.4 version.

3. Gunicorn_paster

This command is of interest to the official documentation study.

Three, Gunicorn configuration

Gunicorn reads configuration information from three different places.

First place: read from the framework-defined configuration information, currently valid only for the Paster framework.

Second place: defined on the command line, the configuration information defined in the command line overrides the value of the same parameter name defined in the framework.

Third place: Create a configuration file that writes configuration information to a file (it's a Python source file, so you're like you're writing Python code).

View all command configuration information via Gunicorn-h

More details can also be viewed through the official documentation: HTTP://DOCS.GUNICORN.ORG/EN/19.2/

As above on the basis of the MyApp example above

Gunicorn--workers=4--bind=127.0.0.1:8000 Myapp.gunicorn_app.test:app

The above command launches 4 workers, bound to 127.0.0.1:8000

Or you use the configuration file, below is a config.py configuration file source code:

Import Multiprocessingbind = "127.0.0.1:8001" workers = Multiprocessing.cpu_count () * 2 + 1

Gunicorn--config=config.py Myapp.gunicorn_app.test:app

Iv. Introduction of Gunicorn Framework

Gunicorn is based on the Pre-fork model. It also means that a central management process (master process) is used to manage the worker process collection. Master never knows any information about the client. All requests

and response processing are handled by the worker process.

Master (manager)

The main program is a simple loop that listens to various signals and the corresponding response process. Master manages the set of worker workers that are running.

Worker Type

1. Sync Workers

The most basic is also the default worker type.

A synchronized worker class that can control only one request at a time.

2. Async Workers

The use of asynchronous workers is based on greenlets (via Eventlet and Gevent). So be sure to install Python-corresponding packages before using this worker type.

Greenlets is an implementation of Python multi-threaded collaboration.

3. Tornado Workers

This is a tornado worker class.

V. Deployment of Gunicorn

Using Gunicorn must be based on a proxy server.

1. Nginx Configuration

Although there are many HTTP proxies to use, we strongly recommend Nginx. If you choose a different proxy server, you need to confirm that it buffers slow clients when you use the default Gunicorn workers. No buffering Gunicorn will be vulnerable to denial of service attacks. You can also use Slowloris to verify that your proxy server is working well.

The following is an Nginx configuration file instance (assuming the 127.0.0.1:8888 port has been monitored by the gunicorn binding):

server {        #listen   ; # listen for IPv4; the-line is default and implied        #listen   [::]:80 default Ipv6only =on; # # Listen for IPv6        listen;        Client_max_body_size 4G;        server_name www.android_stat.com        keepalive_timeout 5;        Location/{                try_files $uri @proxy_to_app;        }        Location @proxy_to_app {                proxy_set_header x-forwarded-for $proxy _add_x_forwarded_for;                Proxy_set_header Host $http _host;                Proxy_redirect off;                Proxy_pass http://127.0.0.1:8888;        }

Vi. Monitoring and Gunicorn

Note: To monitor Gunicorn, Gunicorn cannot start daemon mode, and if you use daemon mode, you will be able to fork out a process so that the monitoring tool will not be able to monitor the process.

I'm here to introduce supervisor.

1. Supervisor

Supervisor can be used to monitor the process, the following is a simple supervisor configuration file:

[Program:gunicorn]command=/path/to/gunicorn main:application-c/path/to/gunicorn.conf.pydirectory=/path/to/ Projectuser=nobodyautostart=trueautorestart=trueredirect_stderr=true

The above describes the Nginx+gunicorn+django, including aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.