通常来说,一个优化良好的 nginx linux 服务器可以达到 500,000 – 600,000 次/秒 的请求处理性能,然而我的 nginx 服务器可以稳定地达到 904,000 次/秒 的处理性能,并且我以此高负载测试超过 12 小时,服务器工作稳定。
这里需要特别说明的是,本文中所有列出来的配置都是在我的测试环境验证的,而你需要根据你服务器的情况进行配置:
从 epel 源安装 nginx:
1
yum -y install nginx
备份配置文件,然后根据你的需要进行配置:
1
2
cp /etc/nginx/nginx.conf /etc/nginx/nginx.conf.orig
vim /etc/nginx/nginx.conf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# this number should be, at maximum, the number of cpu cores on your system.
# (since nginx doesn't benefit from more than one worker per cpu.)
# 这里的数值不能超过 cpu 的总核数,因为在单个核上部署超过 1 个 nginx 服务进程并不起到提高性能的作用。
worker_processes 24;
# number of file descriptors used for nginx. this is set in the os with 'ulimit -n 200000'
# or using /etc/security/limits.conf
# nginx 最大可用文件描述符数量,同时需要配置操作系统的 ulimit -n 200000,或者在 /etc/security/limits.conf 中配置。
worker_rlimit_nofile 200000;
# only log critical errors
# 只记录 critical 级别的错误日志
error_log /var/log/nginx/error.log crit
# determines how many clients will be served by each worker process.
# (max clients = worker_connections * worker_processes)
# max clients is also limited by the number of socket connections available on the system (~64k)
# 配置单个 nginx 单个进程可服务的客户端数量,(最大值客户端数 = 单进程连接数 * 进程数 )
# 最大客户端数同时也受操作系统 socket 连接数的影响(最大 64k )
worker_connections 4000;
# essential for linux, optmized to serve many clients with each thread
# linux 关键配置,允许单个线程处理多个客户端请求。
use epoll;
# accept as many connections as possible, after nginx gets notification about a new connection.
# may flood worker_connections, if that option is set too low.
# 允许尽可能地处理更多的连接数,如果 worker_connections 配置太低,会产生大量的无效连接请求。
multi_accept on;
# caches information about open fds, freqently accessed files.
# changing this setting, in my environment, brought performance up from 560k req/sec, to 904k req/sec.
# i recommend using some varient of these options, though not the specific values listed below.
# 缓存高频操作文件的fds(文件描述符/文件句柄)
# 在我的设备环境中,通过修改以下配置,性能从 560k 请求/秒 提升到 904k 请求/秒。
# 我建议你对以下配置尝试不同的组合,而不是直接使用这几个数据。
open_file_cache max=200000 inactive=20s;
open_file_cache_valid 30s;
open_file_cache_min_uses 2;
open_file_cache_errors on;
# buffer log writes to speed up io, or disable them altogether
# 将日志写入高速 io 存储设备,或者直接关闭日志。
# access_log /var/log/nginx/access.log main buffer=16k;
access_log off;
# sendfile copies data between one fd and other from within the kernel.
# more efficient than read() + write(), since the requires transferring data to and from the user space.
# 开启 sendfile 选项,使用内核的 fd 文件传输功能,这个比在用户态用 read() + write() 的方式更加高效。
sendfile on;
# tcp_nopush causes nginx to attempt to send its http response head in one packet,
# instead of using partial frames. this is useful for prepending headers before calling sendfile,
# or for throughput optimization.
# 打开 tcp_nopush 选项,nginux 允许将 http 应答首部与数据内容在同一个报文中发出。
# 这个选项使服务器在 sendfile 时可以提前准备 http 首部,能够达到优化吞吐的效果。
tcp_nopush on;
# don't buffer data-sends (disable nagle algorithm). good for sending frequent small bursts of data in real time.
# 不要缓存 data-sends (关闭 nagle 算法),这个能够提高高频发送小数据报文的实时性。
tcp_nodelay on;
# timeout for keep-alive connections. server will close connections after this time.
# 配置连接 keep-alive 超时时间,服务器将在超时之后关闭相应的连接。
keepalive_timeout 30;
# number of requests a client can make over the keep-alive connection. this is set high for testing.
# 单个客户端在 keep-alive