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.32-504.el6.x86_64 #1 SMP Wed Oct 15 04:27:16 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

[root@crazy-acong ~]# cat /etc/redhat-release

CentOS release6.6 (Final)

2、python 及 django 版本

[root@crazy-acong ~]# python3 --version

Python3.4.4[root@crazy-acong ~]# django-admin --version1.10.6

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 :8000 --wsgi-filetest.py

# 查看启动进程

[root@crazy-acong ~]# netstat -lnpt | grepuwsgi

tcp0 0 127.0.0.1:26685 0.0.0.0:* LISTEN 22120/uwsgi

tcp0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 22120/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= :9000#the local unix socketfilethan commnuincate to Nginx 用于和 nginx 进行数据交互的端口

socket= 127.0.0.1:8001# 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= 4#thread numbers startchedineach worker process

threads= 2#monitor uwsgi status 通过该端口可以监控 uwsgi 的负载情况

stats= 127.0.0.1:9191#clearenvironment 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:8001; # for a web port socket (we'll use this first)

}

# configuration of the server

server {

# the port your site will be served on

listen8000;

# the domain name it will serveforserver_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/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 fileyou installed

}

}

5.2 重启nginx 服务

[root@crazy-acong django_test]# nginx -t

nginx: the configurationfile /data/application/nginx-1.10.3/conf/nginx.conf syntax is ok

nginx: configurationfile /data/application/nginx-1.10.3/conf/nginx.conf test is successful

[root@crazy-acong django_test]# nginx -s reload

[root@crazy-acong django_test]# netstat -lnpt | grep 8000tcp0 0 0.0.0.0:8000 0.0.0.0:* LISTEN 43492/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

total40drwxr-xr-x 3 nginx games 4096 Mar 14 14:42app01-rw-r--r-- 1 root root 3072 Mar 14 14:51db.sqlite3-rw-r--r-- 1 root root 1026 Mar 14 15:18 django-nginx.conf

drwxr-xr-x 3 nginx games 4096 Mar 14 15:45django_test-rwxr-xr-x 1 nginx games 809 Mar 14 14:37manage.py

drwxr-xr-x 2 nginx games 4096 Mar 14 14:42static

drwxr-xr-x 3 root root 4096 Mar 14 15:47static_all # 此时会发现多了一个该目录,所有 app 的静态文件都整合到这一个目录中了

drwxr-xr-x 2 nginx games 4096 Mar 14 14:40templates-rw-r--r-- 1 root root 565 Mar 14 15:40 test-uwsgi.ini-rw-r--r-- 1 root root 664 Mar 14 15:28 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:8001; # for a web port socket (we'll use this first)

}

# configuration of the server

server {

# the port your site will be served on

listen8000;

# the domain name it will serveforserver_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/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 fileyou installed

}

}

最后重启 nginx 服务即可

[root@crazy-acong django_test]# nginx -t

nginx: the configurationfile /data/application/nginx-1.10.3/conf/nginx.conf syntax is ok

nginx: configurationfile /data/application/nginx-1.10.3/conf/nginx.conf test is successful

[root@crazy-acong django_test]# nginx -s reload

