1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > nginx uwsgi部署django项目理论+实战

nginx uwsgi部署django项目理论+实战

时间:2019-07-20 14:38:08

相关推荐

nginx uwsgi部署django项目理论+实战

基本环境: Ubuntu 16.04.4 LTSpython 3.5 virtualenvdjango project deployable

#理论分析

部署django项目有很多方法,方法推荐可见 django官方文档-deployment,作为一个强迫症患者,当然是要选择一个既简单又高效的部署方式了。在网上找到了一张这个对比图:

由此我就基本决定用uwsgi部署我的django项目了,下面我们正式来介绍一下uwsgi以及相关必要理论。

##WSGI

WSGI,the Python Web Server Gateway Interface, 也就是说WSGI 是作为 Web 服务器与Web 应用程序或应用框架之间的一种低级别的接口,完成协议之间的转换。WSGI 是基于现存的 CGI 标准而设计的。Django框架自带了WSGI_Server,但是性能不好,所以自带的web server更多的是测试(debug)用途,发布时则使用生产环境的WSGI server。

从上图来看WSGI就像一个桥梁连接服务器和应用程序。当浏览器发送请求时,服务方调用应用方并提供环境信息,以及一个回调函数(提供给应用程序用来将消息头传递给服务器方),并接收Web内容作为返回值。故,我们可称WSGI是一个中间件,主要功能如下:

重写环境变量后,根据目标URL,将请求消息路由到不同的应用对象。

允许在一个进程中同时运行多个应用程序或应用框架。

负载均衡和远程处理,通过在网络上转发请求和响应消息。

进行内容后处理,例如应用XSLT样式表。

##uWSGI

uWSGI是一个Web服务器,它实现了WSGI协议、uwsgi、http等协议。Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换。而前述WSGI是一种Web服务器网关接口,是一个Web服务器(如nginx,uWSGI等服务器)与web应用通信的一种规范。

uWSGI的主要特点如下:

超快的性能低内存占用多app管理详尽的日志功能高度可定制(内存大小限制,服务一定次数后重启等)

##uwsgi

uwsgi是一个协议,是一个uWSGI服务器自有的协议,它用于定义传输信息的类型(type of information),每一个uwsgi packet前4byte为传输信息类型描述。而uWSGI是实现了uwsgi和WSGI两种协议的Web服务器。

nginx

所以到现在你应该清楚的明白一点,我们要配置一个 django项目,配置一个uWSGI服务器就够了。但是考虑到性能问题,我们在uWSGI服务器之前再加一个nginx服务器。为什么呢?原因很简单,因为nginx具备优秀的静态内容处理能力,所以静态内容由nginx自己处理;动态内容转发给uWSGI服务器,这样就可以达到更好的客户端响应。

#部署实战

根据上面的分析,我们的部署架构如下图所示:

uWSGI部署

uwsgi部署更详细请参考uwsgi官方文档,安装命令pip install uwsgi,这里我就简单列出条目:

# Django-related settings[uwsgi]socket = 127.0.0.1:8001# the base directory (full path)chdir = /share_dir/newsite/# Django s wsgi filewsgi-file= newsite/wsgi.py# process-related settings# mastermaster= true# maximum number of worker processesprocesses = 10enable-threads = truethreads= 1# ... with appropriate permissions - may be needed# chmod-socket = 664# clear environment on exitvacuum= truedaemonize = /share_dir/newsite/uwsgi.logpidfile=/share_dir/newsite/uwsgi.pid# 启动uwsgi的用户名和用户组uid=rootgid=root

nginx

 安装apt install nginx,之后修改配置文件如下:

server {# the port your site will be served onlisten80 default_server;# the domain name it will serve for#server_name 127.0.0.1; # substitute your machine's IP address or FQDNcharsetutf-8;# max upload sizeclient_max_body_size 75M; # adjust to taste# Django medialocation /media {alias /share_dir/newsite/media; # your Django project's media files - amend as required}location /static {alias /share_dir/newsite/global_static; # your Django project's static files - amend as required}# Finally, send all non-media requests to the Django server.location / {include /etc/nginx/uwsgi_params; # the uwsgi_params file you installeduwsgi_pass 127.0.0.1:8001;}}

uwsgi和nginx参数映射文件(uwsgi_params):

uwsgi_param QUERY_STRING $query_string;uwsgi_param REQUEST_METHOD$request_method;uwsgi_param CONTENT_TYPE $content_type;uwsgi_param CONTENT_LENGTH$content_length;uwsgi_param REQUEST_URI$request_uri;uwsgi_param PATH_INFO $document_uri;uwsgi_param DOCUMENT_ROOT $document_root;uwsgi_param SERVER_PROTOCOL$server_protocol;uwsgi_param UWSGI_SCHEME $scheme;uwsgi_param REMOTE_ADDR$remote_addr;uwsgi_param REMOTE_PORT$remote_port;uwsgi_param SERVER_PORT$server_port;uwsgi_param SERVER_NAME$server_name;

然后再介绍几个常用命令:

// uwsgi命令ps -ax | grep uwsgi //查看uwsgi运行进程uwsgi yourfile.ini //启动uwsgi服务,必须说明这是其中一种方式,其他的请参考官方文档uwsgi --stop uwsgi.pid // 停止服务uwsgi --reload uwsgi.pid // 重启服务// nginx 命令systemctl status/stop/restart/start nginx.services

必须说这个总结的太简单,当时部署我是做了大量的工作,比如了解nginx的工作模型等,有时间我再总结!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。