性能優化概述
在做性能優化前, 我們需要對如下進?考慮
1.當前系統結構瓶頸
觀察指標
壓?測驗 ab(httpd-tools) webbanch
2.了解業務模式
接?業務型別
系統層次化結構
3.性能與安全
性能好安全弱
安全好性能低
壓?測驗?具
1.安裝壓?測驗?具 ab
yum install httpd-tools -y
2.了解壓測?具使??式
ab -n 200 -c 2 http://127.0.0.1/
-n 總的請求次數
-c 并發請求數
-k 是否開啟?連接
3. 配置 Nginx 靜態?站與 tomcat 動態?站環境
給Nginx準備靜態?站
mkdir -p /soft/code
echo "<h1> Ab Load </h1>" > /soft/code/bgx.html
給Tomcat準備靜態?站?件
yum -y install tomcat
mkdir -p /usr/share/tomcat/webapps/ROOT/
echo "<h1> Ab Load </h1>" > /usr/share/tomcat/webapps/ROOT/bgx.html
vi /usr/local/nginx/conf/nginx.conf
root /soft/code;
try_files $uri $uri/ @java_page;
index index.jsp index.html;
}
location @java_page{
proxy_pass http://192.168.1.1:8080;
}
}

4.使? ab ?具進?壓?測驗
##進?壓?測驗
ab -n2000 -c2 http://192.168.1.1/bgx.html
ab -n2000 -c2 http://192.168.1.1:8080/bgx.html
Server Software: nginx/1.18.0
Server Hostname: 192.168.1.1
Server Port: 80
Document Path: /bgx.html
Document Length: 19 bytes
Concurrency Level: 2
Time taken for tests: 0.104 seconds ##總花費總時見
Complete requests: 2000 ##總請求數
Failed requests: 0 ##請求失敗數
Write errors: 0
Total transferred: 500000 bytes
HTML transferred: 38000 bytes
Requests per second: 19140.59 [#/sec] (mean) ##每秒多少請求/s(總請求出/總共完成的時間)
Time per request: 0.104 [ms] (mean) ##客戶端訪問服務端, 單個請求所需花費的時間
Time per request: 0.052 [ms] (mean, across all concurrent requests) ##服務端處理請求的時間
Transfer rate: 4673.00 [Kbytes/sec] received ##判斷?絡傳輸速率, 觀察?絡是否存在瓶頸
5.將 nginx 下的 bgx ?件移?, 再次壓測會由 tomcat 進?處理
影響性能指標
影響性能?便整體關注
1.?絡
?絡的流量
?絡是否丟包
這些會影響http的請求與調?
2.系統
硬體有沒有磁盤損壞,磁盤速率
系統負載、記憶體、系統穩定性
3.服務
連接優化、請求優化
根據業務形態做對應的服務設定
4.程式
接?性能
處理速度
程式執?效率
5.資料庫
每個架構服務與服務之間都或多或少有?些關聯, 我們需要將整個架構進?分層, 找到對應系統或服務的短板, 然后進?優化
系統性能優化
?件句柄, Linux?切皆?件,?件句柄可以理解為就是?個索引
?件句柄會隨著我們行程的調?頻繁增加
系統默認對?件句柄有限制,不能讓?個行程?限的調?
需要限制每個行程和每個服務使?多?的?件句柄
?件句柄是必須要調整的優化引數
設定?式
系統全域性修改
?戶區域性修改
Error:too many open files
vim /etc/security/limits.conf
//針對root?戶
root soft nofile 65535
root hard nofile 65535
//所有?戶, 全域
* soft nofile 25535
* hard nofile 25535
//對于Nginx行程
worker_rlimit_nofile 45535;
//root?戶
//soft提醒
//hard限制
//nofile?件數配置項
//65535
Nginx性能優化
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 0000000100
00 000000100000 000001000000 000010000000 000100000000 001000000000 010000000000 10
000000000;
//第?種?式
#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 35535;
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;
}
Nginx常見問題
1.Nginx 多個相同 Server_name 優先級
2.location 匹配優先級
3.try_files使?
4.Ngnx的alias和root區別
Server優先級
Nginx 多個相同 Server_name 優先級
1.環境準備
mkdir /soft/code{1..3} -p
for i in {1..3};do echo "<h1>Code $i</h1>" > /soft/code"$i"/index.html;done
2.修改組態檔
vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name testserver1 192.168.1.1;
location / {
root /soft/code1;
index index.html;
}
}
server {
listen 80;
server_name testserver2 192.168.1.1;
location / {
root /soft/code2;
index index.html;
}
}
server {
listen 80;
server_name testserver3 192.168.1.1;
location / {
root /soft/code3;
index index.html;
}
}

nginx -t ##檢查語法
systemctl restart nginx ##重啟nginx服務
3.測驗訪問效果
[root@localhost ~]# curl 192.168.1.1
<h1>Code 1</h1>
當servername一樣的時候,訪問的優先級從上到下
location優先級
?個 server 出現多個 location

= 正則匹配 ^~ ~ ~* 默認匹配
1.實體準備
vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name 192.168.1.1;
root /soft;
index index.html;
location = /code1/ {
rewrite ^(.*)$ /code1/index.html break;
}
location ~ /code* {
rewrite ^(.*)$ /code3/index.html break;
}
location ^~ /code {
rewrite ^(.*)$ /code2/index.html break;
}
}

2.測驗效果
[root@localhost ~]# curl http://192.168.1.1/code1
<h1>Code 2</h1>
注釋掉^~, 重啟Nginx

systemctl restart nginx
測驗效果
[root@localhost ~]# curl http://192.168.1.1/code1
<h1>Code 3</h1>
注釋掉精確匹配=, 重啟Nginx

