Nginx 参数、配置性能优化

[TOC]

Nginx 参数、配置性能优化

Linux 系统层面

conntrack 参数

一般,设置 nf_conntrack_max 为 200w, nf_conntrack_buckets 为 1/4 或者 1/2 倍 nf_conntrack_max,防止桶太大导致性能影响。

$ cat /proc/sys/net/netfilter/nf_conntrack_buckets
524288

$ cat /proc/sys/net/netfilter/nf_conntrack_max
2097152
复制代码

Backlog 队列

  • net.core.somaxconn

    • 可以排队等待 Nginx 接受的最大连接数。通常如果太小导致了 Nginx 性能问题可以查看内核日志发现这个状态
    • 配合 NGINX listen 指令一起调整。
  • net.core.netdev_max_backlog

    • 数据包在发送给 CPU 之前被网卡缓冲的速率;增加该值可以提高具有高带宽的机器的性能

文件描述符

文件描述符是操作系统资源,用于表示连接和打开的文件。Nginx 的一个连接最多可以使用两个文件描述符。比如当做反向代理的时候,一个文件描述符和 client 相连,一个文件描述符和后端 upstream 相连。

  • sys.fs.file-max

    • Linux 系统允许的最大文件描述数
    • cat /proc/sys/fs/file-max
  • nofile

    • 应用层面允许的最大文件描述符数,一般设置 /etc/security/limits.conf文件

Ports

  • net.ipv4.ip_local_port_range

    • port 端口的范围
  • 对压测端而言,如果是短链接

    • net.ipv4.tcp_tw_reuse = 1
      • 表示开启端口复用。允许将TIME-WAIT sockets重新用于新的TCP连接,默认为0,表示关闭;
    • net.ipv4.tcp_tw_recycle = 1
      • 表示开启TCP连接中TIME-WAIT sockets的快速回收,默认为0,表示关闭。

Nginx 层面

Open Files

  • worker_rlimit_nofile 65535;
    • nginx worker 进程的最大文件描述符 open files (RLIMIT_NOFILE)

Worker Processes 进程数

  • worker_processes

    • 一般设置为 auto,这样就是一个 CPU 一个 Nginx worker 进程
  • worker_connections

    • 每个 worker 进程的最大连接数,一般在大流量的情况下必须要调大这个数量,如 655350

Keepalive Connections 长连接

  • keepalive_requests

    • client 可以通过一个 keepalive 连接发出的最大请求数
  • keepalive_timeout

    • 活跃连接保持的最大超时时间
  • keepalive

    • 为每个 worker 进程保持打开状态的到 upstream 的空闲保持活动连接的数量。

为了保持后端 upstream 可以使用长连接,必须要做如下配置:

proxy_http_version 1.1;
proxy_set_header Connection "";   
复制代码

Events【multi_accept】

multi_accept指令如下,默认关闭,表示 nginx worker 进程一次只接收一个新的连接,如果开启表示 nginx worker 进程可以一次接收所有新的连接:

Syntax:	multi_accept on | off;
Default:	
multi_accept off;
Context:	events
复制代码
events {
        worker_connections 655350; 
        multi_accept on;
}
复制代码

不过,一般不需要开启,避免搞出一些请求不均的问题,虽然整体性能在短连接的场景下会有提升,但是优势不大。

Logging

access log

每个请求都进行日志记录会大量消耗 CPU 和 I/O 周期,减少影响的一种方法是启用访问日志缓冲。使用缓冲之后 Nginx 不是为每个日志条目执行单独的写操作,而是缓冲一系列条目,并在一个操作中将它们一起写到文件中。

access_log 指令中增加 buffer=size 就可以启用这个功能;这样的话,当 buffer 满了就会写到日志文件中,flush=time 参数可以定期将 buffer 中的日志刷到日志文件中,

error log

error_log /usr/local/nginx/logs/nginx_error.log error;

注意,这里的 error log,表示只有 error 级别的以上的就会打印到 error log 中。这里的级别包括 warn, error , crit, alert, emerg。指令的用法参考官网error_log

nginx Lua 的日志级别参考 Nginx log level constants

线上,一般可以把error 级别以上的,通过企业微信告警处理

Sendfile

