在作性能優化前, 咱們須要對以下進行考慮html
1.安裝壓力測試工具ab
java
[root@nginx-lua ~]# yum install httpd-tools -y
2.瞭解壓測工具使用方式node
[root@nginx-lua ~]# ab -n 200 -c 2 http://127.0.0.1/ //-n總的請求次數 //-c併發請求數 //-k是否開啓長鏈接
3.配置Nginx
靜態網站與tomcat
動態網站環境nginx
[root@nginx-lua conf.d]# cat jsp.conf server { server_name localhost; listen 80; location / { root /soft/code; try_files $uri @java_page; index index.jsp index.html; } location @java_page{ proxy_pass http://192.168.56.20:8080; } } //分別給Nginx準備靜態網站 [root@nginx-lua ~]# cat /soft/code/bgx.html <h1> Ab Load </h1> //給Tomcat準備靜態網站文件 [root@tomcat-node1-20 ROOT]# cat /soft/tomcat-8080/webapps/ROOT/bgx.html <h1> Ab Load </h1>
4.使用ab
工具進行壓力測試web
//進行壓力測試 [root@Nginx conf.d]# ab -n2000 -c2 http://127.0.0.1/bgx.html ... Server Software: nginx/1.12.2 Server Hostname: 127.0.0.1 Server Port: 80 Document Path: /bgx.html Document Length: 19 bytes Concurrency Level: 200 # 總花費總時長 Time taken for tests: 1.013 seconds # 總請求數 Complete requests: 2000 # 請求失敗數 Failed requests: 0 Write errors: 0 Total transferred: 510000 bytes HTML transferred: 38000 bytes # 每秒多少請求/s(總請求出/總共完成的時間) Requests per second: 9333.23 [#/sec] (mean) # 客戶端訪問服務端, 單個請求所需花費的時間 Time per request: 101.315 [ms] (mean) # 服務端處理請求的時間 Time per request: 0.507 [ms] (mean, across all concurrent requests) # 判斷網絡傳輸速率, 觀察網絡是否存在瓶頸 Transfer rate: 491.58 [Kbytes/sec] received
5.將nginx
下的bgx
文件移走, 再次壓測會由tomcat
進行處理數據庫
Concurrency Level: 200 Time taken for tests: 1.028 seconds Complete requests: 2000 Failed requests: 0 Write errors: 0 Total transferred: 510000 bytes HTML transferred: 38000 bytes Requests per second: 1945.09 [#/sec] (mean) Time per request: 102.823 [ms] (mean) Time per request: 0.514 [ms] (mean, across all concurrent requests) Transfer rate: 484.37 [Kbytes/sec] received
影響性能方便總體關注vim
每一個架構服務與服務之間都或多或少有一些關聯, 咱們須要將整個架構進行分層, 找到對應系統或服務的短板, 而後進行優化tomcat
vim /etc/security/limits.conf //針對root用戶 root soft nofile 65535 root hard nofile 65535 //全部用戶, 全局 * soft nofile 25535 * hard nofile 25535 //對於Nginx進程 worker_rlimit_nofile 65535; //root用戶 //soft提醒 //hard限制 //nofile文件數配置項 //65535最大大小安全
備註:能夠使用ulimit -a 命令查看open files 65535 系統最大打開文件數的值。性能優化
CPU
親和, 減小進程之間不斷頻繁遷移, 減小性能損耗
1.查看當前CPU
物理狀態
[root@nginx ~]# lscpu |grep "CPU(s)" CPU(s): 24 On-line CPU(s) list: 0-23 NUMA node0 CPU(s): 0,2,4,6,8,10,12,14,16,18,20,22 NUMA node1 CPU(s): 1,3,5,7,9,11,13,15,17,19,21,23
//2顆物理cpu,沒顆cpu12核心, 總共24核心
2.將Nginx worker
進程綁到不一樣的核心上
//啓動多少worker進程, 官方建議和cpu核心一致, 第一種綁定組合方式 #worker_processes 24; #worker_cpu_affinity 000000000001 000000000010 000000000100 000000001000 000000010000 000000100000 000001000000 000010000000 000100000000 001000000000 010000000000 10000000000; //第二種方式 #worker_processes 2; #worker_cpu_affinity 101010101010 010101010101; //最佳方式綁定方式 worker_processes auto; worker_cpu_affinity auto;
3.查看nginx worker
進程綁定至對應cpu
ps -eo pid,args,psr|grep [n]ginx
4.Nginx
通用優化配置文件
[root@nginx ~]# cat nginx.conf user nginx; worker_processes auto; worker_cpu_affinity auto; error_log /var/log/nginx/error.log warn; pid /run/nginx.pid; #調整至1w以上,負荷較高建議2-3w以上 worker_rlimit_nofile 65535; events { use epoll; #限制每一個進程能處理多少個鏈接請求,10240x16 worker_connections 10240; } http { include /etc/nginx/mime.types; default_type application/octet-stream; # 統一使用utf-8字符集 charset utf-8; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' '$status $body_bytes_sent "$http_referer" ' '"$http_user_agent" "$http_x_forwarded_for"'; access_log /var/log/nginx/access.log main; # Core module sendfile on; # 靜態資源服務器建議打開 tcp_nopush on; # 動態資源服務建議打開,須要打開keepalived tcp_nodelay on; keepalive_timeout 65; # Gzip module gzip on; gzip_disable "MSIE [1-6]\."; gzip_http_version 1.1; # Virtal Server include /etc/nginx/conf.d/*.conf; }