通用优化
修改/etc/nginx/nginx.conf
user nginx;
worker_processes 16; // 调至CPU核心数
worker_cpu_affinity auto; //一个work分配一个CPU,需要配合上面worker_processes
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
work_rlimit_nofile 35535; //nginx进程文件句柄,建议调整到1万以上
events {
use epoll; // 采用epoll 方式
worker_connections 10240; //每个worker能处理的最大连接数
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
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;
sendfile on;
#tcp_nopush on;
keepalive_timeout 65;
gzip on; //打开压缩
gzip_disable "MSIE [1-6]\."; //IE有些不支持
gzip_http_version 1.1;
include /etc/nginx/conf.d/*.conf;
}
ab 测试
docker run -d -P nginx
$ yum install httpd-tools
$ ab -n 2000 -c 2 http://172.17.0.2:80/
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 172.17.0.2 (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.17.2
Server Hostname: 172.17.0.2
Server Port: 80
Document Path: /
Document Length: 612 bytes
Concurrency Level: 2
Time taken for tests: 0.261 seconds
Complete requests: 2000
Failed requests: 0
Write errors: 0
Total transferred: 1690000 bytes
HTML transferred: 1224000 bytes
Requests per second: 7652.51 [#/sec] (mean)
Time per request: 0.261 [ms] (mean) // 一个请求的平均时间,包括网络传输
Time per request: 0.131 [ms] (mean, across all concurrent requests) //一个请求的服务起响应时间
Transfer rate: 6314.82 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 0
Processing: 0 0 0.1 0 3
Waiting: 0 0 0.1 0 2
Total: 0 0 0.2 0 3
Percentage of the requests served within a certain time (ms)
50% 0
66% 0
75% 0
80% 0
90% 0
95% 0
98% 1
99% 1
100% 3 (longest request) //解释:100%的请求在3ms内返回
参数解释
ab -n 2000 -c 2 http://172.17.0.2:80/
- -n 总的请求数
- -c 并发数
- -k 是否开启长连接