Nginx 長連接keep_alive的具體使用
如何開啟并支持長連接
當(dāng)使用nginx作為反向代理時,為了支持長連接,需要做到兩點:
- 從client到nginx的連接是長連接
- 從nginx到server的連接是長連接
從HTTP協(xié)議的角度看,nginx在這個過程中,對于客戶端它扮演著HTTP服務(wù)器端的角色。而對于真正的服務(wù)器端(在nginx的術(shù)語中稱為upstream)nginx又扮演著HTTP客戶端的角色。
客戶端(Client)和 Nginx 保持長連接,兩個要求:
- client發(fā)送的HTTP請求要求keep alive
- nginx設(shè)置上支持keep alive
一、客戶端 與 Nginx 長連接配置
默認情況下,nginx已經(jīng)自動開啟了對client連接的keep alive支持。一般場景可以直接使用,但是對于一些比較特殊的場景,還是有必要調(diào)整個別參數(shù)。
keepalive_timeout 指令
keepalive_timeout指令的語法:
Syntax: keepalive_timeout timeout [header_timeout]; Default: keepalive_timeout 75s; Context: http, server, location
第一個參數(shù)設(shè)置keep-alive客戶端連接在服務(wù)器端保持開啟的超時值。值為0會禁用keep-alive客戶端連接。可選的第二個參數(shù)在響應(yīng)的header域中設(shè)置一個值“Keep-Alive: timeout=time”。這兩個參數(shù)可以不一樣。
注:默認75s一般情況下也夠用,對于一些請求比較大的內(nèi)部服務(wù)器通訊的場景,適當(dāng)加大為120s或者300s。第二個參數(shù)通??梢圆挥迷O(shè)置。
keepalive_requests 指令
keepalive_requests指令用于設(shè)置一個keep-alive連接上可以服務(wù)的請求的最大數(shù)量。當(dāng)最大請求數(shù)量達到時,連接被關(guān)閉。默認是100。
這個參數(shù)的真實含義,是指一個keep alive建立之后,nginx就會為這個連接設(shè)置一個計數(shù)器,記錄這個keep alive的長連接上已經(jīng)接收并處理的客戶端請求的數(shù)量。如果達到這個參數(shù)設(shè)置的最大值時,則nginx會強行關(guān)閉這個長連接,逼迫客戶端不得不重新建立新的長連接。
這個參數(shù)往往被大多數(shù)人忽略,因為大多數(shù)情況下當(dāng)QPS(每秒請求數(shù))不是很高時,默認值100湊合夠用。但是,對于一些QPS比較高(比如超過10000QPS,甚至達到30000,50000甚至更高) 的場景,默認的100就顯得太低。
簡單計算一下,QPS=10000時,客戶端每秒發(fā)送10000個請求(通常建立有多個長連接),每個連接只能最多跑100次請求,意味著平均每秒鐘就會有100個長連接因此被nginx關(guān)閉。同樣意味著為了保持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 指令
兩次向客戶端寫操作之間的間隔 如果大于這個時間則關(guān)閉連接 默認60s
此處有坑,注意耗時的同步操作有可能會丟棄用戶連接
該設(shè)置表示Nginx服務(wù)器與客戶端連接后,某次會話中服務(wù)器等待客戶端響應(yīng)超過10s,就會自動關(guān)閉連接。
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 強制斷開連接。 注意:準(zhǔn)備過程中,不是傳輸過程 keepalive_requests 1000; #一個tcp復(fù)用中 可以并發(fā)接收的請求個數(shù)
二、Nginx 與 服務(wù)端長連接配置
2.1 在 upstream 中配置
keepalive 100;
向上游服務(wù)器的保留連接數(shù)(線程池的概念)
keepalive_timeout 65
連接保留時間(單位秒)
keepalive_requests 10000
一個tcp復(fù)用中 可以并發(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)
到此這篇關(guān)于Nginx 長連接keep_alive的具體使用的文章就介紹到這了,更多相關(guān)Nginx 長連接keep_alive內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
配置nginx訪問本地靜態(tài)資源,本地圖片,視頻教程
文章介紹了如何配置Nginx以訪問本地靜態(tài)資源、圖片和視頻,首先,進入Nginx安裝目錄并打開`nginx.conf`文件,添加一個新的`server`配置來指定本地路徑,然后,通過命令行重啟Nginx服務(wù)以應(yīng)用更改,最后,通過瀏覽器訪問配置的圖片路徑來驗證配置是否成功2025-01-01nginx日志導(dǎo)入elasticsearch的方法示例
這篇文章主要介紹了nginx日志導(dǎo)入elasticsearch的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05Nginx 平滑升級的實現(xiàn)(拒絕服務(wù)漏洞)
本文主要介紹了Nginx 平滑升級的實現(xiàn)(拒絕服務(wù)漏洞),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-02-02nginx worker進程循環(huán)的實現(xiàn)
這篇文章主要介紹了nginx worker進程循環(huán)的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-02-02詳解Nginx服務(wù)器中HTTP Headers相關(guān)的模塊配置使用
這篇文章主要介紹了詳解Nginx服務(wù)器中HTTP Headers相關(guān)的模塊配置使用,包括ngx_http_headers_module與它的增強版ngx_headers_more的配置使用講解,需要的朋友可以參考下2016-01-01PHP(FastCGI)在Nginx的alias下出現(xiàn)404錯誤的解決方法
這篇文章主要介紹了PHP(FastCGI)在Nginx的alias下出現(xiàn)404錯誤的解決方法,涉及nginx平臺的相關(guān)配置技巧,需要的朋友可以參考下2016-05-05