硬件配置信息
阿里云ECS:
CPU: 1核
内存: 2 GiB
实例类型: I/O优化
操作系统: CentOS 7.6 64位
网络带宽:1Mbps
调优前uwsgi设置
uwsgi1:
[uwsgi]
#使用nginx连接时使用
socket=127.0.0.1:8081
#项目目录
chdir=/home/Winston_crm
#项目中wsgi.py文件的目录,相对于项目目录
wsgi-file=Winston_crm/wsgi.py
#指定启动的工作进程数
processes=4
#指定工作进程中的线程数
threads=2
master=True
#保存启动之后主进程的pid
pidfile=uwsgi1.pid
#设置uwsgi后台运行,uwsgi.log保存日志信息
daemonize=uwsgi1.log
#设置虚拟环境的路径
virtualenv=/home/python/virtualenvs/django_py
uwsgi2:
[uwsgi]
#使用nginx连接时使用
socket=127.0.0.1:8082
#项目目录
chdir=/home/Winston_crm
#项目中wsgi.py文件的目录,相对于项目目录
wsgi-file=Winston_crm/wsgi.py
#指定启动的工作进程数
processes=4
#指定工作进程中的线程数
threads=2
master=True
#保存启动之后主进程的pid
pidfile=uwsgi2.pid
#设置uwsgi后台运行,uwsgi.log保存日志信息
daemonize=uwsgi2.log
#设置虚拟环境的路径
virtualenv=/home/python/virtualenvs/django_py
调优前nginx设置
增加了这些内容,其它都没变动
upstream Winston_crm {
server 127.0.0.1:8081;
server 127.0.0.1:8082;
}
server {
listen 80;
server_name xx.xxx.xxx.xxx;
location / {
# 包含uwsgi请求的参数
include uwsgi_params;
# 转交请求给uwsgi
uwsgi_pass Winston_crm;
}
location /static {
# 指定静态文件存放的目录
alias /var/www/crm/static/;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
调优前ab压测惨不忍睹
需在客户端安装web 压力测试工具 ApacheBench ,简称 ab。ab 是一个命令行工具,即通过 ab 命令行,模拟多个请求同时对某一 URL 地址进行访问,因此可以用来测试目标服务器的负载压力。
yum -y install httpd-tools
测试结果:
root@withong ~]# ab -c 10 -n 100 -k http://xx.xxx.xx.xxx:80/login/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking xx.xxx.xx.xxx (be patient).....done
Server Software: nginx/1.16.1
Server Hostname: xx.xxx.xx.xxx
Server Port: 80
Document Path: /login/
Document Length: 47200 bytes
Concurrency Level: 10
Time taken for tests: 35.921 seconds
Complete requests: 100
Failed requests: 0
Write errors: 0
Keep-Alive requests: 100
Total transferred: 4757500 bytes
HTML transferred: 4720000 bytes
Requests per second: 2.78 [#/sec] (mean)
Time per request: 3592.080 [ms] (mean)
Time per request: 359.208 [ms] (mean, across all concurrent requests)
Transfer rate: 129.34 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 3 8.9 0 38
Processing: 136 3522 1763.9 3353 8783
Waiting: 37 268 331.0 162 2514
Total: 163 3525 1763.6 3353 8783
Percentage of the requests served within a certain time (ms)
50% 3353
66% 4095
75% 4409
80% 4725
90% 6393
95% 7168
98% 8034
99% 8783
100% 8783 (longest request)
系统调优
socket最大连接数修改
系统的默认值为128 这里改为50000
echo 50000 > /proc/sys/net/core/somaxconn
加快系统的tcp回收(NAT连接下不建议修改,会出现大量连接错误的现象)
echo 1 > /proc/sys/net/ipv4/tcp_tw_recycle
允许空的tcp回收(同样不建议修改)
echo 1 > /proc/sys/net/ipv4/tcp_tw_reuse
关闭系统的洪水抵御防护
正常情况下,当系统收到大量的重复请求时,系统会认为攻击,会在响应头中加入cookie信息,但我们这是高并发请求并不是攻击
系统默认为1 修改为0
echo 0 > /proc/sys/net/ipv4/tcp_syncookies
swap空间使用
阿里云镜像默认是没有开swap分区的,偶尔峰值的时候就会出现out of memory的错误,增加swap分区能有效的避免这个情况,并设置swap分区的使用前提
sysctl vm.swappiness=10 #物理内存还剩10%的时候开始使用虚拟内存
开辟虚拟空间:
fallocate -l 4G /swapfile
操作系统允许打开文件的最大值设为65535
ulimit -n 65535
其他优化选项,生产环境不要盲目使用:
https://yq.aliyun.com/articles/718976
Nginx的优化
修改nginx子进程运行打开的连接数
worker_processes 1;
worker_rlimit_nofile 65535;
events {
worker_connections 10000;
use epoll;
}
启用gzip压缩
gzip on;
gzip_buffers 32 4K;
gzip_comp_level 6;
gzip_min_length 100;
gzip_types application/javascript text/css text/xml;
gzip_vary on;
uwsgi的优化
# 监听数量
listen = 10000
#指定启动的工作进程数
processes=2
#指定工作进程中的线程数
threads=4
调优后ab压测
(django_py) [root@iZbp18jvb8bcz46dte504uZ nginx]# ab -c 1000 -n 10000 -k -r http://xx.x.xx.xx:80/login/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/
Benchmarking xxx.x.x.xx(be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests
Server Software: nginx/1.16.1
Server Hostname: xxx.x.x.x
Server Port: 80
Document Path: /login/
Document Length: 47200 bytes
Concurrency Level: 1000
Time taken for tests: 73.117 seconds
Complete requests: 10000
Failed requests: 362
(Connect: 0, Receive: 0, Length: 362, Exceptions: 0)
Write errors: 0
Keep-Alive requests: 9638
Total transferred: 458749524 bytes
HTML transferred: 454913600 bytes
Requests per second: 136.77 [#/sec] (mean)
Time per request: 7311.731 [ms] (mean)
Time per request: 7.312 [ms] (mean, across all concurrent requests)
Transfer rate: 6127.11 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 134 529.8 0 3013
Processing: 365 6845 2098.6 7200 12752
Waiting: 10 7037 1912.6 7242 12752
Total: 384 6979 2048.1 7274 12752
Percentage of the requests served within a certain time (ms)
50% 7274
66% 7926
75% 8330
80% 8528
90% 9276
95% 9633
98% 9763
99% 9814
100% 12752 (longest request)