Reference Links: Supervisor + Tornado + Nginx use detailed, with Tornado, Supervisord, nginx Rack website, Tornado official documents
Project Document Tree:
. ├──chnservices│└──channels.py├──etc│├──chnservices.conf│├──nginx││└──nginx.conf│├──superviso Rd.conf│└──supervisord.conf.original└──venv├──bin│├──activate│├──activate.csh│├──activat E.fish│├──activate_this.py│├──easy_install│├──easy_install-2.7│├──pip│├──pip2│├──pip2.7│├──python│├──python2-Python│└──python2.7-Python├──include│└──python2.7/usr/include/python2.7├──lib│└──python2.7└──local├──bin/a/path/venv/Bin├──include/a/path/venv/Include└──lib/a/path/venv/lib
Supervisor.conf (part)
[program:app-Channels]process_name=% (program_name) s-%(process_num) sdirectory=/a/path/chnservices/Command=/a/path/venv/bin/python2.7/a/path/chnservices/channels.py--port=%(process_num) Snumprocs=2Numprocs_start=8001; Umask=022;p riority=999autostart=Truestartsecs=2; Startretries=3; AutoRestart=Unexpected;exitcodes=0,2; stopsignal=Quit;stopwaitsecs=10; Stopasgroup=False;killasgroup=Falseuser=www-Dataredirect_stderr=Truestdout_logfile=/var/log/nginx/chn_stdout.log;stdout_logfile_maxbytes=1mb;stdout_logfile_backups=10; Stdout_capture_maxbytes=1mb;stdout_events_enabled=Falsestderr_logfile=/var/log/nginx/chn_stderr.log;stderr_logfile_maxbytes=1mb;stderr_logfile_backups=10; Stderr_capture_maxbytes=1mb;stderr_events_enabled=false;environment=a="1", b="2"; ServerURL=auto
channels.py
ImportTornado.ioloopImportTornado.web fromTornado.optionsImportdefine, Optionsdefine ("Port", default=8006, help="run on the given port", type=int)classMainHandler (tornado.web.RequestHandler):defGet (self): Self.write ("Hello, World")defMake_app ():returnTornado.web.Application ([(R"/", MainHandler),])if __name__=="__main__": Tornado.options.parse_command_line ()#tornado.options.parse_config_file ("/etc/chnservices.conf") Print 'Port:', Options.port app=Make_app () app.listen (Options.port) tornado.ioloop.IOLoop.current (). Start ()
NGINX.CONF (Tornado recommended configuration modification)
User Www-data;Worker_processes 1;Error_log/var/log/nginx/error.log;Pid/var/run/nginx.pid;Events {Worker_connections1024;Use Epoll;}http {# Enumerate all ' Tornado servers here upstream frontends {server127.0.0.1:8001;Server 127.0.0.1:8002;} include/etc/nginx/mime.types;Default_type Application/octet-stream;Access_log/var/log/nginx/access.log;Keepalive_timeout65;Proxy_read_timeout 200;Sendfile on;Tcp_nopush on;Tcp_nodelay on;Gzip on;Gzip_min_length 1000;Gzip_proxied any;gzip_types text/plain text/html text/css text/xml application/x-javascript application/xml Application/atom+xml Text/javascript;# Only retry if there is a communication error,Not a timeout # on the Tornado server (to avoid propagating"Queries of Death"# to all frontends) Proxy_next_upstream error;Proxy_next_upstream Error;Server {Listen8000;# Allow file uploads client_max_body_size 50M;Location ^~/static/{root/var/www/tornoda/;if ($query _string) {Expires Max;}} Location=/favicon.ico {rewrite (. *)/static/favicon.ico;} Location=/robots.txt {rewrite (. *)/static/robots.txt;} location/{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://frontends; } }}
Installation:
Use supervisor to do process management, use Nginx to do reverse proxy; Supervisor can use source installation and Yum installation (CentOS), using PIP installation and source installation is the same reason:
sudo Install Supervisor
Supervisor default will be from (/usr/local/etc/supervisord.conf,/usr/local/supervisord.conf, supervisord.conf, etc/ supervisord.conf,/etc/supervisord.conf) Looking for configuration files, for convenience, we generate a profile and modify it after linking to/etc/supervisord.conf.
To build the configuration file:
echo_supervisord_conf >/a/path/etc/supervisord.conf
Modified by example, linked to the specified location after modification:
sudo LN -s/a/path/etc/chnservices.conf/etc/chnservices.conf
Modify the Nginx configuration file to link to the default location:
sudo LN -s/a/path/etc/nginx/nginx.conf/etc/nginx/nginx.conf
Start Supervisor:
sudo supervisord-c/etc/supervisord.confsudo supervisorctl startallsudo supervisorcrl Reloadallsudo supervisorcrl restart all
At this point, the browser accesses http://127.0.0.1:8001/and http://127.0.0.1:8002/should be able to see "Hello, World".
Start Nginx:
sudo service nginx restart
At this point, the browser access http://127.0.0.1:8000/can see from the tornado via Nginx "Hello, World".
Add boot up:
Tornado code encountered an exception exit when supervisor will automatically restart our Python code, but now supervisor is not on boot, edit/etc/rc.local, add Supervisord before exit 0.
Nginx + Tornado + Supervisor Deployment