python怎么测试uwsgi并发量_nginx + uWSGI 为 django 提供高并发相关推荐

  1. 电商项目的并发量一般是多少_【高并发】高并发秒杀系统架构解密,不是所有的秒杀都是秒杀!...

    写在前面 很多小伙伴反馈说,高并发专题学了那么久,但是,在真正做项目时,仍然不知道如何下手处理高并发业务场景!甚至很多小伙伴仍然停留在只是简单的提供接口(CRUD)阶段,不知道学习的并发知识如何运用到 ...

  2. 从根上理解高性能、高并发(五):深入操作系统,理解高并发中的协程

    本文原题"程序员应如何理解高并发中的协程",转载请联系作者. 1.系列文章引言 1.1 文章目的 作为即时通讯技术的开发者来说,高性能.高并发相关的技术概念早就了然与胸,什么线程池 ...

  3. 什么是并发、并行、高并发?到底多大才算高并发?

    并发是指在一个时间段内有多个进程在执行.  并行指的是在同一时刻有多个进程在同时执行. 如果是在只有一个CPU的情况下,是无法实现并行的,因为同一时刻只能有一个进程被调度执行,如果此时同时要执行其他进 ...

  4. mysql 高并发 集群架构_一种高并发的GPU集群架构及其负载均衡方法技术

    [技术实现步骤摘要] 一种高并发的GPU集群架构及其负载均衡方法 本专利技术属于GPU集群架构及其负载均衡方法 ,特别是涉及一种高并发的GPU集群架构及其负载均衡方法. 技术介绍 GPU因其高性能的并 ...

  5. 高并发到底要怎么算才是高并发?

    什么是并发: 并发,在操作系统中,是指一个时间段中有几个程序都处于已启动运行到运行完毕之间,且这几个程序都是在同一个处理机上运行,但任一个时刻点上只有一个程序在处理机上运行. 什么是高并发: 高并发( ...

  6. 阿里、字节面试必撸,阿里大能总结 410 页 Java 并发编程手册全彩版,附录高并发面试真题及答案详解

    虽然说并发编程的第一原则是不要写并发程序.但是,随着硬件的驱动和国内互联网行业的飞速发展,对软件系统的并发量要求越来越高,传统的中间件和数据库已经成为性能的瓶颈.并发编程已经成为绕不开的话题,也慢慢成 ...

  7. java webservice 高并发_浅谈WEB中的高并发

    今天主要是对JAVA_WEB中高并发的概念及常见的处理手段做个基本介绍,后面会每个点都做详细的介绍及实现. 何谓高并发 高并发指的是:在同时或极短时间内,有大量的请求到达服务端,每个请求都需要服务端耗 ...

  8. springboot 压测 50并发 线程等待_线程池+CountDownLatch——高并发就是这么简单

    今天和大家分享的是:在开发服务端API时候,如何合理的运用线程池+CountDownLatch来保证API的高并发访问. 首先,作为Java开发的同学来说,java.util.concurrent并发 ...

  9. php高并发用什么框架,PHP有适用于高并发的WebService框架嘛?

    现在的nodejs项目很火,ruby语言也出现了像sinatra,Padrino之类的webservice框架,PHP语言有没有类似的框架啊? 回复内容: 现在的nodejs项目很火,ruby语言也出 ...

最新文章

  1. ideal如何创建dynamic web project
  2. 三个事件同步实现双相机同步WaitForMultipleObjects
  3. AndroidStudio EventBus报错解决方法its super classes have no public methods with the @Subscribe
  4. 三层交换解决了VLAN之间的通信问题
  5. Python Number(数字)
  6. 佛吉尼亚大学计算机世界排名,弗吉尼亚大学计算机世界排名
  7. Linux基础之bash脚本进阶篇-循环语句(for,while,until)
  8. 最优化作业第6章——无约束多维非线性规划方法
  9. 【直观理解】为什么梯度的负方向是局部下降最快的方向?
  10. java实验二答案天津商业大学,天津商业大学java实验报告
  11. pycharm 2018.1 专业版激活 亲测可用!!!
  12. JAVA贪吃蛇游戏1.0版本
  13. 层次分析法(AHP)—以b站up主评价问题为例
  14. 谷歌经纬度转百度地图经纬度(精确度还可以)
  15. HTML:利用canvas画定位图标
  16. gitblit+jenkins本地服务
  17. 拆解KinectFusion算法之TSDF
  18. 24小时切换简易时钟-51单片机
  19. 10个你必须知道的实时图片搜索引擎
  20. NOJ 1056 地道 普里姆算法+贪心法

热门文章

  1. vimrc.local 备份
  2. TreeView 操作应用
  3. 人生总是起起落落落落落落...
  4. java mvel_mvel java和脚本的融合 -- kkito的博客
  5. python excelwriter保存路径_太赞!Python和Excel终于可以互通了
  6. CRS磁盘force dismount引起的RAC节点宕机故障
  7. 对于数据,科技小白提出了灵魂三问:从哪儿来?到哪儿去?能干什么?
  8. 没有它你的DevOps是玩不转的,你信不?
  9. 原来AI也可以如此简单!教你从0到1开发开源知识问答机器人
  10. 【华为云技术分享】序列特征的处理方法之一:基于注意力机制方法