柚子快報邀請碼778899分享:Nginx 長連接keep
柚子快報邀請碼778899分享:Nginx 長連接keep
如何開啟并支持長連接
當使用nginx作為反向代理時,為了支持長連接,需要做到兩點:
從client到nginx的連接是長連接從nginx到server的連接是長連接
????????從HTTP協(xié)議的角度看,nginx在這個過程中,對于客戶端它扮演著HTTP服務器端的角色。而對于真正的服務器端(在nginx的術語中稱為upstream)nginx又扮演著HTTP客戶端的角色。
客戶端(Client)和 Nginx 保持長連接,兩個要求:
client發(fā)送的HTTP請求要求keep alivenginx設置上支持keep alive
一、客戶端 與 Nginx 長連接配置
????????默認情況下,nginx已經(jīng)自動開啟了對client連接的keep alive支持。一般場景可以直接使用,但是對于一些比較特殊的場景,還是有必要調整個別參數(shù)。
keepalive_timeout 指令
keepalive_timeout指令的語法:
Syntax: keepalive_timeout timeout [header_timeout];
Default: keepalive_timeout 75s;
Context: http, server, location
第一個參數(shù)設置keep-alive客戶端連接在服務器端保持開啟的超時值。值為0會禁用keep-alive客戶端連接。可選的第二個參數(shù)在響應的header域中設置一個值“Keep-Alive: timeout=time”。這兩個參數(shù)可以不一樣。
注:默認75s一般情況下也夠用,對于一些請求比較大的內部服務器通訊的場景,適當加大為120s或者300s。第二個參數(shù)通??梢圆挥迷O置。
keepalive_requests 指令
keepalive_requests指令用于設置一個keep-alive連接上可以服務的請求的最大數(shù)量。當最大請求數(shù)量達到時,連接被關閉。默認是100。
這個參數(shù)的真實含義,是指一個keep alive建立之后,nginx就會為這個連接設置一個計數(shù)器,記錄這個keep alive的長連接上已經(jīng)接收并處理的客戶端請求的數(shù)量。如果達到這個參數(shù)設置的最大值時,則nginx會強行關閉這個長連接,逼迫客戶端不得不重新建立新的長連接。
這個參數(shù)往往被大多數(shù)人忽略,因為大多數(shù)情況下當QPS(每秒請求數(shù))不是很高時,默認值100湊合夠用。但是,對于一些QPS比較高(比如超過10000QPS,甚至達到30000,50000甚至更高) 的場景,默認的100就顯得太低。
簡單計算一下,QPS=10000時,客戶端每秒發(fā)送10000個請求(通常建立有多個長連接),每個連接只能最多跑100次請求,意味著平均每秒鐘就會有100個長連接因此被nginx關閉。同樣意味著為了保持QPS,客戶端不得不每秒中重新新建100個連接。因此,如果用netstat命令看客戶端機器,就會發(fā)現(xiàn)有大量的TIME_WAIT的socket連接(即使此時keep alive已經(jīng)在client和nginx之間生效)。
因此對于QPS較高的場景,非常有必要加大這個參數(shù),以避免出現(xiàn)大量連接被生成再拋棄的情況,減少TIME_WAIT。
keepalive_requests 指令
默認值1000,單個連接中處理的最大請求數(shù),超過這個數(shù),連接銷毀。
keepalive_disable 指令
不對某些瀏覽器建立長連接,默認:msie6
send_timeout 指令
兩次向客戶端寫操作之間的間隔 如果大于這個時間則關閉連接 默認60s
此處有坑,注意耗時的同步操作有可能會丟棄用戶連接
該設置表示Nginx服務器與客戶端連接后,某次會話中服務器等待客戶端響應超過10s,就會自動關閉連接。
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65 65; #超過這個時間 沒有活動,會讓keepalive失效
keepalive_time 1h; # 一個tcp連接總時長,超過之后 強制失效
send_timeout 60;# 默認60s 此處有坑??! 系統(tǒng)中 若有耗時操作,超過 send_timeout 強制斷開連接。 注意:準備過程中,不是傳輸過程
keepalive_requests 1000; #一個tcp復用中 可以并發(fā)接收的請求個數(shù)
二、Nginx 與 服務端長連接配置
2.1 在 upstream 中配置
keepalive 100;
向上游服務器的保留連接數(shù)(線程池的概念)
keepalive_timeout 65
連接保留時間(單位秒)
keepalive_requests 10000
一個tcp復用中 可以并發(fā)接收的請求個數(shù)
2.2 在 server 中配置
proxy_http_version 1.1;
配置http版本號
默認使用http1.0協(xié)議,需要在request中增加”Connection: keep-alive“ header才能夠支持,而HTTP1.1默認支持。
proxy_set_header Connection "";
清楚close信息
三、壓力測試
3.1 【客戶端】 直連 【Nginx】
Server Software: nginx/1.21.6
Server Hostname: 192.168.44.102
Server Port: 80
Document Path: /
Document Length: 16 bytes
Concurrency Level: 30
Time taken for tests: 13.035 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Total transferred: 25700000 bytes
HTML transferred: 1600000 bytes
Requests per second: 7671.48 [#/sec] (mean)
Time per request: 3.911 [ms] (mean)
Time per request: 0.130 [ms] (mean, across all concurrent requests)
Transfer rate: 1925.36 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.4 0 12
Processing: 1 3 1.0 3 14
Waiting: 0 3 0.9 3 14
Total: 2 4 0.9 4 14
Percentage of the requests served within a certain time (ms)
50% 4
66% 4
75% 4
80% 4
90% 5
95% 5
98% 6
99% 7
100% 14 (longest request)
3.2 【客戶端】 連接?【Nginx】 反向代理?【Nginx】
Server Software: nginx/1.21.6
Server Hostname: 192.168.44.101
Server Port: 80
Document Path: /
Document Length: 16 bytes
Concurrency Level: 30
Time taken for tests: 25.968 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Total transferred: 25700000 bytes
HTML transferred: 1600000 bytes
Requests per second: 3850.85 [#/sec] (mean)
Time per request: 7.790 [ms] (mean)
Time per request: 0.260 [ms] (mean, across all concurrent requests)
Transfer rate: 966.47 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.2 0 13
Processing: 3 8 1.4 7 22
Waiting: 1 7 1.4 7 22
Total: 3 8 1.4 7 22
Percentage of the requests served within a certain time (ms)
50% 7
66% 8
75% 8
80% 8
90% 9
95% 10
98% 12
99% 13
100% 22 (longest request)
3.3【客戶端】 直連 【Tomcat】
Server Software:
Server Hostname: 192.168.44.105
Server Port: 8080
Document Path: /
Document Length: 7834 bytes
Concurrency Level: 30
Time taken for tests: 31.033 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Total transferred: 804300000 bytes
HTML transferred: 783400000 bytes
Requests per second: 3222.38 [#/sec] (mean)
Time per request: 9.310 [ms] (mean)
Time per request: 0.310 [ms] (mean, across all concurrent requests)
Transfer rate: 25310.16 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.3 0 15
Processing: 0 9 7.8 7 209
Waiting: 0 9 7.2 7 209
Total: 0 9 7.8 7 209
Percentage of the requests served within a certain time (ms)
50% 7
66% 9
75% 11
80% 13
90% 18
95% 22
98% 27
99% 36
100% 209 (longest request)
3.4?【客戶端】 連接?【Nginx】 反向代理?【Tomcat】并開啟keepalive
Server Software: nginx/1.21.6
Server Hostname: 192.168.44.101
Server Port: 80
Document Path: /
Document Length: 7834 bytes
Concurrency Level: 30
Time taken for tests: 23.379 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Total transferred: 806500000 bytes
HTML transferred: 783400000 bytes
Requests per second: 4277.41 [#/sec] (mean)
Time per request: 7.014 [ms] (mean)
Time per request: 0.234 [ms] (mean, across all concurrent requests)
Transfer rate: 33688.77 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.2 0 9
Processing: 1 7 4.2 6 143
Waiting: 1 7 4.2 6 143
Total: 1 7 4.2 6 143
Percentage of the requests served within a certain time (ms)
50% 6
66% 7
75% 7
80% 7
90% 8
95% 10
98% 13
99% 16
100% 143 (longest request)
3.5?【客戶端】 連接?【Nginx】 反向代理?【Tomcat】不開啟keepalive
Server Software: nginx/1.21.6
Server Hostname: 192.168.44.101
Server Port: 80
Document Path: /
Document Length: 7834 bytes
Concurrency Level: 30
Time taken for tests: 33.814 seconds
Complete requests: 100000
Failed requests: 0
Write errors: 0
Total transferred: 806500000 bytes
HTML transferred: 783400000 bytes
Requests per second: 2957.32 [#/sec] (mean)
Time per request: 10.144 [ms] (mean)
Time per request: 0.338 [ms] (mean, across all concurrent requests)
Transfer rate: 23291.74 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.2 0 9
Processing: 1 10 5.5 9 229
Waiting: 1 10 5.5 9 229
Total: 1 10 5.5 9 229
Percentage of the requests served within a certain time (ms)
50% 9
66% 10
75% 11
80% 11
90% 13
95% 14
98% 17
99% 19
100% 229 (longest request)
柚子快報邀請碼778899分享:Nginx 長連接keep
文章鏈接
本文內容根據(jù)網(wǎng)絡資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉載請注明,如有侵權,聯(lián)系刪除。