使用Nginx+Tornado部署Django应用

1.nginx.conf文件配置

1 # For more information on configuration, see:
  2 #   * Official English Documentation: http://nginx.org/en/docs/
  3 #   * Official Russian Documentation: http://nginx.org/ru/docs/
  4 
  5 user root;
  6 worker_processes auto;
  7 error_log /var/log/nginx/error.log;
  8 pid /run/nginx.pid;
  9 
 10 # Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
 11 include /usr/share/nginx/modules/*.conf;
 12 
 13 events {
 14     worker_connections 1024;
 15 }
 16 
 17 http {
 18     log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
 19                       '$status $body_bytes_sent "$http_referer" '
 20                       '"$http_user_agent" "$http_x_forwarded_for"';
 21 
 22     access_log  /var/log/nginx/access.log  main;
 23 
 24     sendfile            on;
 25     tcp_nopush          on;
 26     tcp_nodelay         on;
 27     keepalive_timeout   65;
 28     types_hash_max_size 2048;
 29 
 30     include             /etc/nginx/mime.types;
 31     default_type        application/octet-stream;
 32 
 33     # Load modular configuration files from the /etc/nginx/conf.d directory.
 34     # See http://nginx.org/en/docs/ngx_core_module.html#include
 35     # for more information.
 36     include /etc/nginx/conf.d/*.conf;
 37     
 38     upstream tornado-backend {  # 将HTTP请求转发到Tornado服务器
 39         server localhost:6001;
 40         server localhost:6002;
 41         server localhost:6003;
 42         server localhost:6004;
 43     }
 44 
 45     server {
 46         listen       80 default_server;
 47         listen       [::]:80 default_server;
 48         server_name  _;
 49         root         /usr/share/nginx/html;
 50 
 51         # Load configuration files for the default server block.
 52         include /etc/nginx/default.d/*.conf;
 53         
 54         location / {
 55             proxy_pass_header Server;
 56             proxy_set_header Host $http_host;
 57             proxy_redirect off;
 58             proxy_set_header X-Real-IP $remote_addr;
 59             proxy_set_header X-Scheme $scheme;
 60             proxy_pass http://tornado-backend;  # 上面定义的tornado-backend
 61         }
 62         
 63         location /staticfiles/ {
 64             root /root/zanhu/zanhu;  # staticfiles文件所在的目录路径
 65         }
 66 
 67         location /media/ {
 68             root /root/zanhu/zanhu;  # media文件所在的目录路径
 69         }
 70 
 71         error_page 404 /404.html;
 72             location = /40x.html {
 73         }
 74 
 75         error_page 500 502 503 504 /50x.html;
 76             location = /50x.html {
 77         }
 78     }

2.项目的根目录建立TornadoServer.py

 1 import os
 2 import sys
 3 
 4 from django.core.wsgi import get_wsgi_application
 5 
 6 from tornado.options import options, define
 7 import tornado.httpserver
 8 import tornado.ioloop
 9 import tornado.web
10 import tornado.wsgi
11 
12 
13 # Django Application加入查找路径中
14 app_path = os.path.abspath(
15     os.path.join(os.path.dirname(os.path.abspath(__file__)), os.pardir)
16 )
17 sys.path.append(os.path.join(app_path, "zanhu"))
18 # 定义tornado服务的端口
19 define("port", default=6000, type=int, help="run on the given port")
20 
21 
22 def main():
23     tornado.options.parse_command_line()
24     # 指定配置生产环境配置文件
25     os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.production")
26     # 用tornado的WSGI容器运行django应用实现的WSGI服务
27     wsgi_app = tornado.wsgi.WSGIContainer(get_wsgi_application())
28     # xheaders=True, 后端可以接收到客户端的IP
29     http_server = tornado.httpserver.HTTPServer(wsgi_app, xheaders=True)
30     # 监听
31     http_server.listen(options.port)
32     tornado.ioloop.IOLoop.instance().start()
33 
34 
35 if __name__ == '__main__':
36     main()

3.在项目的环境下创建4个Tornado进程并运行

(zanhu) [root@192 zanhu]# python TornadoServer.py --port=6001
^Z
[1]+  Stopped                 python TornadoServer.py --port=6001
(zanhu) [root@192 zanhu]# python TornadoServer.py --port=6002
^Z
[2]+  Stopped                 python TornadoServer.py --port=6002
(zanhu) [root@192 zanhu]# python TornadoServer.py --port=6003
^Z
[3]+  Stopped                 python TornadoServer.py --port=6003
(zanhu) [root@192 zanhu]# python TornadoServer.py --port=6004
^Z
[4]+  Stopped                 python TornadoServer.py --port=6004
(zanhu) [root@192 zanhu]# jobs
[1]   Stopped                 python TornadoServer.py --port=6001
[2]   Stopped                 python TornadoServer.py --port=6002
[3]-  Stopped                 python TornadoServer.py --port=6003
[4]+  Stopped                 python TornadoServer.py --port=6004
(zanhu) [root@192 zanhu]# bg 1 2 3 4
[1] python TornadoServer.py --port=6001 &
[2] python TornadoServer.py --port=6002 &
[3]- python TornadoServer.py --port=6003 &
[4]+ python TornadoServer.py --port=6004 &
(zanhu) [root@192 zanhu]# 

4.启动Nginx

systemctl restart nginx

猜你喜欢

转载自www.cnblogs.com/listash/p/12465356.html