0


nginx性能优化

1.CPU亲缘性

修改全局配置:

worker_processes [number | auto];

#启动Nginx工作进程的数量一般设为和CPU核心数相同

worker_cpu_affinity 00000001 00000010 00000100 00001000 | auto;

#将Nginx工作进程绑定到指定的CPU核心,默认Nginx是不进行进程绑定的,绑定并不是意味着当前nginx进程独占以一核心CPU,但是可以保证此进程不会运行在其他核心上,这就极大减少了nginx的工作进程在不同的cpu核心上的来回跳转,减少了CPU对进程的资源分配与回收以及内存管理等,因此可以有效的提升nginx服务器的性能

设置固定值:

worker_processes 4;
worker_cpu_affinity 00001000 00000100 00000010 00000001;
[root@centos8 ~]# ps axo pid,cmd,psr | grep nginx
31093 nginx: master process /apps 1
34474 nginx: worker process 1
34475 nginx: worker process 3
34476 nginx: worker process 5
34477 nginx: worker process 7
35751 grep nginx

设置默认值:

worker_processes auto;

worker_cpu_affinity auto;

[root@nginx ~]# ps axo pid,cmd,psr | grep nginx
1362 nginx: master process /apps 1
1647 nginx: worker process 0
1648 nginx: worker process 1
1649 nginx: worker process 2
1650 nginx: worker process 3
1652 grep --color=auto nginx 3
#系统会根据拥有的CUP内核数分配进程

2.进程优先级

修改全局配置:

worker_priority -20;

#工作进程优先级,-20~19(超过-20,按-20,超过19,按19)

[root@nginx ~]# ps -axo pid,cmd,nice | grep nginx
1362 nginx: master process /apps 0
48255 nginx: worker process -20
48256 nginx: worker process -20
48257 nginx: worker process -20
48258 nginx: worker process -20
48260 grep --color=auto nginx 0

3.实现高并发

1)客户端向服务端发送两万个请求

[root@centos7 ~]#ulimit -n 102400

[root@centos7 ~]#ab -c 10000 -n 20000 http://10.0.0.8/;

2)查看nginx错误日志,报Too many open files错误

[root@centos7 ~]#tail /apps/nginx/logs/error.log

2022/012/04 21:19:33 [crit] 41006#0: *1105860 open() "/apps/nginx/html/50x.html" failed (24: Too many open files), client: 10.0.0.7, server: localhost, request: "GET / HTTP/1.0", host: "10.0.0.8" 2022/12/04 21:19:33 [crit] 41006#0: accept4() failed (24: Too many open files)

2022/12/04 21:19:33 [crit] 41006#0: *1114177 open() "/apps/nginx/html/index.html" failed (24: Too many open files), client: 10.0.0.7, server: localhost, request: "GET / HTTP/1.0", host: "10.0.0.8"

3)修改并发数

#如果systemd启动,则需要修改nginx.service(自启动)文件中加LimitNOFILE=100000,才能有效

[root@centos7 ~]#vim /lib/systemd/system/nginx.service

[Service]

.............省略...............

LimitNOFILE=100000

.............省略..............

#如果非systemd启动,可以修改下面pam限制,然后重启服务

[root@centos7 ~]#vim /etc/security/limits.conf

.............省略...............

  • soft nofile 1000000

  • hard nofile 1000000

.............省略...............

[root@centos7 ~]#vim /apps/nginx/conf/nginx.conf

.............省略...............

worker_rlimit_nofile 100000;

.............省略...............

[root@centos7 ~]#systemctl restart nginx

4)重新向服务端发请求就没有错误了

待更新............................................................................................


本文转载自: https://blog.csdn.net/zhanghongshun624/article/details/128167042
版权归原作者 a rookie. 所有, 如有侵权,请联系我们删除。

“nginx性能优化”的评论:

还没有评论