Python server settings (nginx + Gunicorn)

I looked it up in 2014 and personally said this is the Python API server settings.

Put nginx on the front and use gunicorn on the application server to reverse proxy.

It's a bit old, but I decided on this by referring to Instagram. INSTAGRAM ENGINEERING

A memo when setting to AWS EC2.

Install gunicorn

Installed using pip on virtualenv. I use virtualenv because I don't like yum getting stuck when I play with Python on the OS, and I want to raise the Python version.

Start gunicorn at startup

I'm not very familiar with it, but I registered for upstart.

Describe the following configuration file.

/etc/init/myapp.conf


description "[APP NAME] API Server"
 
start on runlevel [2345]
stop on runlevel [016]
 
respawn
 
script
su - www
/home/www/[APP NAME]/virtualenv/bin/gunicorn -b 127.0.0.1:8000 -w 4 -u www --chdir /home/www/[APP NAME]/server/python --log-file /home/www/[APP NAME]/logs/error_log --pythonpath /home/www/[APP NAME]/server/python server:application
end script

At the time of centos7, upstart is not included, so set it to systemctl. You can find it on the Gunicorn site, including the configuration file.

Reference: Deploying Gunicorn --Gunicorn 19.3.0 documentation

nginx installation

Install with yum.

nginx configuration file.

/etc/nginx/nginx.conf


user  www;
worker_processes  1;

error_log  /var/log/nginx/error.log;

pid        /var/run/nginx.pid;


events {
    worker_connections  1024;
}


http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    sendfile        on;

    keepalive_timeout  65;

    include /etc/nginx/conf.d/*.conf;

    index   index.html;

    upstream app_server {
        server 127.0.0.1:8000 fail_timeout=0;
    }

    server {
        listen       80;
        server_name  [DOMAIN];
        root         /home/www/[APP NAME]/server/html;

        access_log  /var/log/nginx/[APP NAME].access.log  main;

        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://app_server;
        }

        error_page  404              /404.html;
        location = /40x.html {
        }

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
        }

    }

}

The main settings are the following two.

Recommended Posts

Python server settings (nginx + Gunicorn)
Try the Taxii server (3.gunicorn settings)
python environment settings
Python execution server construction (Python + uWSGI + Django + Nginx)
Run python wsgi server on NGINX Unit
Run Flask on CentOS with python3.4, Gunicorn + Nginx.
python> udp> echo server
DNS server in Python ....
Local server with python
Launch a Python web application with Nginx + Gunicorn with Docker
Easy HTTP server with Python
Simple HTTP Server for python
Snippet settings for python jupyter notebook
Install Python3 on Sakura server (FreeBSD)
Write an HTTP / 2 server in Python
Emacs settings for Python development environment
Install nginx python postgresql using apt-get
Behavior of python3 by Sakura's server