程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Deploy Django applications with nginx+tornado

編輯:Python

  Operation background , Have learned Tornado function Django application

Tornado function / Deploy Django_ A blog of Yue Wangchao -CSDN Blog We use nginx+tornado+django Technology stack to realize project deployment .https://blog.csdn.net/qq_36564503/article/details/122852990

1.nginx.conf File configuration

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 { # take HTTP Request forwarding to Tornado The server
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; # As defined above tornado-backend
61 }
62
63 location /staticfiles/ {
64 root /root/zanhu/zanhu; # staticfiles The path to the directory where the file is located
65 }
66
67 location /media/ {
68 root /root/zanhu/zanhu; # media The path to the directory where the file is located
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. The root directory of the project is established 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 Join the search path
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 # Definition tornado Port of service
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 # Specify the production environment configuration file
25 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.production")
26 # use tornado Of WSGI Container operation django Application implementation WSGI service
27 wsgi_app = tornado.wsgi.WSGIContainer(get_wsgi_application())
28 # xheaders=True, The back end can receive the from the client IP
29 http_server = tornado.httpserver.HTTPServer(wsgi_app, xheaders=True)
30 # monitor
31 http_server.listen(options.port)
32 tornado.ioloop.IOLoop.instance().start()
33
34
35 if __name__ == '__main__':
36 main()

3. Create in the context of the project 4 individual Tornado Process and run

(zanhu) [[email protected] zanhu]# python TornadoServer.py --port=6001
^Z
[1]+ Stopped python TornadoServer.py --port=6001
(zanhu) [[email protected] zanhu]# python TornadoServer.py --port=6002
^Z
[2]+ Stopped python TornadoServer.py --port=6002
(zanhu) [[email protected] zanhu]# python TornadoServer.py --port=6003
^Z
[3]+ Stopped python TornadoServer.py --port=6003
(zanhu) [[email protected] zanhu]# python TornadoServer.py --port=6004
^Z
[4]+ Stopped python TornadoServer.py --port=6004
(zanhu) [[email protected] 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) [[email protected] 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) [[email protected] zanhu]#

4. start-up Nginx

systemctl restart nginx

  The current problem is that if you close xshell Connect , Corresponding Tornado The process will be shut down , Will be inaccessible !

How can we make Tornado Running in the background ?


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved