Saturday 31 May 2014

Run Tornado ap on nginx using supervisor

First need to install supervisor:
apt-get install supervisor

Then add tornado.conf to /etc/supervisor/conf.d

an example tornado.conf:

programs=tornado-8000,tornado-8001
stderr_logfile=/var/log/tornado.err.log

[program:tornado-8000]
command=python /path/to/upload.py --port=8000
directory=/path/to
user=www-data
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/tornado.log
stderr_logfile=/var/log/tornado.err.log
loglevel=info
[program:tornado-8001]
command=python /path/to/upload.py --port=8001
directory=/path/to
user=www-data
autorestart=true
redirect_stderr=true
stdout_logfile=/var/log/tornado.log
stderr_logfile=/var/log/tornado.err.log
loglevel=info
 
add tornado config to nginx.conf

proxy_next_upstream error;
upstream tornadoes {
server 127.0.0.1:8000;
server 127.0.0.1:8001;
}
server {
listen 1.2.3.4:80;
server_name example.com;
root /path/to;
client_max_body_size 50m;
try_files  $uri @tornado;

error_log /var/log/nginx/tornado-error.log;
access_log /var/log/nginx/tornado-access.log;

location ^~ /static/ {
        root /path/to;
        if ($query_string) {#
        expires max;
        }
}

location ~* ^.+.(jpg|jpeg|gif|png|rar|ico|xml|avi|zip|3gp|flv|pdf)$ {
        expires           max;
        root /path/to;
        add_header Pragma public;
        add_header Cache-Control "public, must-revalidate, proxy-revalidate";
 }


location @tornado  {
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Scheme $scheme;
proxy_pass http://tornadoes;
}
}

Now fire the supervisor console:

~# supervisorctl
         supervisor>  update

If everyhing is alrigh you will see


tornadoes:tornado-8000           RUNNING    pid 25430, uptime 0:11:42
tornadoes:tornado-8001           RUNNING    pid 25431, uptime 0:11:42


Otherwise

Check the process status
supervisor> status
If there is something wrong you will see:

tornadoes:tornado-8000           FATAL      Exited too quickly (process log may have details)
tornadoes:tornado-8001           FATAL      Exited too quickly (process log may have details)



If you don't see the processes running, you need to restart them



supervisor> restart all

tornadoes:tornado-8000           RUNNING    pid 6023, uptime 0:03:32
tornadoes:tornado-8001           RUNNING    pid 6024, uptime 0:03:32



That's it!