systemctl restart nginx
測驗效果
[root@localhost ~]# curl http://192.168.1.1/code1
<h1>Code 2</h1>
Break 和 last 的區別
Break:頁面找不到,停止匹配
Last: 頁面找不到,繼續向下匹x配
try_files的使?
nginx 的 try_files 按順序檢查?件是否存在
location /{
try_files $uri $uri/ /index.php;
}
http://192.168.1.1/zps
$uri 判斷zps目錄是否存在
$uri/ 如果zps目錄存在,那么就將zps目錄下的index.html檔案決議之后回傳給客戶端
/index.php 如果index.html不存在,那么就看zps目錄下是否存在index.php頁面
#1.檢查?戶請求的uri內容是否存在本地,存在則決議
#2.將請求加/, 類似于重定向處理
#3.最后交給index.php處理
1.演示環境準備
echo "Try-Page" > /usr/local/nginx/html/index.html
echo "Tomcat-Page" > /soft/app/apache-tomcat-9.0.7/webapps/ROOT/index.html
sh /soft/app/apache-tomcat-9.0.7/bin/startup.sh
netstat -lntp|grep 8080
2.配置 Nginx 的 tryfiles
vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name 192.168.1.1;
root /soft/code;
index index.html;
location / {
try_files $uri @java_page;
}
location @java_page {
proxy_pass http://127.0.0.1:8080;
}
}
nginx -s reload ##重啟Nginx
3.測驗 tryfiles
[root@Nginx ~]# curl http://192.168.1.1/index.html
Try-Page
4.將/soft/code/index.html?件移?
mv /soft/code/{index.html,index.html_bak}
5.發現由Tomcat吐回了請求
[root@Nginx ~]# curl http://192.168.1.1/index.html
Tomcat-Page
alias與root區別
root 路徑配置
//Nginx的root配置
[root@Nginx ~]# vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
index index.html;
location /request_path/code/ {
root /local_path/code/;
}
}
//請求測驗
[root@Nginx conf.d]# curl http://192.168.1.1/request_path/code/index.html
Root
//實際請求本地?件路徑為
/local_path/code/'request_path/code'/index.html
alias 路徑配置
[root@Nginx ~]# mkdir /local_path/code/request_path/code/ -p
[root@Nginx ~]# echo "Alias" > /local_path/code/index.html
//配置?件
[root@Nginx ~]# vi /usr/local/nginx/conf/nginx.conf
server {
listen 80;
index index.html;
location /request_path/code/ {
alias /local_path/code/;
}
}
//測驗訪問
[root@Nginx ~]# curl http://192.168.1.1/request_path/code/index.html
Alias
//實際訪問本地路徑
/local_path/code/'index.html'
Root和alias都是用來指定網頁存放路徑的,只不過root可以用相對路徑,alias必須是絕對路徑
獲取?戶真實IP
Nginx 傳遞?戶的真實IP地址
$remote_addr 只能獲取到最近?臺服務器訪問IP
x_forwarded_for 頭部資訊容易被篡改
獲取客戶端真實的IP地址?
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
常見HTTP狀態碼
200 正常請求
301 永久跳轉
302 臨時跳轉
400 請求引數錯誤
401 賬戶密碼錯誤(authorization required)
403 權限被拒絕(forbidden)
404 ?件沒找到(Not Found)
413 ?戶上傳?件??限制(Request Entity Too Large)
502 后端服務?回應(bad gateway)
504 后端服務執?超時
?站相關術語
如果?棟?廈?所有?作?員通過1個IP公?接?上?, 總共100個設備, 當所有?同時請求?個? 站, 并且重繪了5次, 那么請求pv、ip、uv
pv:頁?瀏覽量 500
uv:唯?設備100
ip:唯?出? 1
?站訪問原理
1.DNS流程
1.查詢本地Hosts
2.請求本地localDNS
3.回傳對應的IP
2.HTTP連接
1.建?TCP三次握?,發送請求內容, 請求頭、請求的?、請求的主體
2.將請求傳遞給負載均衡, 負載均衡做對應的調度
3.如果請求的是靜態頁?, 那么調度?對應的靜態集群組即可
4.如果請求的是動態頁?, 將請求調度?動態集群組
1.如果僅僅是請求頁?, 可能會經過Opcache
2.如果請求頁?需要查詢資料庫, 或者是往資料庫插?內容
3.檢查對應的操作是查詢還是寫?, 如果是查詢資料庫
4.檢查查詢的內容是否有被快取, 如有快取則回傳
5.檢查查詢陳述句, 將查詢結果回傳
6.記憶體快取Redis快取對應的查詢結果
7.回傳對應客戶端請求的內容?于WEB節點
8.WEB節點收到請求后回傳內容?負載均衡
9.負載均衡回傳客戶端內容, TCP四次斷開
3.HTTP斷開連接
1.按照分層結構
CDN層->負載層->WEB層->存盤層->快取層->資料庫層
同時需要注意, 每?層都有對應的快取機制
Nginx優化?案
Nginx優化
1.gzip壓縮
2.expires靜態?件快取
3.調整?絡IO模型,調整Nginx worker行程的最?連接數
5.隱藏Nginx名稱和版本號
6.配置防盜鏈,防?資源被盜?
7.禁?通過IP地址訪問,禁?惡意域名決議,只允許域名訪問
8.防DDOS、cc攻擊, 限制單IP并發請求連接
9.配置錯誤頁?,根據錯誤代碼指定?頁反饋?戶
10.限制上傳資源?錄被程式訪問,防??馬?侵系統
11.Nginx加密傳輸優化
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/286373.html
標籤:其他
上一篇:SpringBoot基礎入門

