Nginx性能优化

性能优化概述

在做性能优化前, 我们需要对如下进⾏考虑
1.当前系统结构瓶颈
观察指标
压⼒测试

2.了解业务模式
接⼝业务类型
系统层次化结构

3.性能与安全
性能好安全弱
安全好性能低

压⼒测试⼯具

1.安装压⼒测试⼯具 ab
[root@nginx-lua ~]# yum install httpd-tools -y

2.了解压测⼯具使⽤⽅式
[root@nginx-lua ~]# ab -n 200 -c 2 http://127.0.0.1/
//-n总的请求次数
//-c并发请求数
//-k是否开启⻓连接

3.配置 Nginx 静态⽹站与 tomcat 动态⽹站环境
[root@nginx-lua conf.d]# cat jsp.conf
server {
 server_name jsp.wingsredevsecops.top;
 listen 80;
 location / {
 root /soft/code/jsp/;
 try_files $uri @java_page;
 index index.jsp index.html;
 }
 location @java_page{
 proxy_pass http://10.1.106.66:8080;
 }
}

//分别给Nginx准备静态⽹站
[root@nginx-lua ~]# cat /soft/code/jsp/wing.html
<h1> Ab Load </h1>
//给Tomcat准备静态⽹站⽂件
[root@tomcat-node1-20 ROOT]# cat /soft/tomcat-8080/webapps/ROOT/wing.html
<h1> Tomcat Ab Load </h1>

4.使⽤ ab ⼯具进⾏压⼒测试
//进⾏压⼒测试
[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

5.将 nginx 下的 wing.html ⽂件移⾛, 再次压测会由 tomcat 进⾏处理
[root@lb-node1 jsp]# mv wing.html{,.bak}
Concurrency Level: 200
Time taken for tests: 1.028 seconds
Complete requests: 2000
Failed requests: 0
Write errors: 0
Total transferred: 510000 bytes
HTML transferred: 38000 bytes
Requests per second: 1945.09 [#/sec] (mean)
Time per request: 102.823 [ms] (mean)
Time per request: 0.514 [ms] (mean, across all concurrent requests)
Transfer rate: 484.37 [Kbytes/sec] received

影响性能指标

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

系统性能优化

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

设置⽅式
系统全局性修改
⽤户局部性修改
进程局部性修改

vim /etc/security/limits.conf

//针对root⽤户
root soft nofile 1048576
root hard nofile 1048576s

//所有⽤户, 全局
* soft nofile 1048576
* hard nofile 1048576

//对于Nginx进程
worker_rlimit_nofile 45535;

//root⽤户
//soft提醒
//hard限制
//nofile⽂件数配置项
//65535最⼤⼤⼩

Nginx性能优化

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

1.查看当前 CPU 物理状态
[root@i-m60lx3hh conf]# lscpu |grep "CPU(s)"
CPU(s):                2
On-line CPU(s) list:   0,1
NUMA node0 CPU(s):     0,1

//1颗物理cpu,每颗cpu1核⼼, 总共2核⼼

2.将 Nginx worker 进程绑到不同的核⼼上
//启动多少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;

3.查看 nginx worker 进程绑定⾄对应 cpu
ps -eo pid,args,psr|grep [n]ginx

4. 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以上
#⽤于限制每个nginx worker进程的打开⽂件数量的最⼤值
worker_rlimit_nofile 65535;

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"';
 access_log /var/log/nginx/access.log main;

# Core module
 sendfile on;
# 静态资源服务器建议打开
 tcp_nopush on;
# 动态资源服务建议打开,需要打开keepalived
 tcp_nodelay on;
 keepalive_timeout 65;

# Gzip module
 gzip on;
 gzip_disable "MSIE [1-6]\.";
 gzip_http_version 1.1;

# Virtal Server
 include /etc/nginx/conf.d/*.conf;
}

Nginx性能优化总结

安全: 隐藏版本号 加密传输 csof 防盗链 refere 限制上传类型 访问控制acess waf 等
性能: 硬件 Epool 连接数 max gzip 压缩 expire sendfile 零拷⻉
posted @ 2025-03-15 17:46  basickill  阅读(78)  评论(0)    收藏  举报