centos 7上nginx+uwsgi 性能调优

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/apple9005/article/details/76232852

centos 7上nginx+uwsgi 性能调优

需测试的服务器运行环境nginx+uwsgi+django


问题

在用ab测试get请求并发量时,只要设置的并发数超过100,request请求就会返回大量502错误

原因

经查证,uwsgi的默认并发socket连接数为100。

解决方案

第一种:
更改uwsgi配置文件中的listen

 workers = 24  # 并发处理进程数
 listen = 65535 # 并发的socket 连接数。默认为100。优化需要根据系统配置

第二种:
直接在启动uwsgi命令中加入 listen

uwsgi -M -C -s /tmp/ktv_service.sock  -p 4  --listen 10000 
--wsgi-file /website/myproject/wsgi.py --logto /website/logs/uwsgi/myproject/access_${ToDay}.log  
--enable-threads --uid nobody --gid nobody

附参考博文

上一章将nginx +uwsgi搭建起来,将keystone挂载后面。但是发现一个问题,如果http请求达到一定量后,nginx直接返回502。这让需要部署大规模openstack集群的我很是头疼,比较了一下请求的处理能力,keystone 原生的api 接口是可以承受一定量的大规模并发请求的。这就让我思考一个问题,为什么nginx作为web,性能却没有keystone api 自身的承载能力好呢。于是我做了压测,查了一下原因。

服务器配置:24 cpu+200G mem+1T disk
系统:centos 7.1
部署:kesytone nginx+uwsgi
先将keystone部署在nginx+uwsgi 后端
在同机房的另外一台服务器上发起压测:

ab -r -n 100000 -c 100 -H "User-Agent: python-keystoneclient" 
-H "Accept: application/json" -H "X-Auth-Token: 65e194" 
 http://keystonehost:35357/v2.0/

发现http处理正常。然后改一下并发数到200.发现大概50%的返回为502。然后我将keystone-api 却换成keystone 自身的服务openstack-keystone。再次用200并发压测:发现处理正常,没有返回502的。

ab -r -n 100000 -c 200 -H "User-Agent: python-keystoneclient" 
-H "Accept: application/json" -H "X-Auth-Token: 65e194" 
 http://keystonehost:35357/v2.0/

大概知道问题应该在nginx和uwsgi上,限制了并发的处理数。

查了nginx uwsgi官方的资料。原来调优有好几个参数,并且系统本身也需要做一些调优
1.首先先看一下nginx.conf里影响处理的配置。

user nginx;
worker_processes  xx;  #可以设置成cpu个数,体验较佳的性能

error_log  /var/log/nginx/error.log;
pid        /var/run/nginx.pid;


worker_rlimit_nofile 65535; # 最大打开文件数,这个值需要<= worker_connections


events {
  worker_connections 65535;  # 最大连接数,这个值依赖系统的配置。

}

2.看一下系统的配置sysctl.conf

 net.core.somaxconn = 2048  
 #定义了系统中每一个端口最大的监听队列的长度,这是个全局的参数。默认是128.优化可以根据系统配置做优化

3.uwsgi 的配置优化/etc/uwsgi.d/admin.ini

 workers = 24  # 并发处理进程数
 listen = 65535 # 并发的socket 连接数。默认为100。优化需要根据系统配置

在做优化之前,发现并发数过不了100.原因就是这里,uwsgi 的socket 默认链接为100.
做完调优,在此压测了一下性能,并发为10000时:

ab -r -n 100000 -c 10000 -H "User-Agent: python-keystoneclient" 
-H "Accept: application/json" -H "X-Auth-Token: 65e194"
  http://keystonehost:35357/v2.0/

压测报告:

Server Software: nginx/1.8.1
Server Hostname: keystonehost
Server Port: 35357
Document Path: /v2/
Document Length: 450 bytes
Concurrency Level: 15000
Time taken for tests: 30.136 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Total transferred: 72900000 bytes
HTML transferred: 45000000 bytes

Time per request: 4520.417 [ms] (mean)

Transfer rate: 2362.33 [Kbytes/sec] received

这个时候切换到keystone自身服务openstack-keystone。并发为1000时。
压测报告:

Server Software:
Server Hostname: keystonehost
Server Port: 35357
Document Path: /v2/
Document Length: 450 bytes
Concurrency Level: 10000
Time taken for tests: 100.005 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Total transferred: 70800000 bytes
HTML transferred: 45000000 bytes

Time per request: 10000.507 [ms] (mean)

Transfer rate: 691.37 [Kbytes/sec] received

可以看出,openstack-keystone服务到并发为1000时,api服务已经到极限了。

所以nginx+uswgi 经过调优后,nginx的高性能立即体现出来。
大家要是还有更好的调优方式,欢迎大家贴出来一起学习哈。
原博文地址:http://evawalle.blog.51cto.com/9555145/1750801

展开阅读全文

Nginx和uwsgi的问题

10-04

centos 5.5下, 安装nginx和uwsgi都可以用, 可以装上Django框架, 但是在运行的时候出现了import zlib error, 但是我单独运行python是可以import zlib并且可以用其的函数的.rn提示如下:rn[root@AY12091908253411d5807 hntrip]# uwsgi -s 127.0.0.1:8000 -w django_wsgirn*** Starting uWSGI 1.3 (32bit) on [Thu Oct 4 16:42:46 2012] ***rncompiled with version: 4.1.2 20080704 (Red Hat 4.1.2-52) on 01 October 2012 16:27:03rnos: Linux-2.6.18-164.el5 #1 SMP Thu Sep 3 03:33:56 EDT 2009rnnodename: AY12091908253411d5807rnmachine: i686rnclock source: unixrndetected number of CPU cores: 1rncurrent working directory: /data/htdocs/release/hntriprndetected binary path: /usr/bin/uwsgirnuWSGI running as root, you can use --uid/--gid/--chroot optionsrn*** WARNING: you are running uWSGI as root !!! (use the --uid flag) ***rn*** WARNING: you are running uWSGI without its master process manager ***rnyour processes number limit is 24576rnyour memory page size is 4096 bytesrndetected max file descriptor number: 1024rnlock engine: pthread robust mutexesrnuwsgi socket 0 bound to TCP address 127.0.0.1:8000 fd 3rnPython version: 2.7.3 (default, Oct 1 2012, 16:12:38) [GCC 4.1.2 20080704 (Red Hat 4.1.2-52)]rn*** Python threads support is disabled. You can enable it with --enable-threads ***rnPython main interpreter initialized at 0x88e88a0rnyour server socket listen backlog is limited to 100 connectionsrnmapped 63760 bytes (62 KB) for 1 coresrn*** Operational MODE: single process ***rnTraceback (most recent call last):rn File "./django_wsgi.py", line 3, in rn import django.core.handlers.wsgirn File "/usr/local/lib/python2.7/site-packages/django/core/handlers/wsgi.py", line 8, in rn from django import httprn File "/usr/local/lib/python2.7/site-packages/django/http/__init__.py", line 116, in rn from django.core import signingrn File "/usr/local/lib/python2.7/site-packages/django/core/signing.py", line 37, in rn import zlibrnImportError: No module named zlibrnunable to load app 0 (mountpoint='') (callable not found or import error)rn*** no app loaded. going in full dynamic mode ***rn*** uWSGI is running in multiple interpreter mode ***rnspawned uWSGI worker 1 (and the only) (pid: 27143, cores: 1)rn 论坛

没有更多推荐了,返回首页