Nginx反向代理(七層代理)、Nginx的TCP/UDP調度器(四層代理)、
一、Nginx反向代理(七層代理)
步驟:
??? ?部署后端web服務器集群
??? ?配置Nginx代理服務器
??? ??? ?配置upstream集群池
??? ??? ?調節集群池權重比
<img src="/home/student/Desktop/QYF筆記&nsd2411/資源包/image-20241008213058281.png" style="zoom: 67%;" />
1.部署后端web服務器集群
??? ?兩個服務器web1和web2安裝httpd服務,并創建不同網頁用以實驗
```shell
#進入web1操作:
yum -y install httpd
echo "test web1" > /var/www/html/index.html
systemctl enable httpd --now
```
```shell
#進入web2操作:
yum -y install httpd
echo "test web2" > /var/www/html/index.html
systemctl enable httpd --now
```
2.配置Nginx代理服務器:/usr/local/nginx/conf/nginx.conf
> 配置upstream集群池并調節權重比
```shell
http {
...
#使用upstream集群池定義后端服務器集群,集群名稱自定義
#使用server定義集群中的具體服務器和端口
? ? ? upstream webtestserver {
? ? ? ?? ? #設置相同客戶端訪問相同Web服務器
? ? ? ? ?ip_hash;
? ? ? ?? ? #設置權重控制weight后臺服務器訪問比例,權重越大任務的分配量就越大
? ? ? ? ?server 192.168.99.100:80 weight=2;
? ? ? ? ?#設置健康檢查
? ? ? ? ?#max_fails設置后臺服務器連接失敗的次數
? ? ? ? ?#fail_timeout設置后臺服務器連接失敗后再次連接的等待時間
? ? ? ? ?server 192.168.99.200:80 max_fails=2 fail_timeout=30;
? ? ? ? ?#設置down標記使主機暫時不參與集群活動,用于停機修復或者做項目時臨時借調
? ? ? ? ?server 192.168.99.120:80 down;
? ? ? }
? ? ? server {
? ? ? ? ? listen ? ? ? 80;
? ? ? ? ? server_name ?localhost;
??
? ? ? ? ? location / {
? ? ? ? ? ? ? root ? html;
? ? ? ? ? ? ? index ?index.html index.htm;
? ? ? ? ? ? ? #通過proxy_pass將用戶的請求轉發給服務器集群,名稱同上
? ? ? ? ? ? ? proxy_pass http://webtestserver;
? ? ? ? ? }
...
```
> 設置ip_hash后用戶訪問只會被分配給上次請求相同的服務器
二、Nginx的TCP/UDP調度器(四層代理)
使用Nginx實現TCP/UDP調度器功能,采用輪詢的方式調用后端SSH服務器
<img src="image-20241008213311333.png" style="zoom: 67%;" />
```shell
添加stream參數開啟4層代理模塊
./configure --with-stream
/usr/local/nginx/sbin/nginx -V?? ?#查看安裝模塊情況
```
修改nginx配置文件:/usr/local/nginx/conf/nginx.conf
```shell
...
stream {?? ??? ??? ??? ??? ??? ? ? #stream與http同級
?? ?upstream testssh { ? ? ? ? ? ? #創建集群,名稱自定義
?? ??? ?server 192.168.99.100:22; ?#后端SSH服務器的IP和端口
?? ??? ?server 192.168.99.200:22;
?? ?}
?? ?server { ? ? ? ? ? ? ? ?#調用集群
?? ??? ?listen 2222; ? ? ? ?#Nginx代理監聽的端口,可以自己定義
?? ??? ?proxy_pass testssh; #調用backend集群
?? ?}
}
http {
...
}
```
重載配置、測試連接到nginx代理服務器監聽的2222接口,而不是代理服務器的22端口或者后端的web服務器
```shell
ssh 192.168.99.5 -p 12345?? ?#使用該命令多次訪問查看輪詢效果
```
三、nginx的優化與查錯
1.優化客戶端使用瀏覽器訪問不存在的頁面192.168.99.5/xxxxxxx時提示404文件未找到
??? ?修改nginx配置文件:/usr/local/nginx/conf/nginx.conf
```shell
charset utf-8;?? ??? ??? ??? ??? ?#僅在需要中文時修改該選項,可以識別中文
error_page ? 404 ?/404.html; ? ?#當網站發生404報錯時,給用戶看的頁面
echo "抱歉!您訪問的頁面不存在呢?" > html/404.html
------------------------------------------------------------------
HTTP常見狀態碼列表:
200 正常
301 & 302 重定向
400 請求語法錯誤
401 訪問被拒絕
403 禁止訪問
404 資源找不到
414 請求URI頭部太長
500 服務器內部錯誤
502 代理服務器無法正常獲取下一個服務器正常的應答
```
??? ?重載,測試
2.查看nginx服務網站的狀態信息
??? ?需要使用--with-http_stub_status_module開啟狀態頁面模塊
```shell
[root@proxy nginx-1.22.1]# /usr/local/nginx/sbin/nginx ?-V
nginx version: nginx/1.22.1
built by gcc 8.5.0 20210514 (Red Hat 8.5.0-10) (GCC)?
configure arguments: --with-stream --with-http_stub_status_module
```
??? ?修改nginx配置文件:/usr/local/nginx/conf/nginx.conf
```shell
server {
? ? listen ? ? ? 80;
? ? server_name ?localhost;
? ? location /status { ??? ? ?#定義狀態頁面
? ? ? ? stub_status on;
? ? ? ? allow 192.168.99.5; ? #允許99.5訪問
? ? ? ? deny all; ? ? ? ? ? ? #其他人全部拒絕
? ? }
? ? charset utf-8;
}
```
??? ?重載,測試
```shell
[root@proxy nginx-1.22.1]# curl 192.168.99.5/status
Active connections: 1?
server accepts handled requests
?1 1 1?
Reading: 0 Writing: 1 Waiting: 0
------------------------------------------------------------------
? ? Active connections:當前活動的連接數量,有多少人訪問網站
? ? Accepts:已經接受客戶端的連接總數量,有多少人曾經來過
? ? Handled:已經處理客戶端的連接總數量
? ? Requests:客戶端發送的請求數量
? ? Reading:當前服務器正在讀取客戶端請求頭的數量
? ? Writing:當前服務器正在寫響應信息的數量,指服務器正在給客戶回應信息
? ? Waiting:當前多少客戶端在等待服務器的響應
```
3.優化Nginx并發量
??? ?并發測試,使用測試主機作為海量客戶訪問proxy主機
```shell
#-n任務量,-c是連接數
ab -n 10000 -c 10000 http://192.168.99.5/
socket: Too many open files (24)?? ?#linux系統有限制,文件打開數量不超過1024
```
??? ?優化Linux內核參數可打開最大文件數量(臨時)
```shell
ulimit -n?? ??? ??? ?#查看最大文件數量
ulimit -n 100000 ? ?#臨時設置最大文件數量
```
??? ?修改后,再次測試;?? ?
??? ?設置可打開最大文件數量目錄(永久):/etc/security/limits.conf
```shell
* ? ? ? ? ? ? ? soft ? ?nofile ? ? ? ? ? ?100000
* ? ? ? ? ? ? ? hard ? ?nofile ? ? ? ? ? ?100000
#用戶或組 ? 軟限制或硬限制 ?需要限制的項目 ? ? ?限制的值
```
??? ?重啟系統生效
??? ?服務器主機增加并發量修改Nginx配置文件目錄:/usr/local/nginx/conf/nginx.conf
```shell
worker_processes ?2; ? ? #與真機CPU核心數量一致
events {
? ? ? ?worker_connections ?50000;?? ?單個工作進程可接受訪問的數量
}
```
??? ?重載,測試
4.優化Nginx數據包頭緩存支持超長地址
??? ?默認情況下nginx無法支持長地址欄,會報414錯誤
??? ?修改Nginx配置文件,增加數據包頭部緩存大小
```shell
http {
? ? client_header_buffer_size ? ?200k; ? ?#請求包頭信息的緩存大小,默認1k
? ? large_client_header_buffers ?4 200k; ?#請求包頭部信息的緩存個數與容量
? ? include ? ? ? mime.types;
? ? default_type ?application/octet-stream;
?? ?....
}
```
??? ?重載,測試
5.瀏覽器本地緩存靜態數據
??? ?修改Nginx配置文件,定義對靜態頁面的緩存時間
```shell
server {
... ?
? ? location ~* \.(jpg|txt|png|html|php)$ {
? ? ?? ?expires 30d; ?#新添加,定義客戶端緩存時間為30天
? ? }
....
}
```
??? ?重載,測試,火狐瀏覽器查看本地緩存數據訪問about:cache