在生产系统使用Tornado WebServer
在生产系统使用Tornado WebServer[program:web]command=python /var/www/site/Serv.py 80%(process_num)02dprocess_name=%(program_name)s_%(process_num)02dumask=022startsecs=0stopwaitsecs=0redirect_stderr=truestdout_logfile=/tmp/codoon.lognumprocs=4numprocs_start=1
这个配置会启动4个Tornado的服务进程分别监听 8001,8002,8003,8004 这四个端口
command这一行是要执行的命令,这里是用 python /var/www/site/Serv.py 端口号来启动Tornado的服务进程?80%(process_num)02d 的用途是通过进程编号来生成端口号。下面的process_name这个参数也会用到。这里要指定的文件名就是上一步我们创建那个Serv.py文件
process_name是进程的名字,由于这里要启动4个进程,所以要用process_num来区分
umask是程序执行的权限参数
startsecs这个参数是程序启动的等待时间
stopwaitsecs这个参数是程序停止的等待时间
redirect_stderr这个参数将错误流重定向到std的流输出,这样可以省去一个日志文件的配置,当然也可以不用这个参数分开配置日志文件
stdout_logfile 这个参数是STD流输出日志文件的路径,Tornado会输出所有的请求和错误信息,通过这个可以统一做日志处理,分隔什么的,在程序里就只需要print到std流就行了。
numprocs 这个参数指定了进程的数量,这里是4,表明要启动4个Tornado进程
numprocs_start 这个参数指定了进程号的起始编号,这里是1,这样前面的command和process_name里的%(process_num)02d部分就会在执行的时候被替换为01~05的字符串
配置修改完成后:wq保存退出,执行:
supervisorctl reload
重新加载配置后,这些进程就启动起来了
Step4:修改配置Nginx
首先找到在vhost目录里你的站点配置文件,打开后,在头上增加upstream的内容
upstream frontends {? ? ? ?
server 127.0.0.1:8001;? ? ? ?
server 127.0.0.1:8002;? ? ? ?
server 127.0.0.1:8003;? ? ? ?
server 127.0.0.1:8004;
}
然后在Server配置节里找到
?location / { 这个配置节
以前是用的FastCGI,所以里面的配置可能是这样子的
# host and port to fastcgi server ? ? ? ? ? ? ? ? ? ? ? ??
#fastcgi_pass 127.0.0.1:8081;? ? ? ? ? ? ? ? ? ? ? ? ??
#fastcgi_param PATH_INFO $fastcgi_script_name;? ? ? ? ? ? ? ? ? ? ? ? ??
#fastcgi_param REQUEST_METHOD $request_method;? ? ? ? ? ? ? ? ? ? ? ? ??
#fastcgi_param QUERY_STRING $query_string;? ? ? ? ? ? ? ? ? ? ? ? ??
#fastcgi_param CONTENT_TYPE $content_type;? ? ? ? ? ? ? ? ? ? ? ? ??
#fastcgi_param CONTENT_LENGTH $content_length;? ? ? ? ? ? ? ? ? ? ? ? ??
#fastcgi_pass_header Authorization;? ? ? ? ? ? ? ? ? ? ? ? ??
#fastcgi_param REMOTE_ADDR $remote_addr;? ? ? ? ? ? ? ? ? ? ? ? ??
#fastcgi_param SERVER_PROTOCOL $server_protocol;? ? ? ? ? ? ? ? ? ? ? ? ??
#fastcgi_param SERVER_PORT $server_port;? ? ? ? ? ? ? ? ? ? ? ? ??
#fastcgi_param SERVER_NAME $server_name;? ? ? ? ? ? ? ? ? ? ? ? ??
#fastcgi_intercept_errors off;
把这些统统删掉,变成了这样
location / {
}
在{}中加入upstream的配置,变成如下样子
location / {? ? ? ? ? ? ? ? ? ? ? ? ?
proxy_pass_header Server;? ? ? ? ? ? ? ? ? ? ? ? ?
proxy_set_header Host $http_host;? ? ? ? ? ? ? ? ? ? ? ? ?
proxy_set_header X-Real-IP $remote_addr;? ? ? ? ? ? ? ? ? ? ? ? ?
proxy_set_header X-Scheme $scheme;? ? ? ? ? ? ? ? ? ? ? ? ?
proxy_pass http://frontends;? ? ? ? ? ? ? ? ? ? ? ? ?
proxy_next_upstream error;
}
保存配置文件后执行 ?让nginx重启的指令 nginx -s reload(注意 nginx文件在不同发行版中位置有差别)
然后你就能够通过域名看到你的网站了,试试是不是快多了
注意:生产系统下开启多少个Tornado进程比较好呢,这个见仁见智了,据我压力测试的结果看来,用CPU核数*2的数量最好,再多 就浪费了没有提升(为什么乘2?因为有种CPU上的技术叫超线程)。我的VPS上用的4个进程。如果是8核IntelCPU要挖尽CPU潜能的话需要开16个进程