nginx优化实践与验证
nginx优化实践
实践场景1: 单台nginx 2核4G
实践场景2: 三台nginx 2核4G
压测工具:WRK
初始安装的nginx压测:
yum install -y nginx
安装WRK压测工具
git clone https://gitee.com/mirrors/wrk.git
cd wrk
make
cp -a wrk /usr/local/bin/
wrk参数:
使用方法: wrk <选项> <被测HTTP服务的URL>
Options:
-c, --connections <N> 跟服务器建立并保持的TCP连接数量
-d, --duration <T> 压测时间
-t, --threads <N> 使用多少个线程进行压测
-s, --script <S> 指定Lua脚本路径
-H, --header <H> 为每一个HTTP请求添加HTTP头
--latency 在压测结束后,打印延迟统计信息
--timeout <T> 超时时间
-v, --version 打印正在使用的wrk的详细版本信息
<N>代表数字参数,支持国际单位 (1k, 1M, 1G)
<T>代表时间参数,支持时间单位 (2s, 2m, 2h)
云服务器配置
3台Linux服务器-绑定了一个公网IP39.98.77.148用于配置服务器
1台负载均衡slb


3台服务器安装nginx
yum isntall -y nginx
标识不同nginx
echo "nginx-171" >/usr/share/nginx/html/index.html
echo "nginx-171" >/usr/share/nginx/html/index.html
echo "nginx-171" >/usr/share/nginx/html/index.html
#nginx配置[每台服务器都使用最基础的配置]:
egrep -v "^$|#" /etc/nginx/nginx.conf.default >/etc/nginx/nginx.conf
nginx -s reload
#配置概览:
worker_processes 1;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
检查轮询算法:
[root@nginx-172 ~]# curl 192.168.0.171
nginx-171
[root@nginx-172 ~]# curl 192.168.0.172
nginx-172
[root@nginx-172 ~]# curl 192.168.0.173
nginx-173
wrk压测 初始[root@master wrk]# wrk -t2 -c800 -d120s http://39.99.217.208
初始配置压测:
[root@master wrk]# wrk -t2 -c800 -d120s http://39.99.217.208
Running 2m test @ http://39.99.217.208
2 threads and 800 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 246.22ms 289.79ms 2.00s 85.67%
Req/Sec 1.12k 427.18 3.51k 72.62%
265348 requests in 2.00m, 1.34GB read
Socket errors: connect 0, read 20, write 7873, timeout 3620
Requests/sec: 2210.37
Transfer/sec: 11.41MB
初始数据:
延迟 246.22ms
优化1: work进程连接数优化:
worker_processes 1;
events {
worker_connections 100000; <----连接数配置,3台都配置成这个,重启后测试
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
结果:
[root@master wrk]# wrk -t2 -c800 -d120s http://39.99.217.208
Running 2m test @ http://39.99.217.208
2 threads and 800 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 238.58ms 279.94ms 2.00s 84.64%
Req/Sec 1.06k 420.61 2.89k 70.18%
250585 requests in 2.00m, 1.26GB read
Socket errors: connect 0, read 12, write 10299, timeout 2497
Requests/sec: 2086.60
Transfer/sec: 10.77MB
--------------------------------------
结果:
延迟降低 289.79ms --> 238.58ms
丢包率减少 3620 --> 2497
优化2: work进程数量优化:
worker_processes auto; <--- 这里直接改为了auto,而不是1
events {
worker_connections 100000;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
结果:
[root@master wrk]# wrk -t2 -c800 -d120s http://39.99.217.208
Running 2m test @ http://39.99.217.208
2 threads and 800 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 263.22ms 298.89ms 2.00s 86.82%
Req/Sec 1.10k 382.66 2.81k 71.77%
259948 requests in 2.00m, 1.31GB read
Socket errors: connect 0, read 10, write 6921, timeout 3454
Requests/sec: 2165.50
Transfer/sec: 11.18MB
结果:
每秒处理请求数 2086.60 --> 2165.50
优化3: cpu亲和力和优先级:
worker_processes 2;
worker_cpu_affinity 0101 1010; <--- 亲和力
worker_priority -20; <---优先级
events {
worker_connections 100000;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
结果:
[root@master wrk]# wrk -t2 -c800 -d120s http://39.99.217.208
Running 2m test @ http://39.99.217.208
2 threads and 800 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 222.80ms 267.85ms 2.00s 84.34%
Req/Sec 805.34 333.47 2.61k 73.99%
190706 requests in 2.00m, 0.96GB read
Socket errors: connect 0, read 15, write 16445, timeout 1272 # timeout明显减少
Requests/sec: 1588.88
Transfer/sec: 8.20MB
优化4: gzip优化:
worker_processes 2;
worker_cpu_affinity 0101 1010; <--- 亲和力
worker_priority -20; <---优先级
events {
worker_connections 100000;
}
http {
include mime.types;
default_type application/octet-stream;
sendfile on;
keepalive_timeout 65;
gzip on;
gzip_min_length 500;
gzip_buffers 4 256;
gzip_http_version 1.1;
gzip_comp_level 2;
gzip_types text/plain application/javascript application/x-javascript text/javascript text/css application/xml application/xml+rss;
gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_disable "MSIE [1-6]\.";
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
}
[root@master wrk]# wrk -t2 -c800 -d120s http://39.99.217.208
Running 2m test @ http://39.99.217.208
2 threads and 800 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 232.83ms 277.62ms 2.00s 84.19%
Req/Sec 0.95k 346.60 2.94k 75.79%
225026 requests in 2.00m, 1.14GB read
Socket errors: connect 0, read 13, write 12248, timeout 2490
Requests/sec: 1873.72
Transfer/sec: 9.71MB
优化5 CPU开销优化
events {
worker_connections 100000;
multi_accept on;
accept_mutex on;
accept_mutex_delay 1ms;
}
[root@master wrk]# wrk -t2 -c800 -d120s http://39.99.217.208
Running 2m test @ http://39.99.217.208
2 threads and 800 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 213.94ms 260.76ms 2.00s 84.76%
Req/Sec 0.87k 371.67 3.34k 77.82%
206969 requests in 2.00m, 1.05GB read
Socket errors: connect 0, read 13, write 15329, timeout 1713
Requests/sec: 1724.03
Transfer/sec: 8.93MB
优化6 日志优化
access_log /var/log/nginx/access.log aaa buffer=1m;
[root@master wrk]# wrk -t2 -c800 -d120s http://39.99.217.208
Running 2m test @ http://39.99.217.208
2 threads and 800 connections
Thread Stats Avg Stdev Max +/- Stdev
Latency 246.08ms 278.00ms 2.00s 84.53%
Req/Sec 804.20 420.24 3.23k 75.12%
189759 requests in 2.00m, 0.96GB read
Socket errors: connect 0, read 30, write 14936, timeout 1451
Requests/sec: 1580.08
Transfer/sec: 8.19MB
nginx优化实践与验证的更多相关文章
- 基于 Nginx 的 HTTPS 性能优化实践
前言 分享一个卓见云的较多客户遇到HTTPS优化案例. 随着相关浏览器对HTTP协议的“不安全”.红色页面警告等严格措施的出台,以及向 iOS 应用的 ATS 要求和微信.支付宝小程序强制 HTTPS ...
- 【实战分享】又拍云 OpenResty / Nginx 服务优化实践
2018 年 11 月 17 日,由 OpenResty 主办的 OpenResty Con 2018 在杭州举行.本次 OpenResty Con 的主题涉及 OpenResty 的新开源特性.业界 ...
- 直播推流端弱网优化策略 | 直播 SDK 性能优化实践
弱网优化的场景 网络直播行业经过一年多的快速发展,衍生出了各种各样的玩法.最早的网络直播是主播坐在 PC 前,安装好专业的直播设备(如摄像头和麦克风),然后才能开始直播.后来随着手机性能的提升和直播技 ...
- 2.Nginx优化
[教程主题]:Nginx优化 [课程录制]: 创E [主要内容] Nginx 优化 nginx介绍 Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为"engine ...
- 美图App的移动端DNS优化实践:HTTPS请求耗时减小近半
本文引用了颜向群发表于高可用架构公众号上的文章<聊聊HTTPS环境DNS优化:美图App请求耗时节约近半案例>的部分内容,感谢原作者. 1.引言 移动互联网时代,APP 厂商之间的竞争非常 ...
- 长连接锁服务优化实践 C10K问题 nodejs的内部构造 limits.conf文件修改 sysctl.conf文件修改
小结: 1. 当文件句柄数目超过 10 之后,epoll 性能将优于 select 和 poll:当文件句柄数目达到 10K 的时候,epoll 已经超过 select 和 poll 两个数量级. 2 ...
- 技术干货:实时视频直播首屏耗时400ms内的优化实践
本文由“逆流的鱼yuiop”原创分享于“何俊林”公众号,感谢作者的无私分享. 1.引言 直播行业的竞争越来越激烈,进过2018年这波洗牌后,已经度过了蛮荒暴力期,剩下的都是在不断追求体验.最近正好在做 ...
- Spring Boot 2 (五):Docker Compose + Spring Boot + Nginx + Mysql 实践
Spring Boot 2 (五):Docker Compose + Spring Boot + Nginx + Mysql 实践 Spring Boot + Nginx + Mysql 是实际工作中 ...
- Nginx优化(十七)
[教程主题]:Nginx优化 [课程录制]: 创E [主要内容] Nginx 优化 nginx介绍 Nginx是俄罗斯人编写的十分轻量级的HTTP服务器,Nginx,它的发音为“engine X”,是 ...
- 高并发IM系统架构优化实践
互联网+时代,消息量级的大幅上升,消息形式的多元化,给即时通讯云服务平台带来了非常大的挑战.高并发的IM系统背后究竟有着什么样的架构和特性? 以上内容由网易云信首席架构师内部分享材料整理而成 相关阅读 ...
随机推荐
- ubuntu 20.04.1 安装 PHP+Nginx
ubuntu 20.04.1 安装 PHP+Nginx 更新源 sudo apt-get update 安装环境包 sudo apt-get -y install nginx sudo apt-get ...
- 使用纯c#在本地部署多模态模型,让本地模型也可以理解图像
之前曾经分享过纯c#运行开源本地大模型Mixtral-8x7B 当时使用的是llamasharp这个库和Mixtral的模型在本地部署和推理,前段时间我看到llamasharp更新到了0.11.1版本 ...
- axios 使用与 拦截器
未拦截使用使用: 由于axios每个请求都是异步.例如有ABC三个请求,C需要拿到AB请求回来的值作为参数,则需同步加载才能,所以使用axios.all才好完成.... 拦截器:为了处理axios中g ...
- 【笔记】Linux基础指令
Linux基础指令 cd 跳转文件夹 cd 到根目录 cd usr 到根目录下的usr目录 cd .. 到上一级目录 cd ~ 到home目录 cd - 到上次访问的目录 sh 执行sh命令 ls 查 ...
- 关于<property name="hibernate.hbm2ddl.auto"></property>中的参数填写
hibernate的数据库表自动生成参数 关于<property name="hibernate.hbm2ddl.auto"></property>中的参数 ...
- 网易:Flink + Iceberg 数据湖探索与实践
导读:今天主要和大家交流的是网易在数据湖 Iceberg 的一些思考与实践.从网易在数据仓库建设中遇到的痛点出发,介绍对数据湖 Iceberg 的探索以及实践之路. 主要内容包括: 数据仓库平台建设的 ...
- 项目版本管理的最佳实践:云效飞流Flow篇
简介: 飞流Flow的最佳实践(使用阿里云云效)为了更好地使用飞流Flow,接下来将结合阿里云云效来讲解飞流Flow的最佳实践 目录 一.分支规约 二.版本号规约 2.1 主版本号(首位版本号) 2. ...
- 【阿里云EMR实战篇】以EMR测试集群版本为例,详解 Flink SQL Client 集成 Hive 使用步骤
简介: 以测试集群版本为例(EMR-4.4.1)-- Flink SQL Client 集成 Hive 使用文档 作者:林志成,阿里云EMR产品团队技术支持,拥有多年开源大数据经验 1.以测试集群版本 ...
- 一文说清linux system load
简介:双十一压测过程中,常见的问题之一就是load 飙高,通常这个时候业务上都有受影响,比如服务rt飙高,比如机器无法登录,比如机器上执行命令hang住等等.本文就来说说,什么是load,load是 ...
- WPF 应用启动过程同时启动多个 UI 线程且访问 ContentPresenter 可能让多个 UI 线程互等
在应用启动过程里,除了主 UI 线程之外,如果还多启动了新的 UI 线程,且此新的 UI 线程碰到 ContentPresenter 类型,那么将可能存在让新的 UI 线程和主 UI 线程互等.这是多 ...