nginx + uWSGI 为 django 提供高并发
django 的并发能力真的是令人担忧,这里就使用 nginx + uwsgi 提供高并发
nginx 的并发能力超高,单台并发能力过万(这个也不是绝对),在纯静态的 web 服务中更是突出其优越的地方,由于其底层使用 epoll 异步IO模型进行处理,使其深受欢迎
做过运维的应该都知道,php 需要使用 nginx + fastcgi 提供高并发,java 需要使用 nginx + tomcat 提供 web 服务
下面介绍如何使用 nginx + uwsgi 为 django 提供高并发 web 服务
1、系统环境
[root@crazy-acong ~]# uname -a
Linux crazy-acong 2.6.-.el6.x86_64 # SMP Wed Oct :: UTC x86_64 x86_64 x86_64 GNU/Linux
[root@crazy-acong ~]# cat /etc/redhat-release
CentOS release 6.6 (Final)
2、python 及 django 版本
[root@crazy-acong ~]# python3 --version
Python 3.4.
[root@crazy-acong ~]# django-admin --version
1.10.
3、安装 uwsgi 及 测试 uwsgi
3.1 安装
[root@crazy-acong ~]# pip3 install uwsgi
3.2 测试 uwsgi 提供 web 服务的功能
# 创建 test.py 文件
def application(env, start_response):
start_response('200 OK', [('Content-Type','text/html')])
return [b"Hello World"] # python3
#return ["Hello World"] # python2 # 启动 uwsgi 服务
[root@crazy-acong ~]# uwsgi --http : --wsgi-file test.py # 查看启动进程
[root@crazy-acong ~]# netstat -lnpt | grep uwsgi
tcp 127.0.0.1: 0.0.0.0:* LISTEN /uwsgi
tcp 0.0.0.0: 0.0.0.0:* LISTEN /uwsgi # 在浏览器中访问 http://ip:8000 就可以看到 Hello World 字样了
3.3 将启动参数写入到配置文件中,然后进行启动 django 程序
3.3.1 创建 uwsgi 配置文件
[root@crazy-acong ~]# cd /data/django_test # 进入到 django 的主目录 [root@crazy-acong django_test]# cat test-uwsgi.ini
[uwsgi]
# 对外提供 http 服务的端口
http = : #the local unix socket file than commnuincate to Nginx 用于和 nginx 进行数据交互的端口
socket = 127.0.0.1: # the base directory (full path) django 程序的主目录
chdir = /data/django_test # Django's wsgi file
wsgi-file = django_test/wsgi.py # maximum number of worker processes
processes = #thread numbers startched in each worker process
threads = #monitor uwsgi status 通过该端口可以监控 uwsgi 的负载情况
stats = 127.0.0.1: # clear environment on exit
vacuum = true # 后台运行,并输出日志
daemonize = /var/log/uwsgi.log
3.3.2 通过 uwsgi 配置文件启动 django 程序
# 通过配置文件启动 django 程序
[root@crazy-acong django_test]# /usr/local/bin/uwsgi test-uwsgi.ini # 在浏览器中 通过访问 http://ip:9000 可以看到发布的 django 程序
4、安装 nginx
nginx 安装参考 http://www.cnblogs.com/CongZhang/p/6548570.html
5、配置 nginx 的配置文件
在 django 的主目录下创建下面的 nginx 配置文件,然后做软连接到 nginx 的配置文件目录,或者直接在 nginx 配置文件目录中添加该文件也可以
5.1 创建 nginx 配置文件
[root@crazy-acong django_test]# cat /data/django_test/django-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:; # for a web port socket (we'll use this first)
} # configuration of the server
server {
# the port your site will be served on
listen ;
# the domain name it will serve for
server_name .example.com; # substitute your machine's IP address or FQDN
charset utf-; # 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 /data/django_test/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 /data/django_test/uwsgi_params; # the uwsgi_params file you installed
}
}
5.2 重启nginx 服务
[root@crazy-acong django_test]# nginx -t
nginx: the configuration file /data/application/nginx-1.10./conf/nginx.conf syntax is ok
nginx: configuration file /data/application/nginx-1.10./conf/nginx.conf test is successful
[root@crazy-acong django_test]# nginx -s reload [root@crazy-acong django_test]# netstat -lnpt | grep
tcp 0.0.0.0: 0.0.0.0:* LISTEN /nginx
这个时候就可以通过 http://ip:8000 访问 django 程序了,不过目前还存在一个问题,访问 http://ip:8000/admin 发现静态文件貌似没读取到,需要通过下面的方法解决静态文件的问题
6、解决 django 多 app 静态文件的问题
# 在 django 程序的 settings.py 文件中添加以下内容 STATIC_ROOT = os.path.join(BASE_DIR, "static_all") # 然后通过执行该命令,将静态文件整合到一个目录中
[root@crazy-acong django_test]# python3 manage.py collectstatic [root@crazy-acong django_test]# ll
total
drwxr-xr-x nginx games Mar : app01
-rw-r--r-- root root Mar : db.sqlite3
-rw-r--r-- root root Mar : django-nginx.conf
drwxr-xr-x nginx games Mar : django_test
-rwxr-xr-x nginx games Mar : manage.py
drwxr-xr-x nginx games Mar : static
drwxr-xr-x root root Mar : static_all # 此时会发现多了一个该目录,所有 app 的静态文件都整合到这一个目录中了
drwxr-xr-x nginx games Mar : templates
-rw-r--r-- root root Mar : test-uwsgi.ini
-rw-r--r-- root root Mar : uwsgi_params
然后需要修改 nginx 配置文件中 指向 django 静态目录的配置文件
[root@crazy-acong django_test]# cat /data/django_test/django-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:; # for a web port socket (we'll use this first)
} # configuration of the server
server {
# the port your site will be served on
listen ;
# the domain name it will serve for
server_name .example.com; # substitute your machine's IP address or FQDN
charset utf-; # 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 /data/django_test/static_all; # your Django project's static files - amend as required
} # Finally, send all non-media requests to the Django server.
location / {
uwsgi_pass django;
include /data/django_test/uwsgi_params; # the uwsgi_params file you installed
}
}
最后重启 nginx 服务即可
[root@crazy-acong django_test]# nginx -t
nginx: the configuration file /data/application/nginx-1.10./conf/nginx.conf syntax is ok
nginx: configuration file /data/application/nginx-1.10./conf/nginx.conf test is successful
[root@crazy-acong django_test]# nginx -s reload
nginx + uWSGI 为 django 提供高并发的更多相关文章
- nginx + uwsgi 部署 Django+Vue项目
nginx + uwsgi 部署 Django+Vue项目 windows 本地 DNS 解析 文件路径 C:\Windows\System32\drivers\etc 单机本地测试运行方式,调用dj ...
- 填坑!!!virtualenv 中 nginx + uwsgi 部署 django
一.为什么会有这篇文章 第一次接触 uwsgi 和 nginx ,这个环境搭建,踩了太多坑,现在记录下来,让后来者少走弯路. 本来在 Ubuntu14.04 上 搭建好了环境,然后到 centos7. ...
- Python3.6+nginx+uwsgi部署Django程序到阿里云Ubuntu16.04系统
Python3.6+nginx+uwsgi部署Django程序到阿里云Ubuntu16.04系统 这个是写好的Django程序在本地机运行的情况,一个查询接口. 准备工作 1.首先购买一台阿里云的EC ...
- CentOS 环境下基于 Nginx uwsgi 搭建 Django 站点
因为我的个人网站 restran.net 已经启用,博客园的内容已经不再更新.请访问我的个人网站获取这篇文章的最新内容,CentOS 环境下基于 Nginx uwsgi 搭建 Django 站点 以下 ...
- nginx简介(轻量级开源高并发web服务器:大陆使用者百度、京东、新浪、网易、腾讯、淘宝等)(并发量5w)(一般网站apache够用了,而且稳定)
nginx简介(轻量级开源高并发web服务器:大陆使用者百度.京东.新浪.网易.腾讯.淘宝等)(并发量5w)(一般网站apache够用了,而且稳定) 一.总结 1.在连接高并发的情况下,Nginx是A ...
- Nginx+uWSGI+Python+Django构建必应高清壁纸站
写在前面 做这个网站的初衷是因为,每次打开必应搜索搜东西的时候都会被上面的背景图片吸引,我想必应的壁纸应该是经过专业人员精选出来的,我甚至会翻看以前的历史图片,唯一美中不足的是必应的首页只能查看最多7 ...
- Nginx + uWSGI 部署Django 项目,并实现负载均衡
一.uWSGI服务器 uWSGI是一个Web服务器,它实现了WSGI协议.uwsgi.http等协议.Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换. 要注意 WSGI ...
- 学习VirtualEnv和Nginx+uwsgi用于django项目部署
以下叙述中用到的操作系统:Linux CentOS 6.X. 最近几天了解一下VirtualEnv,Apache+Daemon mode,Nginx+uwsgi的概念,并且在项目中实验性部署了一下(目 ...
- nginx+uwsgi部署Django项目到Ubuntu服务器全过程,以及那些坑!!!
前言:自己在windows上用PyCharm编写的Django项目,编写完后在windows上运行一点问题都没有,但是部署到服务器上时却Bug百出.百度,CSDN,sf,各种搜索寻求解决方案在历时3天 ...
随机推荐
- 倍福TwinCAT(贝福Beckhoff)基础教程2.0 TwinCAT常用快捷键
F5:运行程序 CTRL+F5:停止运行当前程序 F12:登出 F11:登录 CTRL+F7:强制写入数值 更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.you ...
- iOS 引入外部字体 otf/ttf/ttc
1.首先下载到字体的otf文件(Mac电脑下搜索字体册) 2.将字体文件拖到项目工程下 3.plist设置 Fonts provided by application 属性 4.代码中使用[UIFon ...
- swoole编译安装/数据库连接池/异步mysql客户端
一.编译安装php5.6 0.安装必要软件 http://www.cnblogs.com/itfenqing/p/6055138.html 1.下载php5.6.30 http://php.net/d ...
- RAP + MOCK
前后端分离式开发的思考 目前大部分公司都实行了前后端分离开发.然而在项目开发过程当中,经常会遇到以下几个尴尬的场景: 1.前端开发依赖于后端接口数据,需要与后端接口联调才能获得数据展示,从而拖慢了开发 ...
- Warning: isMounted(...) is deprecated in plain JavaScript React classes.
1.错误提示 警告:isMounted(…)在纯 react.js 类中被弃用. 2.原因解析 出现此错误提示的原因是源代码内有已被React舍弃的代码,但此并不影响程序运行. 在index.js 内 ...
- JavaScript实现深拷贝(深复制) 面试题
1.两种方法实现深拷贝(深复制) (1)方法一:兼容性好,请仔细看代码(网上大部分代码有Bug) (2)方法二:需要对象满足JSON数据格式.JOSN数据格式:http://www.cnblogs.c ...
- 计算两个有序数组的第K大数(转)
传统解法,最直观的解法是O(m+n).直接merge两个数组,然后求第K大的数字. 如果想要时间复杂度将为O(log(m+n)).我们可以考虑从K入手.如果我们每次能够删除一个一定在第K个元素之前的元 ...
- 仿百度壁纸client(六)——完结篇之Gallery画廊实现壁纸预览已经项目细节优化
仿百度壁纸client(六)--完结篇之Gallery画廊实现壁纸预览已经项目细节优化 百度壁纸系列 仿百度壁纸client(一)--主框架搭建,自己定义Tab + ViewPager + Fragm ...
- 键盘上所有键位的ascii值
上次用的时候一直找,这次找到了,收藏起来. 0x1 鼠标左键 0x2 鼠标右键 0x3 CANCEL 键 0x4 鼠标中键 0x8 BACKSPACE 键 0x9 TAB 键 0xC CLEAR 键 ...
- oracle的sys密码重置
运行,cmd sqlplus /nolog;(也可能不要分号) connect / as sysdba alter user sys identified by 新密码; alter user sys ...