搭建Tornado+Nginx

  Tornado一个高效的异步非阻塞式的实时Web服务器,是Facebook旗下的 FriendFeed 网站开源Web服务器版本。但是它内置的HTTP服务器功能有限,不能在生产环境下使用。

  

  在 FriendFeed 中,他们使用Nginx做负载均衡和静态文件伺服。 多台服务器上,同时部署了多个 Tornado 实例,通常,一个 CPU 内核 会对应一个 Tornado 线程。因为Tornado是跑在负载均衡服务器(如 nginx)后面的,所以需要把 xheaders=True 传到 HTTPServer的构造器当中去。这是为了让 Tornado 使用 X-Real-IP 这样的的 header 信息来获取用户的真实 IP地址,如果使用传统 的方法,你只能得到这台负载均衡服务器的 IP 地址。

  下面是 nginx 配置文件的一个示例,整体上与FriendFeed 中使用的差不多

  我新建了一个 tornado-simple 的项目,这是我的目录结构

 

 

  static目录里面存放静态文件,到时候在配置Nginx时静态目录时,就指向该目录。

  app.py的代码

 1 # -*- coding: utf-8 -*-
 2 
 3 import tornado.ioloop
 4 import tornado.web
 5 
 6 
 7 class MainHandler(tornado.web.RequestHandler):
 8     def get(self):
 9         self.write("Hello, world")
10 
11 
12 
13 app = tornado.web.Application([
14     (r"/", MainHandler),
15 ])
16 if __name__ == "__main__":
17     app.listen(8080)
18     tornado.ioloop.IOLoop.instance().start()

  下面是nginx.conf配置

 1 # 工作线程数
 2 worker_processes  1;
 3 
 4 # 错误日志
 5 error_log  logs/error.log;
 6 
 7 # pid
 8 pid        logs/nginx.pid;
 9 
10 #连接数
11 events {
12     worker_connections  1024;
13 }
14 
15 # 配置HTTP
16 http {
17     #上行的前端
18     upstream frontends{
19         server 127.0.0.1:8080;
20     }
21     #可识别的媒体类型
22     include       mime.types;
23     default_type  application/octet-stream;
24     access_log  logs/access.log;
25     
26     # 是否可发送文件 
27     sendfile        on;
28     # 长连接
29     keepalive_timeout  65;
30     # 代理超时
31     proxy_read_timeout 200;
32     #关闭tcp push
33     tcp_nopush on;
34     # 关闭tcp延迟
35     tcp_nodelay on;
36     # 启用gzip压缩算法
37     gzip on;
38     gzip_min_length 1000;
39     gzip_proxied any;
40     gzip_types text/plain  text/css text/xml
41                application/x-javascript application/xml
42                application/atom+xml text/javascript;
43     proxy_next_upstream error;
44     
45     # 监听服务器配置
46     server {
47         listen       80;
48         server_name  localhost;
49         #允许文件上传的最大大小
50         client_max_body_size 50M;
51         
52         # 指定静态文件映射
53         location ^~ /static/ {
54             root 你自己的目录/tornado-simple/;
55             if ($query_string) {
56                 expires max;
57             }
58             index  index.html index.htm;
59         }
60         # 重写 favicon.ico
61         location = /favicon.ico {
62             rewrite (.*) /static/favicon.ico;
63         }
64         # 指定默认的错误页面
65         error_page   500 502 503 504  /50x.html;
66         location = /50x.html {
67             root   html;
68         }
69         
70         #代理配置
71         location / {
72             proxy_pass_header Server;
73             proxy_set_header Host $http_host;
74             proxy_redirect off;
75             proxy_set_header X-Real-IP $remote_addr;
76             proxy_set_header X-Scheme $scheme;
77             proxy_pass http://frontends;
78         }
79     }
80 }

  启动Tornado

1 python app.py

  启动nginx

1 sudo nginx

  

  

posted on 2014-04-04 21:55  Arts&Crafts  阅读(2094)  评论(0编辑  收藏  举报

导航