Nginx + flask + flup: full demo


Recently, I want to learn something about nginx. I found that everyone compiles by themselves. It’s not hard to compile by yourself, but considering that the installation package of Ubuntu is enough, let’s use it first.

Install nginx

Ubuntu offers fournginxThe functions of the installation package are as follows:nginx-lightnginx-corenginx-fullnginx-extras

Before installation, you can useaptitude showView the modules supported by each installation package. such asnginx-extrasthannginx-fullMoreperlandluasupport.

After weighing, it is found that the best choice isnginx-full“Standard edition”.

$ sudo apt-get install nginx-full

     nginx-common (= 1.9.3-1ubuntu1), libc6 (>= 2.14), libexpat1 (>= 2.0.1), libgd3 (>=
     2.1.0~alpha~), libgeoip1, libpam0g (>=, libpcre3, libssl1.0.0 (>= 1.0.2~beta3),
     libxml2 (>= 2.7.4), libxslt1.1 (>= 1.1.25), zlib1g (>= 1:1.2.0)

If you don’t know which files are installed, you can use thedpkg --listfilesCommand view:

[email protected]:~/study/nginx$ dpkg --listfiles nginx-full

You can install the following documents with you:

$ sudo apt-get install nginx-doc

Nginx command

Restart and other operations can be carried out through/etc/init.d/nginxExecution:

$ sudo /etc/init.d/nginx restart

This is a shell script(/etc/init.d/There are shell scripts in the directory, and the following commands are available:

configtest    reload        rotate        status        upgrade
force-reload  restart       start         stop

Of course, directly/usr/sbin/nginxYes, through-stonginxThe process signals.

Write an application with flask

Flask is Python’s micro web framework. It was introduced in the article before, and now it is the simplest application.

stayHOMECreate a project directory:

[email protected]:~$ mkdir flaskr

stayflaskrCreate files in, content:

# encoding: utf-8

from flask import Flask

app = Flask(__name__)

def hello():
    return 'Hello World!'

Recreate fileflaskr.fcgi, content:


from flup.server.fcgi import WSGIServer
from app import app

if __name__ == '__main__':
    WSGIServer(app, bindAddress='/tmp/flaskr-fcgi.sock').run()

This is fastcgi wrapper. Installation requiredFlupTo support fastcgi:

$ sudo pip install flup

Note the bound socket file when starting WSGI server. This file is used to configure nginx later. It is a bridge between flup and nginx.

byflaskr.fcgiAdd the execution permission to the file. It’s a python file anyway.

$ chmod +x flaskr.fcgi

Configure nginx

For simplicity, edit directlynginx.confFile:

$ sudo vim /etc/nginx/nginx.conf

Add aserverConfigure tohttpmodular:

http {
    server {
        listen 80;
        server_name localhost;

        location / {
            try_files $uri @flaskr;
        location @flaskr {
            include fastcgi_params;
            fastcgi_param SCRIPT_NAME "";
            fastcgi_param PATH_INFO $fastcgi_script_name;
            fastcgi_pass unix:/tmp/flaskr-fcgi.sock;

The last line specifies the socket file for communication between nginx and fastcgi:

fastcgi_pass unix:/tmp/flaskr-fcgi.sock;

Run program

Start fastcgi wrapper:

[email protected]:~/flaskr$ ./flaskr.fcgi 


[email protected]:~/flaskr$ python flaskr.fcgi 

Switch torootUser, modify the socket file permission:

[email protected]:/tmp$ sudo su
Password for [sudo] Adam: 
[email protected]:/tmp# chmod 777 flaskr-fcgi.sock

Otherwise, nginx does not have access to the socket file. See/var/log/nginx/error.logYou will see the following errors:

2015/11/09 22:23:21 [crit] 15357#15357: *1 connect() to unix:/tmp/flaskr-fcgi.sock failed (13: Permission denied) while connecting to upstream, client:, server: localhost, request: "GET / HTTP/1.1", upstream: "fastcgi://unix:/tmp/flaskr-fcgi.sock:", host: "localhost"

After modifying the permissions of the socket file, nginx does not seem to need to restart.

Visithttp://localhost/, can be seen without accident"Hello World!"

See also :