nginx長(zhǎng)連接與短連接性能對(duì)比分析
?nginx長(zhǎng)連接與短連接性能對(duì)比
本次準(zhǔn)備看下目前集群的性能。
- 對(duì)應(yīng)機(jī)器如下:虛機(jī)4,前一后三,4臺(tái)配置均為4核4g
- 物理機(jī)為20核32g臺(tái)式機(jī)。
?先進(jìn)行原始nginx配置的壓測(cè)
nginx配置如下:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
#配置nginx json格式日志
log_format main '{"@timestamp": "$time_local", '
'"remote_addr": "$remote_addr", '
'"referer": "$http_referer", '
'"request": "$request", '
'"status": $status, '
'"bytes": $body_bytes_sent, '
'"agent": "$http_user_agent", '
'"x_forwarded": "$http_x_forwarded_for", '
'"up_addr": "$upstream_addr",'
'"up_host": "$upstream_http_host",'
'"up_resp_time": "$upstream_response_time",'
'"request_time": "$request_time"'
' }';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream test.miaohr.com {
server 192.168.175.128:8070;
server 192.168.175.129:8070;
server 192.168.175.130:8070;
}
server {
listen 80;
server_name test.miaohr.com;
charset utf-8;
location / {
root html;
index index.html index.htm;
proxy_pass http://test.miaohr.com;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
}
}
使用jmeter5.0進(jìn)行壓測(cè),壓測(cè)命令如下
jmeter -n -t D:/testYc.jmx -l D:/result.txt -e -o D:/webreport
其中:
- D:/testYc.jmx ------> 測(cè)試計(jì)劃文件的路徑
- D:/result.txt ------> 將要生成的測(cè)試結(jié)果文件的存放路徑
- D:/webreport -------> 將要生成的web報(bào)告的保存路徑
使用物理機(jī)cmd命令進(jìn)行壓測(cè)
線程為200,持續(xù)100s,報(bào)告如下:

此時(shí)rt為180ms+,tps在980左右
修改nginx配置為長(zhǎng)連接
長(zhǎng)連接配置如下:
user nginx;
worker_processes 1;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
#配置nginx json格式日志
log_format main '{"@timestamp": "$time_local", '
'"remote_addr": "$remote_addr", '
'"referer": "$http_referer", '
'"request": "$request", '
'"status": $status, '
'"bytes": $body_bytes_sent, '
'"agent": "$http_user_agent", '
'"x_forwarded": "$http_x_forwarded_for", '
'"up_addr": "$upstream_addr",'
'"up_host": "$upstream_http_host",'
'"up_resp_time": "$upstream_response_time",'
'"request_time": "$request_time"'
' }';
access_log /var/log/nginx/access.log main;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
#gzip on;
upstream test.miaohr.com {
server 192.168.175.128:8070;
server 192.168.175.129:8070;
server 192.168.175.130:8070;
keepalive 500;#最大空閑連接數(shù)
}
server {
listen 80;
server_name test.miaohr.com;
charset utf-8;
location / {
root html;
index index.html index.htm;
proxy_pass http://test.miaohr.com;
proxy_set_header X-Real-IP $remote_addr;
client_max_body_size 100m;
}
}
location / {
proxy_pass http://test-api;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header REMOTE-HOST $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_http_version 1.1; # 設(shè)置http版本為1.1
proxy_set_header Connection ""; # 設(shè)置Connection為長(zhǎng)連接(默認(rèn)為no)
}
}
進(jìn)行第二次壓測(cè),報(bào)告如下:

本次壓測(cè),rt降低到76ms,tps上升到2400+。對(duì)比短連接,集群的性能得到了很大的提升。但錯(cuò)誤率指標(biāo)有所上升。根據(jù)jmeter執(zhí)行日志查看, 報(bào)錯(cuò)集中為以下內(nèi)容
1650551535574,485,HTTP請(qǐng)求,Non HTTP response code: java.net.BindException,Non HTTP response message: Address already in use: connect,線程組 1-135,text,false,,2437,0,200,200,http://192.168.175.131/getSxbm,0,0,485
根據(jù)查閱后發(fā)現(xiàn),這是由于壓測(cè)機(jī)為windows系統(tǒng),tcp端口資源來(lái)不及釋放導(dǎo)致。與集群性能無(wú)關(guān)

長(zhǎng)連接與短連接的區(qū)別
主要在于長(zhǎng)連接模式下,不需要頻繁的建立起tcp連接,同時(shí)也不需要頻繁關(guān)閉,節(jié)省了這部分資源的消耗。這種情況下比較適合頻繁請(qǐng)求的場(chǎng)景下。
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Nginx日志統(tǒng)計(jì)分析的常用命令總結(jié)
這篇文章主要給大家總結(jié)了關(guān)于Nginx日志統(tǒng)計(jì)分析的一些常用命令,其中包括IP相關(guān)統(tǒng)計(jì)、頁(yè)面訪問(wèn)統(tǒng)計(jì)、性能分析、蜘蛛抓取統(tǒng)計(jì)、TCP連接統(tǒng)計(jì)等相關(guān)命令的總結(jié),相信對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-07-07
解決使用了nginx獲取IP地址都是127.0.0.1 的問(wèn)題
這篇文章主要介紹了解決使用了nginx獲取IP地址都是127.0.0.1 的問(wèn)題,獲取i工具的完整代碼文中給大家提到,具體實(shí)例代碼跟隨小編一起看看吧2021-09-09
Nginx設(shè)置https和http同時(shí)使用同一個(gè)端口訪問(wèn)
本文主要介紹了Nginx通過(guò)8070端口同時(shí)支持HTTP和HTTPS的配置方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-05-05
nginx 訪問(wèn)限制與訪問(wèn)控制的實(shí)現(xiàn)
訪問(wèn)控制要做的事情是控制客戶端的資源訪問(wèn)權(quán)限,本文主要介紹了nginx 訪問(wèn)限制與訪問(wèn)控制的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-02-02

