文章目錄
- 一、web服務
- 1.1 nginx安裝
- 1.2 配置文件
- 1.3 Nginx處理Web機制
- 二、反向代理
- 三、負載均衡
- 3.1 分類
- 3.2 負載相關配置文件
- 3.3 keepalive 提高吞吐量
- 3.4 配置瀏覽器緩存
- 附、JMeter性能測試工具
以賽促學內容,大概率感覺會使用nginx做web服務,特對nginx做總結歸納.
Nginx是lgor Sysoev為俄羅斯訪問量第二的rambler.ru站點設計開發的。從2004年發布至今,憑借開源的力量,已經接近成熟與完善。Nginx功能豐富,可作為HTTP服務器,也可作為反向代理服務器,郵件服務器。
官網:http://nginx.org/ https://github.com/nginx/nginx.org
一、web服務
高可用 keepalived
1.1 nginx安裝
# 查詢
nginx -v #安裝版本
dnf search nginx
#安裝
dnf install nginx -y
# 查找配置文件
rpm -qa |grep nginx
rpm -qa |grep nginx
rpm -qc nginx-1.20.1-10.el9.x86_64
vim /etc/nginx/nginx.conf
#驗證配置文件,也顯示位置
nginx -t
......
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
......
#啟動
systemctl enable --now nginx.service
systemctl status nginx.service
ss -ntlp |grep 80
安裝驗證
1.2 配置文件
#備份配置文件
mv /etc/nginx/nginx.conf /etc/nginx/nginx.conf.bak
#重新配置文件
touch nginx.conf
vim nginx.conf
.........
# events 塊用于配置連接處理的設置。它通常位于 http 塊之外,
#但也可以放在 server 或 location 塊內。不寫會報錯.
events {
}http {server {listen 80; #端口server_name localhost; #IP 域名root /usr/share/nginx/lih;# 根目錄 不寫則默認/usr/share/nginx/html;index haha.html;# 首頁 不寫則默認index}
}
.........
nginx -s reload # 重新加載配置文件,-s 發送命令.Nginx 會嘗試平滑地重啟工作進程.
nginx -t #用于檢查 Nginx 配置文件的語法是否正確
配置中http-server中的location有更復雜的配置。可有正在表達式~
#無參數是匹配root/app中的index文件location /app {root /usr/share/nginx/lih;}
#~正則表達式,制訪問6-9的文件,可將root路徑提出。root /var/www/localhost;location ~ /files/file[6-9].file{ }
# 307 重新定向 訪問temp自動到/app/haha.htmlroot /var/www/localhost; location /temp{return 307 /app/haha.html;}
# 精確匹配location =/temp{root /var/www/localhost;}
# 增加不同的服務端口89server {listen 89;server_name localhost;#默認#root /usr/share/nginx/html下的index.htmllocation /app {root /usr/share/nginx/lih2;# index默認是app里的index.html}}# 增加不同的服務端口90server {listen 90;server_name localhost;location /views {root /opt/RuoYi-Vue/ruoyi-ui/src;#程序文件index index.vue;# vue的文件架構還需研究下}#和上面效果一樣 使用alias別名,隱去路徑。location /static {alias /opt/RuoYi-Vue/ruoyi-ui/src/views;index index.vue;}}
1.3 Nginx處理Web機制
異步,多路復用。
配置文件中的events 塊用于配置連接處理的設置。案例如下:
events {worker_connections 1024; # 默認每個工作進程允許的最大并發連接數multi_accept on; # 允許多個連接同時被接受use epoll; # 默認使用 epoll 事件模型
}
# epoll是Linux下的一種I/O復用技術,主要用于提高高并發服務器程序的性能。如圖相對于傳統事件處理,減少了進程。
#ss:專門用于顯示套接字統計信息,包括TCP和UDP套接字的狀態、端口號、連接狀態等。
#優勢在于可以更快地顯示大量套接字連接的信息。
ss -ntlp |grep 80
#ps:提供進程的靜態信息,包括進程ID (PID)、CPU和內存使用情況、狀態、啟動時間等。
#支持多種輸出格式,如較詳細的長格式(-l選項)或完整格式(-f選項)。
ps -ef |grep nginx
二、反向代理
server {listen 80;server_name localhost;location / {proxy_pass http://tomcats;# 需和upstream的名字一樣}
}
# 配置上游服務器
upstream tomcats { server localhost:89;
}
三、負載均衡
3.1 分類
負載均衡是一種優化手段,用于在多個服務器之間均勻地分配工作負載,從而提高系統整體的性能和可靠性
載體維度分類硬件負載均衡:這種方案通常使用專用的硬件設備,如F5和A10,具有高性能和全面的功能,但成本較高且擴展性有限。
軟件負載均衡:通過在標準服務器上運行的軟件實現,例如Nginx、HAProxy和LVS。這些軟件負載均衡器具有部署簡單、成本低、靈活性高等優點。
網絡通信分類 四層負載均衡:基于傳輸層的IP地址和端口進行請求轉發,性能較好,通常用于處理大量網絡流量。
七層負載均衡:基于應用層信息(如URL、HTTP頭部等)進行決策,可以提供更細粒度的控制,常用于需要更智能路由的場景
類比
3.2 負載相關配置文件
http {# 反向代理服務server {listen 80;server_name localhost;location / {proxy_pass http://tomcats;# 需和upstream的名字一樣}}# 配置上游服務器upstream tomcats { server localhost:89;server localhost:90;# ip_hash;#hash #hash算法分配,即每個ip機器對應固定# least_conn; #最少連接#hash $request_uri;#hash根據url算法#server localhost:89 weight =1;#默認為1#server localhost:90 weight= 5;}# 89端口服務server {listen 89;server_name localhost;root /usr/share/nginx/lih2;#更換根路徑#index index.html;}#90端口服務server {listen 90;server_name localhost;#index index.html; 不寫使用默認根路徑和默認主文件}
}
分別顯示89,90輪詢服務,默認為weight=1平均輪詢。
#nginx 做負載均衡的案例server {listen 80; #端口server_name localhost; #IP 域名location / {proxy_pass http://proxy;}}# 兩個服務端口upstream proxy{server localhost:802;server localhost:803;}# 不要再加httpserver服務端口重復了# server {# listen 802;# server_name localhost;# }#server {# listen 803;# server_name localhost;#}
# 配置上游服務器
upstream tomcats {server localhost:89 weight =1;#默認為1server localhost:90 down;server localhost:91 backup;server localhost:92 fail_timeout=10#默認10s
}
3.3 keepalive 提高吞吐量
upstream tomcats {server localhost:90 ;keepalive 32;#32個線程,不用反復消失創建
}server {listen 80;server_name localhost;location / {proxy_pass http://tomcats;proxy_http_version 1.1;# keepalive相關,具體各位自行研究proxy_set_header Connection "";# keepalive相關,具體各位自行研究}
}
JMeter測試吞吐量為2倍
3.4 配置瀏覽器緩存
# 各自有空去研究吧
proxy_cache_path /...
附、JMeter性能測試工具
Meter是一種可以在不同協議或技術上執行負載測試,面向性能的業務(功能)測試,回歸測試等的軟件
官網:https://jmeter.apache.org/
教程文檔參考:
https://blog.csdn.net/yaorongke/article/details/82799609
https://iowiki.com/jmeter/jmeter_quick_guide.html