操作系统的sendfile()系统调用将数据从一个文件描述符复制到另一个文件描述符,通常用来实现零拷贝,这可以加速TCP数据传输。要使 NGINX 能够使用它,在 http 上下文或服务器或位置上下文中包含 sendfile 指令。然后,NGINX 可以将缓存的或磁盘上的内容写入套接字,而不需要向用户空间切换任何上下文,这使得写入非常快,并且消耗更少的 CPU 周期。但是,由于使用 sendfile() 复制的数据绕过了用户空间,所以它不受常规 NGINX 处理链和更改内容的过滤器(如gzip)的约束。当配置上下文同时包含 sendfile 指令和激活内容更改过滤器的指令时,NGINX 将自动禁用该上下文的 sendfile。官方对sendfile的介绍如下,注意,directio 会自动禁用 sendfile:

In this configuration, sendfile() is called with the SF_NODISKIO flag which causes it not to block on disk I/O, but, instead, report back that the data are not in memory. nginx then initiates an asynchronous data load by reading one byte. On the first read, the FreeBSD kernel loads the first 128K bytes of a file into memory, although next reads will only load data in 16K chunks. This can be changed using the read_ahead directive.
复制代码

使用如下:

http {
    sendfile on;
}
复制代码

Limits

设置限制的目的是为了防止 client 消耗过多的资源导致 Nginx 服务器出现一些问题;通常用来降级。

  • limit_conn and limit_conn_zone

    • 限制 Nginx 接受的客户端连接的数量,比如对一个 ip 的限制
    • 防止单个客户端打开过多的连接并消耗超过其资源份额的资源。
  • limit_rate

    • 限制每个连接向 client 响应的速率;一个 client 有多个连接的话,那么就是每个连接都能够达到这个速率
    • 防止系统被某些客户机超载,从而确保为所有客户机提供更高质量的服务。
  • limit_req and limit_req_zone

    • 限制 Nginx 处理请求的速率;和 limit_rate 类似的优势。
  • max_conns

    • server 指令的 max_conns 参数,对后端 upstream 的最大连接限制,表示后端 upstream 可以接受的最大连接数

Buffers

  • client_body_buffer_size

    • 从 client 读取请求数据的最大大小
  • client_header_buffer_size

    • 从 client 读取请求头部的最大数据
  • client_max_body_size

    • 设置 client 端请求的最大大小,超过限制返回 413 或者 Request Entity Too Large
  • large_client_header_buffers

    • 设置 large client headers 的最大大小

推荐值如下:

client_body_buffer_size 10K;
client_header_buffer_size 1k;
client_max_body_size 8m;
large_client_header_buffers 4 4k;
复制代码

Timeout

  • client_header_timeout & client_body_timeout

    • 当请求开始后,nginx 等待 client 发送 header or body 的超时时间
  • keepalive_timeout

    • nginx 保持长连接的空闲连接保持时间
  • send_timeout

    • 发送给 client 响应的超时时间;如果超时则 nginx 主动关闭连接

推荐值如下:

client_body_timeout 12;
client_header_timeout 12;
keepalive_timeout 15;
send_timeout 10;
复制代码

还有一些连接超时:

proxy_connect_timeout 60;
proxy_send_timeout 60;
proxy_read_timeout 60;
复制代码

其他配置

主配置

user  www www;

worker_processes auto;

error_log  /usr/local/nginx/logs/nginx_error.log  error;
pid    /usr/local/nginx/nginx.pid;

worker_rlimit_nofile 65535;

events
{
    use epoll;
    worker_connections 65535;
}

复制代码

tcp_nodelay

使用如下:

http {
    sendfile on;
    tcp_nodelay on;
}
复制代码

gzip

gzip on;
    	gzip_vary on;
    	gzip_proxied any;
    	gzip_comp_level 1;
    	gzip_buffers 16 8k;
    	gzip_http_version 1.1;
    	gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;
复制代码

Open File Cache

参考

Tuning NGINX for Performance

How to Tune and Optimize Performance of Nginx Web Server

【"欢迎关注我的微信公众号:Linux 服务端系统研发,后面会大力通过微信公众号发送优质文章"】

猜你喜欢

转载自juejin.im/post/5d7a4320f265da03b31bfc14