http://www.runoob.com/django/django-nginx-uwsgi.html
http://www.cnblogs.com/my_life/articles/6479568.html
http://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/tutorials/Django_and_nginx.html
在django中的view里设置的全局变量只是针对当前请求而言,新来的一个请求其全局变量仍然是初始值,跟上一次请求做的修改没有任何关系。 我的理解:本质上就是一个cgi程序,执行完就结束,前后没有任何的关系。
即使在settings.py里的变量也不行,也达不到全局变量的效果,并且只能读,不同请求对变量的修改不会被共享。
http://www.cnblogs.com/my_life/articles/7602395.html
一个web服务器面对的是外部世界。它能直接从文件系统提供文件 (HTML, 图像, CSS等等)。然而,它无法 *直接*与Django应用通信;它需要借助一些工具的帮助,这些东西会运行运用,接收来自web客户端(例如浏览器)的请求,然后返回响应。
一个Web服务器网关接口(Web Server Gateway Interface) - WSGI - 就是干这活的。 WSGI 是一种Python标准。
uWSGI是一种WSGI实现。在这个教程中,我们将设置uWSGI,让它创建一个Unix socket,并且通过WSGI协议提供响应到web服务器。最后,我们完整的组件栈看起来将是这样的:
the web client <-> the web server <-> the socket <-> uwsgi <-> Django
在你开始设置uWSGI之
===========================================================
在前面的章节中我们使用 python manage.py runserver 来运行服务器。这只适用测试环境中使用。
正式发布的服务,我们需要一个可以稳定而持续的服务器,比如apache, Nginx, lighttpd等,本文将以 Nginx 为例。
安装基础开发包
Centos 下安装步骤如下:
yum groupinstall "Development tools" yum install zlib-devel bzip2-devel pcre-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel
CentOS 自带 Python 2.4.3,但我们可以再安装Python2.7.5:
cd ~ wget http://python.org/ftp/python/2.7.5/Python-2.7.5.tar.bz2 tar xvf Python-2.7.5.tar.bz2 cd Python-2.7.5 ./configure --prefix=/usr/local make && make altinstall
安装django
http://www.runoob.com/django/django-install.html
/path/to/python/pip install django
[root@solar django]# python Python 2.7.3 (default, May 15 2014, 14:49:08) [GCC 4.8.0] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import django >>> django.VERSION (1, 6, 5, 'final', 0) >>>
我们可以看到输出了Django的版本号,说明安装成功。
新建一个django工程
django-admin.py startproject mysite
cd mysite
安装 测试uwsgi
uwsgi:https://pypi.python.org/pypi/uWSGI
uwsgi 参数详解:http://uwsgi-docs.readthedocs.org/en/latest/Options.html
pip install uwsgi
uwsgi --version # 查看 uwsgi 版本
测试 uwsgi 是否正常:
新建 test.py 文件,内容如下:
def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return "Hello World"
然后在终端运行:
uwsgi --http :8001 --wsgi-file test.py
在浏览器内输入:http://127.0.0.1:8001,查看是否有"Hello World"输出,若没有输出,请检查你的安装过程。
使用uwsgi启动一个http服务,监听在8001端口,把收到的来自客户端的http请求转发给test.py进行处理。 工作方式如下:
the web client <-> uWSGI <-> Python
测试你的Django项目
现在,我们想让uWSGI做同样的事,但是返回一个Django站点(使用django python库写的东西)而不是 test.py
模块。
如果你还没有这样做,那么先请确保你的 mysite
项目实际上正常工作:
python manage.py runserver 0.0.0.0:8000 //测试django站点是否正常
python manage.py runserver 这种方式只适用测试环境中使用。
如果正常,则使用uWSGI来运行它:
uwsgi --http :8000 --module mysite.wsgi
此时的工作方式是: the web client <-> uWSGI <-> Django
通常我们不会让浏览器直接与uWSGI通信。那是web服务器的工作。
安装nginx
为你的站点配置nginx
你会需要 uwsgi_params
文件,可用在uWSGI发行版本的 nginx
目录下,或者从https://github.com/nginx/nginx/blob/master/conf/uwsgi_params 找到。
# mysite_nginx.conf # the upstream component nginx needs to connect to upstream django { # server unix:///path/to/your/mysite/mysite.sock; # for a file socket server 127.0.0.1:8001; # for a web port socket (we'll use this first) } # configuration of the server server { # the port your site will be served on listen 8000; # the domain name it will serve for server_name .example.com; # substitute your machine's IP address or FQDN charset utf-8; # max upload size client_max_body_size 75M; # adjust to taste # Django media location /media { alias /path/to/your/mysite/media; # your Django project's media files - amend as required } location /static { alias /path/to/your/mysite/static; # your Django project's static files - amend as required } # Finally, send all non-media requests to the Django server. location / { uwsgi_pass django; include /path/to/your/mysite/uwsgi_params; # the uwsgi_params file you installed } }
nginx和uWSGI以及test.py
让nginx对 test.py
应用说句”hello world”吧。
uwsgi --socket :8001 --wsgi-file test.py
这几乎与之前相同,除了这次有一个选项不同:
socket :8001
: 使用uwsgi协议,端口为8001
同时,已经配置了nginx在那个端口与uWSGI通信,而对外使用8000端口。此时的工作方式:
the web client <-> the web server(监听在8000端口) <-> the socket <-> uWSGI(监听在8001端口) <-> Python
使用Unix socket而不是端口
目前,我们使用了一个TCP端口socket,因为它简单些,但事实上,使用Unix socket会比端口更好 - 开销更少。
编辑 mysite_nginx.conf
, 修改它以匹配:
server unix:///path/to/your/mysite/mysite.sock; # for a file socket # server 127.0.0.1:8001; # for a web port socket (we'll use this first)
然后重启nginx.
再次运行uWSGI:
uwsgi --socket mysite.sock --wsgi-file test.py
这次, socket
选项告诉uWSGI使用哪个文件。
配置uWSGI以允许.ini文件
uwsgi 配置
uwsgi支持ini、xml等多种配置方式,本文以 ini 为例, 在/ect/目录下新建uwsgi9090.ini,添加如下配置:
[uwsgi] socket = 127.0.0.1:9090 //#与前端nginx进行通信的端口 master = true //主进程 vhost = true //多站模式 no-site = true //多站模式时不设置入口模块和文件 workers = 2 //子进程数 reload-mercy = 10 vacuum = true //退出、重启时清理文件 max-requests = 1000 limit-as = 512 buffer-size = 30000 pidfile = /var/run/uwsgi9090.pid //pid文件,用于下面的脚本启动、停止该进程 daemonize = /website/uwsgi9090.log //日志保存的地方
然后使用这个文件运行uswgi:
uwsgi --ini mysite_uwsgi.ini # the --ini option is used to specify a file
Nginx 配置
找到nginx的安装目录(如:/usr/local/nginx/),打开conf/nginx.conf文件,修改server配置:
server { listen 80; server_name localhost; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:9090; //必须和uwsgi中的设置一致 uwsgi_param UWSGI_SCRIPT demosite.wsgi; //入口文件,即wsgi.py相对于项目根目录的位置,“.”相当于一层目录 uwsgi_param UWSGI_CHDIR /demosite; //项目根目录 index index.html index.htm; client_max_body_size 35m; } }
你可以阅读 Nginx 安装配置 了解更多内容。
设置完成后,在终端运行:
uwsgi --ini /etc/uwsgi9090.ini & /usr/local/nginx/sbin/nginx
在浏览器输入:http://127.0.0.1,你就可以看到 django 的 "It work" 了。
数据流向:
the web client <-> the web server <-> the socket <-> uwsgi <-> Django
======
以上的Nginx和uwsgi的配置,经测试发现不行,可能是我的配置的问题
https://www.zhihu.com/question/27295854
链接:https://www.zhihu.com/question/27295854/answer/37184556
来源:知乎
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
- 创建一个django项目
django-admin.py startproject hello
hello/ ├── hello │ ├── __init__.py │ ├── settings.py │ ├── urls.py │ └── wsgi.py └── manage.py
- 配置uwsgi.ini,不清楚的配置自行搜索
[uwsgi] chdir=/home/liaoziqian/hello //django创建的工程目录 uid=nobody gid=nobody module=hello.wsgi:application //关键之处,让uwsgi能和django的工程联动起来; 否则会出现internal error; no python application found, check your startup logs for errors socket=/home/liaoziqian/hello/uwsgi.sock //关键之处,经测试发现tcp socket不行 master=true workers=5 pidfile=/home/liaoziqian/hello/uwsgi.pid vacuum=true thunder-lock=true //惊群效应避免 enable-threads=true harakiri=30 post-buffering=4096 daemonize=/home/liaoziqian/hello/uwsgi.log
- uwsgi相关命令
启动uwsgi:uwsgi --ini uwsgi.ini 停止uwsgi:uwsgi --stop uwsgi.pid 重新加载配置:uwsgi --reload uwsgi.pid
- nginx配置
server { listen 8080; location / { include uwsgi_params; uwsgi_connect_timeout 30; uwsgi_pass unix:/home/liaoziqian/hello/uwsgi.sock; } }
- 启动uwsgi,启动nginx,一个简单的nginx + uwsgi + django示例就完成了
http://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/WSGIquickstart.html
添加并发和监控
http://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/WSGIquickstart.html
你想进行的第一个调整可能是增加并发性 (默认情况下,uWSGI启动一个单一的进程和一个单一的线程)。
你可以用 --processes
选项添加更多的进程,或者使用 --threads
选项添加更多的线程 (或者可以同时添加)。
master=true //最好同时带上该配置,子进程挂掉的话会重新被拉起,否则会出现[uwsgi] <defunct>僵尸子进程
web应用部署的一个常见问题是“卡住的请求”。你所有的线程/worker都卡住了 (请求阻塞) ,而你的应用无法接收更多的请求。要避免这个问题,你可以设置一个 harakiri
定时器。它是一个监控器 (由master进程管理),会摧毁那些卡住超过指定秒数的进程 (小心选择 harakiri
值)。
harakiri = 30
重启uwsgi
There are several ways to make uWSGI gracefully restart.
# using kill to send the signal
kill -HUP `cat /tmp/project-master.pid` # or the convenience option --reload uwsgi --reload /tmp/project-master.pid # or if uwsgi was started with touch-reload=/tmp/somefile touch /tmp/somefile
Or from your application, in Python:
uwsgi.reload()
Or in Ruby,
UWSGI.reload
停止uwsgi
kill -INT `cat /tmp/project-master.pid` # or for convenience... uwsgi --stop /tmp/project-master.pid
http://www.cnblogs.com/my_life/articles/7606981.html
WSGI 是分成 server 和 framework (即 application) 两部分 ,严格说 WSGI 只是一个协议, 规范 server 和 framework 之间连接的接口。
WSGI framework 就是我们经常提到的 Django 这种框架。
the web client <-> the web server <-> the socket <-> uwsgi <-> Django
惊群现象出现在这样的情况:主进程绑定并监听socket,然后调用fork,在各个子进程进行accept。无论任何时候,只要有一个连接尝试连接,所有的子进程都将被唤醒,但只有一个会连接成功,其他的会得到一个EAGAIN的错误,浙江导致巨大的CPU资源浪费,如果在进程中使用线程,这个问题被再度放大。一个解决方法是串行化accept,在accept前防止一个锁。
--log-maxsize <bytes>
[uwsgi] socket=/data/ffmpeg_monitor/ffmpeg_monitor.sock chdir=/data/ffmpeg_monitor module=ffmpeg_monitor.wsgi master=true processes=10 thunder-lock=true harakiri=30 chmod-socket=666 pidfile=/var/run/uwsgi_ffmpeg_monitor.pid daemonize=/data/ffmpeg_monitor/log/ffmpeg_monitor_uwsgi.log log-maxsize=20000