Nginx性能优化(十 一)

题外话:JD的架构为SOA,他的一个网页里面各种连接都是不一样的。如
京东的首页网址是jd.com。
京东的类目页网址是list.jd.com。
点击一个具体的商品后网址是item.jd.com。
在这里插入图片描述
可参考:https://blog.csdn.net/li_canhui/article/details/89294683

性能优化概述

在做性能优化前,我们需要对如下进行考虑

1.当前系统结构瓶颈
。观察指标
。压力测试·
2.了解业务模式
。接口业务类型
。系统层次化结构
3.性能与安全
。性能好安全弱
。安全好性能低

一、压力测试工具ab

市面上的产品如:压测宝 (可了解)

yum install httpd-tools -y
ab -n 200 -c 2 http://127.0.0.1/
#-n 总的请求次数; -c 并发请求数 ; -k 是否开启长连接,Enable the HTTP KeepAlive feature
开启长链接的话,Requests per second会有明显的提升。
Requests per second:    5986.05 [#/sec] (mean)  (未加参数 -k)
Requests per second:    10296.38 [#/sec] (mean)  (加了参数 -k)

这里能5000, 10000是因为在本机,走的是环回网络,几乎没有网络资源的消耗。如是走外网,考虑到会有I/O,硬盘,网络设备等因素影响。

nginx配置禁止ab压力测试

可写在location / { } 里面
在这里插入图片描述
在这里插入图片描述
下面是压测数据 与 部分注释

[root@lb-136 testFile]# ab -n 2000 -c 2 http://127.0.0.1/
This is ApacheBench, Version 2.3 <$Revision: 1430300 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 127.0.0.1 (be patient)
Completed 200 requests
Completed 400 requests
Completed 600 requests
Completed 800 requests
Completed 1000 requests
Completed 1200 requests
Completed 1400 requests
Completed 1600 requests
Completed 1800 requests
Completed 2000 requests
Finished 2000 requests
这里是压测的进度条

Server Software:        nginx/1.18.0   #nginx的版本
Server Hostname:        127.0.0.1      #地址
Server Port:            80             #端口

Document Path:          /       #请求页面
Document Length:        30 bytes  #多少字节

Concurrency Level:      2   #并发数
Time taken for tests:   0.334 seconds   #用时
Complete requests:      2000   #总请求次数
Failed requests:        0    #失败次数
Write errors:           0
Total transferred:      522000 bytes  #总共传输的字节
HTML transferred:       60000 bytes   #html的字节
##	每秒多少请求/s(总请求出/总共完成的时间)
Requests per second:    5986.05 [#/sec] (mean) 

#	客户端访问服务端,	单个请求所需花费的时间	
Time per request:       0.334 [ms] (mean)
#	服务端处理请求的时间
Time per request:       0.167 [ms] (mean, across all concurrent requests)
#	判断⽹络传输速率,	观察⽹络是否存在瓶颈
Transfer rate:          1525.74 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.2      0       8
Processing:     0    0   0.7      0      30
Waiting:        0    0   0.7      0      30
Total:          0    0   0.8      0      30

Percentage of the requests served within a certain time (ms)
  50%      0
  66%      0
  75%      0
  80%      0
  90%      1
  95%      1
  98%      1
  99%      1
 100%     30 (longest request)

三、影响性能指标

影响性能方便整体关注
1.网络
。网络的流量
。网络是否丢包
。这些会影响http的请求与调用.
2.系统
o硬件有没有磁盘损坏,磁盘速率
。系统负载、内存、系统稳定性.
3.服务
。连接优化、请求优化
。根据业务形态做对应的服务设置
4.程序
。接口性能
。处理速度o程序执行效率·
5.数据库
每个架构服务与服务之间都或多或少有一些关联,我们需要将整个架构进行分层,找到对应系统或服务的短板,然后进行优化

四、系统性能优化

文件句柄,Linux一切皆文件,文件句柄可以理解为就是一个索引
。文件句柄会随着我们进程的调用频繁增加
。系统默认对文件句柄有限制,不能让一个进程无限的调用
。需要限制每个进程和每个服务使用多大的文件句柄
。文件句柄是必须要调整的优化参数

设置方式
。系统全局性修改
。用户局部性修改
。进程局部性修改

#查看
ulimit -n

vim	/etc/security/limits.conf
//针对root用户
root	soft	nofile	65535
root	hard	nofile	65535
//所有用户,	全局
*	soft	nofile	25535
*	hard	nofile	25535


=====分割线=======
//对于Nginx进程 。 在nginx的conf里面写
worker_rlimit_nofile	45535;
//root用户
//soft提醒
//hard限制
//nofile文件数配置项
//65535最大大小

在这里插入图片描述

五、CPU affinity (nginx与CPU亲和)

.查看当前 CPU 物理状态

 lscpu | grep -i "cpu(s)"

在这里插入图片描述
也可用 top指令,输入数字1,查看CPU数量

将 Nginx worker 进程绑到不同的核心上,在nginx.conf上配置

#worker_processes	2;
#worker_cpu_affinity	101010101010 010101010101;

worker_processes auto;
worker_cpu_affinity auto;

可参考:https://blog.csdn.net/u011957758/article/details/50959823
官网文档:http://nginx.org/en/docs/ngx_core_module.html#worker_cpu_affinity

查看 nginx worker 进程绑定至对应 cpu

[root@lb-136 ~]# ps -eo pid,args,psr | grep [n]ginx
  6793 nginx: master process /srv/   1
  9261 nginx: master process nginx   0
  9473 nginx: worker process         0
  9474 nginx: worker process         1
  9864 nginx: master process nginx   0
  9923 nginx: worker process         0
  9924 nginx: worker process         0
119995 nginx: worker process         0
119996 nginx: worker process         0

额外插个搜索技巧
site: nginx.org worker_cpu_affinity
在这里插入图片描述

Nginx通用优化配置文件

[root@nginx	~]#	cat	nginx.conf
user	nginx;
worker_processes	auto;
worker_cpu_affinity	auto;
error_log	/var/log/nginx/error.log	warn;
pid	/run/nginx.pid;
#调整⾄1w以上,负荷较⾼建议2-3w以上
worker_rlimit_nofile	35535;
events	{
				use	epoll;
#限制每个进程能处理多少个连接请求,10240x16
				worker_connections	10240;
}
http	{
				include													/etc/nginx/mime.types;
				default_type								application/octet-stream;
#	统⼀使⽤utf-8字符集
				charset	utf-8;
				
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
                '$status $body_bytes_sent "$http_referer" '
                '"$http_user_agent" "$http_x_forwarded_for" '
                '$connection $upstream_addr '
                 '$upstream_response_time $request_time';
access_log  logs/access.log  main;


#	Core	module
				sendfile												on;
#	静态资源服务器建议打开
				tcp_nopush										on;
#	动态资源服务建议打开,需要打开keepalived
				tcp_nodelay									on;
				keepalive_timeout			65;
#	Gzip	module 。
				gzip	on;
				#老旧的浏览器IE1-6 不进行gzip压缩,IE他压缩后页面可能会出问题 
				gzip_disable	"MSIE	[1-6]\.";
				gzip_http_version	1.1;
#	Virtal	Server
				include	/etc/nginx/conf.d/*.conf;
}

other参考:
在这里插入图片描述
在这里插入图片描述
IO ,socket的
在这里插入图片描述
在这里插入图片描述
fastcgi设置
在这里插入图片描述
proxyCache 设置
在这里插入图片描述
在这里插入图片描述

END

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值