django开发的服务器只能用于开发环境,在生产环境时候直接使用有安全以及性能问题。
nginx很好得解决这一问题, uwsgi为nginx和django直接搭建一个桥梁。
步骤1:
安装nginx:
| sudo apt-get install nginx |
启动停止命令:
| fnngj@ubuntu:~$ /etc/init.d/nginx start #启动 fnngj@ubuntu:~$ /etc/init.d/nginx stop #关闭 fnngj@ubuntu:~$ /etc/init.d/nginx restart #重启 |
先看看能否正常访问:
![nginx + uWSGI 为 django 提供高并发](https://www.zhikugroup.com/zb_users/upload/2018/12/20181227101023_91814.png)
2 。 安装uwsgi
先创建一个简单脚本test.py测试uwsgi:
| def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] |
保存,运行
| uwsgi --http :8001 --wsgi-file test.py |
这里实现了个http Server 开放的http端口 8001
访问 127.0.0.1:8001
![nginx + uWSGI 为 django 提供高并发](https://www.zhikugroup.com/zb_users/upload/2018/12/20181227101024_71603.png)
访问成功说明uwsgi基本没问题,再测试django工程;
先找到django工程中的 wsgi.py文件 ,在创建工程文件的时候应该自动创建好了:
假设我的django工程目录是这样的:
myDjangoProject/
├── manage.py
├── myweb/
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ └── wsgi.py
└── myweb_uwsgi.ini
wsgi.py基本上是这个样子:
| from __future__ import unicode_literals import os os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myweb.settings") from django.core.wsgi import get_wsgi_application application = get_wsgi_application() |
命令行输入:
| uwsgi --http :8001 --chdir /myDjangoProject/ --wsgi-file myweb/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191 |
常用选项:
http : 协议类型和端口号
processes : 开启的进程数量
workers : 开启的进程数量,等同于processes(官网的说法是spawn the specified number ofworkers / processes)
chdir : 指定运行目录(chdir to specified directory before apps loading)
wsgi-file : 载入wsgi-file(load .wsgi file)
stats : 在指定的地址上,开启状态服务(enable the stats server on the specified address)
threads : 运行线程。由于GIL的存在,我觉得这个真心没啥用。(run each worker in prethreaded mode with the specified number of threads)
master : 允许主进程存在(enable master process)
daemonize : 使进程在后台运行,并将日志打到指定的日志文件或者udp服务器(daemonize uWSGI)。实际上最常用的,还是把运行记录输出到一个本地文件上。
pidfile : 指定pid文件的位置,记录主进程的pid号。
vacuum : 当服务器退出的时候自动清理环境,删除unix socket文件和pid文件(try to remove all of the generated file/sockets)
此处如果输入没有问题,访问127.0.0.1:8001 能够正常访问网站
输入长长的命令行会稍显麻烦,此处可以创建一个配置文件myweb_uwsgi.ini
内容如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | # myweb_uwsgi.ini file [uwsgi] # Django-related settings socket = :8000 # the base directory (full path) chdir = /home/myDjangoProject # Django s wsgi file module = myweb.wsgi # process-related settings # master master = true # maximum number of worker processes processes = 4 # ... with appropriate permissions - may be needed # chmod-socket = 664 # clear environment on exit vacuum = true |
输入命令:
然而此处对我并没有X用,提示:
| The -s/--socket option is missing and stdin is not a socket. |
此处百思不得其解,幸好使用 命令同样效果:
| uwsgi --http :8001 --chdir /myDjangoProject/ --wsgi-file myweb/wsgi.py --master --processes 4 --threads 2 --stats 127.0.0.1:9191 -d /var/log/uwsgi.log |
3.下面配置nginx:
编辑nginx配置文件:
| nano /etc/nginx/nginx.conf |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | server { listen 80; server_name 127.0.0.1 charset UTF-8; access_log /var/log/nginx/myweb_access.log; error_log /var/log/nginx/myweb_error.log; client_max_body_size 75M; location / { include uwsgi_params; uwsgi_pass 127.0.0.1:8000; uwsgi_read_timeout 2; #try_files $uri $uri/ =404; #这里切忌不要加上这行,因为优先等级较高,子路径很可能会返回404.不会转发到socket端口 } location /static { expires 30d; autoindex on; add_header Cache-Control private; alias /home/static/; } } …… |
访问 127.0.0.1 查看效果,配置完成
补充:uwsgin 对于url 长度有限制,会丢弃超长url链接,这时候要增加缓存,如果是配置文件ini ,增加buffer-size=32768
如果是命令行中,增加 -b 32768
如果使用mysql数据库,当数据库长时间没有连接是,数据链接会关闭,访问网站会返回500错误,可以在参数加上 --lazy-apps 选项
参考资料: http://uwsgi-docs-zh.readthedocs.io/zh_CN/latest/tutorials/Django_and_nginx.html
英文版本:http://uwsgi-docs.readthedocs.io/en/latest/tutorials/Django_and_nginx.html
评论专区