阿里雲Nginx與uswgi性能調優

硬件配置信息

阿里雲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)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章