Nginx+Uwsgi Integration

To improve the performance of python website under nginx, it’s better to use tcp connection rather than proxy_pass with http protocol.

nginx conf

edit /etc/nginx/site-enabled/default or add a new file in site-enabled folder

server{
    listen 80;
    server_name abc.com
    location /api {
        uwsgi_pass 127.0.0.1:3015;
        include uwsgi_params;
        access_log /var/log/api.log;
    }
}

nginx will forward http request http://abc.com/api/* to 127.0.0.1:3015

uwsgi conf file

[uwsgi]
pidfile=%d/uwsgi.pid
socket=127.0.0.1:3015
# uwsgitop can be used to monitor the uwsgi stat
stats=127.0.0.1:3016  
chdir=%d/../backend
pythonpath=%d/../backend
master=true
plugins=python2
cpu-affinity=2
processes=16
threads=4
module=server
daemonize=%d/log/uwsgi.log 
uid=website
gid=website
cap=setgid,setuid
# wait the process to reload for 10 seconds, in the document the key is reload-mercy which is deprecated
worker-reload-mercy=10 
# drop the connection longer than 30 seconds 
harakiri=30  
master=1
no-orphans=1

save it as uwsgi.ini, then start uwsgi with command uwsgi --ini uwsgi.ini

Detail options can be found here

Misc command

reload uwsgi smoothly

uwsgi --reload uwsgi.pid

reload nginx smoothly

nginx -s reload

test nginx configration file

nginx -t -c /etc/nginx/nginx.conf
comments powered by Disqus