【整理】Nginx 战斗准备 —— 优化指南


本文内容参考自《   Nginx 战斗准备 —— 优化指南   》。  

【一句话总结】  
本文不是一个全面的微调指南,而是让你了解哪些设置能够让你在大量客户端访问时拥有良好的性能,为什么它们会提高性能。  

【知识点】  
  • worker_processes 定义了 nginx 对外提供 web 服务时的 worder 进程数。
  • worker_rlimit_nofile 更改 worker 进程的最大打开文件数限制。
  • events 模块中包含 nginx 中所有和处理连接相关的设置。
  • worker_connections 的值和 worker_rlimit_nofile 值有关系,该值的大小同时受限于系统可以使用的端口数目(~64k)。
  • multi_accept 告诉 nginx 在收到关于新连接的可读通知后,应尽可能处理掉当前存在的所有来自客户端的连接。
  • use 设置用于采用哪种 I/O 复用方式。
  • http 模块控制着 nginx http 处理的所有核心特性。
  • server_tokens 并不会让 nginx 执行的速度更快,但它可以关闭在错误页面中的 nginx 版本数字,这样对于安全性是有好处的。
  • sendfile 使能 sendfile() 的使用,在内核态完成相关动作,更高效。
  • tcp_nopush 告诉 nginx 在一个数据包里发送所有 HTTP 包头,而不是通过 TCP 的 PSH 方式一个一个发送。
  • tcp_nodelay 告诉 nginx 不要在发送小块数据的时候进行缓存,而是将短小的数据片段即时发送出去。这个选项用于要求频繁发送小数据片段,而不需要获得即时回应的应用,因为对这种应用来说,即时投递的能力才是必须的。
  • access_log 设置 nginx 是否将存储访问日志。关闭这个选项可以让读取磁盘 I/O 操作更快(这个不太好关闭吧)。
  • error_log 告诉 nginx 只能记录严重的错误。
  • keepalive_timeout 给客户端分配 keep-alive 链接超时时间。服务器将在这个超时时间过后关闭链接。我们将它设置低些可以让 ngnix 持续工作的时间更长。
  • client_header_timeout 和 client_body_timeout 设置在一定时间内没有收到来自客户端的请求的 header 或 body 部分则认为超时。
  • reset_timeout_connection 告诉 nginx 在发生连接超时后,通过向客户端发送 RST 直接重置不响应的客户端连接。这将会释放那个客户端所占有的内存空间(使用 RST 方式重置可能会有副作用)。
  • send_timeout 指定 nginx 发送响应的超时时间,即 nginx 向客户端发送了数据包,但客户端一致没有处理该数据包的情况。该超时时间不是指一个应答被完整传输的时间,而是用于约束两次连续的客户端侧读操作的时间间隔。
  • limit_conn_zone 用于设置共享内存区相关信息。共享内存区中可以保存各种 key 的状态信息(例如当前连接数据)。用法:limit_conn_zone $binary_remote_addr zone=addr:5m;
  • limit_conn 设置与指定 key 值关联的连接数的最大值。用法:limit_conn addr 100; 表示每个 ip 地址上仅允许 100 个并发连接。
  • include 只是一个在当前文件中包含另一个文件内容的指令。可以使用通配符 * 。
  • default_type 设置文件使用的默认的 MIME-type 。
  • charset 设置在 header 中携带的默认字符集。
  • gzip 设置 nginx 对发送的数据进行 gzip 压缩。
  • gzip_disable 用于设置针对特定的客户端不使用 gzip 压缩。例如 gzip_disable "msie6"; 表示针对 IE6 不做 gzip 。
  • gzip_static 告诉 nginx 在对资源进行 gzip 压缩前,通过名字查找是否已有预压缩过的资源。这就允许你可以采用高压缩比算法事先进行压缩,而不用在请求时才执行 gzip 压缩。
  • gzip_proxied allows or disallows compression of a response based on the request/response. We’ll set it to any, so we gzip all requests.
  • gzip_min_length 设置进行 gzip 压缩时数据大小的字节数下限。例如 gzip_min_length 1000; 表示当请求的大小低于 1000 字节,将不采用 gzip 进行压缩,因为毕竟 gzip 压缩会拖慢请求处理进程。
  • gzip_comp_level 设置 gzip 压缩级别。级别范围 1~9,其中 9 是最高压缩级别,但也是最慢的。一般设置中间值 4 比较好。
  • gzip_type 设置针对那些数据类型进行 gzip 压缩的。例如
  • gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
  • open_file_cache 启用文件缓存,同时可以指定在该缓存中可以存储的元素的最大个数,以及淘汰时间。
  • open_file_cache_valid 设置检验缓存中元素有效性的频率。
  • open_file_cache_min_uses 与 open_file_cache 中的 inactive 参数配合使用。如果在 inactive 指定时间内,访问次数超过 open_file_cache_min_uses 指定的值,则不会淘汰出缓存。
  • open_file_cache_errors 设置是否缓存打开、查找文件错误的信息。

?
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
user www-data;
pid /var/run/nginx .pid;
worker_processes auto;
worker_rlimit_nofile 100000;
 
events {
     worker_connections 2048;
     multi_accept on;
     use epoll;
}
 
http {
     server_tokens off;
     sendfile on;
     tcp_nopush on;
     tcp_nodelay on;
 
     access_log off;
     error_log /var/log/nginx/error .log crit;
 
     keepalive_timeout 10;
     client_header_timeout 10;
     client_body_timeout 10;
     reset_timedout_connection on;
     send_timeout 10;
 
     limit_conn_zone $binary_remote_addr zone=addr:5m;
     limit_conn addr 100;
 
     include /etc/nginx/mime .types;
     default_type text /html ;
     charset UTF-8;
 
     gzip on;
     gzip_disable "msie6" ;
     gzip_proxied any;
     gzip_min_length 1000;
     gzip_comp_level 6;
     gzip_types text /plain text /css application /json application /x-javascript text /xml application /xml application /xml +rss text /javascript ;
 
     open_file_cache max=100000 inactive=20s;
     open_file_cache_valid 30s;
     open_file_cache_min_uses 2;
     open_file_cache_errors on;
 
     include /etc/nginx/conf .d/*.conf;
     include /etc/nginx/sites-enabled/ *;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值