nginx 性能优化

nginx 性能优化

在做性能优化前需要考虑的几块 :

  1. 系统结果瓶颈
  2. 业务模式
  3. 性能与安全

压力测试工具

ab

简单的压力测试工具,安装方式 :

yum install httpd-tools -y

ab 使用:

ab -n 200 -c 2 http://localhost

-n 总请求次数
-c 并发连接数
-k 是否开启长连接

进行压力测试

//进⾏压⼒测试
[root@Nginx conf.d]# ab -n2000 -c2 http://jsp.wingsredevsecops.top/wing.html
...
Server Software: nginx/1.24.0
Server Hostname: jsp.wingsredevsecops.top
Server Port: 80
Document Path: /wing.html
Document Length: 19 bytes
Concurrency Level: 200
# 总花费总时⻓
Time taken for tests: 0.143 seconds
# 总请求数
Complete requests: 2000
# 请求失败数
Failed requests: 0
Write errors: 0
Total transferred: 510000 bytes
HTML transferred: 38000 bytes
# 每秒多少请求/s(总请求出/总共完成的时间)
Requests per second: 9333.23 [#/sec] (mean)
# 客户端访问服务端, 单个请求所需花费的时间
Time per request: 101.315 [ms] (mean)
# 服务端处理请求的时间
Time per request: 0.507 [ms] (mean, across all concurrent requests)
# 判断⽹络传输速率, 观察⽹络是否存在瓶颈
Transfer rate: 491.58 [Kbytes/sec] received

影响性能指标

网络

  • 网络的流量
  • 网络的丢包率,是否丢包

这些会影响http的请求与调用

系统

  • 大量请求时,磁盘IO的速率,磁盘是否存在坏道
  • 系统负载,内存,CPU,稳定性

服务

  • 连接优化,请求优化 sysctl.conf

程序

  • 接口性能
  • 处理速度
  • 程序执行效率

数据库与中间件

nginx 具体优化项与配置

文件句柄

inux/Unix上,一切皆文件,每一次用户发起请求就会生成一个文件句柄,文件句柄可以理解为就是一个索引,所以文件句柄就会随着请求量的增多,而进程调用的频率增加,文件句柄的产生就越多,系统对文件句柄默认的限制是1024个,对Nginx来说非常小了,需要改大一点

CPU绑定

减少进程之间不断频繁迁移, 减少性能损耗

//启动多少worker进程, 官⽅建议和cpu核⼼⼀致, 第⼀种绑定组合⽅式
#worker_processes 24;
#worker_cpu_affinity 000000000001 000000000010 000000000100 000000001000 000000010000
000000100000 000001000000 000010000000 000100000000 001000000000 010000000000
10000000000;
//第⼆种⽅式
#worker_processes 2;
#worker_cpu_affinity 101010101010 010101010101;
//最佳⽅式绑定⽅式
worker_processes auto;
worker_cpu_affinity auto;

epool 模型

nginx的连接处理机制在于不同的操作系统会采用不同的I/O模型,Linux下,nginx使用epoll的I/O多路复用模型,在freebsd使用kqueue的IO多路复用模型,在solaris使用/dev/pool方式的IO多路复用模型,在windows使用的icop等等。要根据系统类型不同选择不同的事务处理模型,我们使用的是Centos,因此将nginx的事件处理模型调整为epoll模型。

events {
    worker_connections  10240;    //
    use epoll;
}

worker_connections 连接数

设置每个worker进程可连接的最大连接数

worker_connections  10240;

keepalive timeout会话保持时间

keepalive_timeout  60;

GZIP压缩性能优化

gzip on;       #表示开启压缩功能
gzip_min_length  1k; #表示允许压缩的页面最小字节数,页面字节数从header头的Content-Length中获取。默认值是0,表示不管页面多大都进行压缩,建议设置成大于1K。如果小于1K可能会越压越大
gzip_buffers     4 32k; #压缩缓存区大小
gzip_http_version 1.1; #压缩版本
gzip_comp_level 6; #压缩比率, 一般选择4-6,为了性能gzip_types text/css text/xml application/javascript;  #指定压缩的类型 gzip_vary on; #vary header支持

隐藏版本号

从配置文件中隐藏

![](https://img2024.cnblogs.com/blog/1612727/202404/1612727-20240409145005200-1189287958.png)

从源码处隐藏

[root@localhost core]# pwd
/usr/src/nginx-1.16.0/src/core
[root@localhost core]# vim nginx.h

![](https://img2024.cnblogs.com/blog/1612727/202404/1612727-20240409144931752-511722583.png)

加密传输

csof

防倒链

location / {
  valid_referers none blocked example.com *.example.com;
  if($invalid_referer) {
    return 403;
  }

}

限制上传类型

location /upload {
    upload_pass @upload_handler;
    upload_store /path/to/upload;
    upload_store_access user:rw;
    upload_set_form_field $upload_field_name.name "$upload_file_name";
    upload_set_form_field $upload_field_name.content_type "$upload_content_type";
    upload_set_form_field $upload_field_name.path "$upload_tmp_path";
    upload_cleanup 400 404 499 500-505;
    upload_limit_rate 10k;
    upload_types image/jpg image/jpeg image/png image/gif; #只允许上传jpg、jpeg、png、gif类型
}

访问控制

server {
    listen 80;
    server_name localhost;
    location ~ ^/admin {
        root /home/www/html;
        index index.html index.htl;
        deny 192.168.0.0/16;
        allow all;
    }
}

sendfile 开启高效传输模式

sendfile on; # 开启高效文件传输模式。
tcp_nopush on; #需要在sendfile开启模式才有效,防止网路阻塞,积极的减少网络报文段的数量。将响应头和正文的开始部分一起发送,而不一个接一个的发送。

posted @ 2024-04-09 14:38  DreamDZhu  阅读(8)  评论(0编辑  收藏  举报