Nginx的性能优化

1、优化worker进程个数:

   在高并发、高访问量的WEB服务场景,需要事先启动更多的nginx进程,以保证快速响应并处理大量并发用户的请求,优化nginx进程个数的配置项就是,在nginx.conf主配置文件中的,如下:

worker_processes  1;        # 指定nginx默认开启的进程数,修改末尾数字

  那对于这个值要怎么设置,官方给的参考:

  一开始的时候我们可以先对CPU的核数进行查看,根据CPU的核数设置这个值,在一开始的时候设置的值可以等于CPU的核数也可大于CPU的核数,这样就会缩短服务的瞬时开销的时间提高访问速度,在高并发、高访问量的情况下也可以设置CPU核数*2,具体的实际情况根据需求来挑选,除了考虑CPU以外还要考虑硬盘和系统的负载

  这里提供一些关于统计CPU核心数和修改进程数的命令,如下:

[root@Nginx conf]# grep -c processor /proc/cpuinfo             # 查看CPU的核心数
2
[root@Nginx conf]# grep "physical id" /proc/cpuinfo|sort|uniq|wc -l   # 查看CPU的总颗数
1
[root@Nginx conf]# grep worker_processes nginx.conf        # 查看NGINX的开启进程的值
worker_processes  1;
[root@Nginx conf]# sed -i "s#worker_processes  1#worker_processes  6#g" nginx.conf   # 修改NGINX开启进程的值
[root@Nginx conf]# 
[root@Nginx conf]# grep worker_processes nginx.conf        # 在次查看值是否成功修改
worker_processes  6;
[root@Nginx conf]# ../sbin/nginx -s reload              # 平滑重启NGINX服务
[root@Nginx conf]# ps aux | grep nginx | grep -v grep           # 查看nginx开启进程
root      31719  0.0  0.1  46632  1968 ?        Ss   04:08   0:00 nginx: master process sbin/nginx     # 这是NGINX的管理进程,不包括在开启的worker进程中
nginx     86630  0.0  0.1  46632  1976 ?        S    10:33   0:00 nginx: worker process
nginx     86631  0.0  0.1  46632  1976 ?        S    10:33   0:00 nginx: worker process
nginx     86632  0.0  0.1  46632  1976 ?        S    10:33   0:00 nginx: worker process
nginx     86633  0.0  0.1  46632  1976 ?        S    10:33   0:00 nginx: worker process
nginx     86634  0.0  0.1  46632  1976 ?        S    10:33   0:00 nginx: worker process
nginx     86635  0.0  0.1  46632  1976 ?        S    10:33   0:00 nginx: worker process  

2、绑定不同nginx进程到不同cpu上:

  我们也知道,当所有的进程都在一个CPU上运行的时候,会导致NGINX进程的使用硬件的资源不均,那我们怎么解决这个事情呢,好办 哈哈 那就是把每个进程发送到不同的cpu上,这样就可以让CPU的资源得到充分利用

  需要在nginx.conf主配置文件中添加  如下:(红色标记位置添加)

[root@Nginx conf]# cat nginx.conf
worker_processes  4;
worker_cpu_affinity 0001 0010 0100 100;   # 添加此行内容(这个配置nginx进程与cpu核心数的亲和力参数,就是把不同的进程给到不同的CPU)
error_log logs/error.log;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    server_tokens   on;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    sendfile        on;
    keepalive_timeout  65;
include www_date/brian.conf;
include www_date/brianzjz.conf;
include www_date/status.conf;
}

   修改完成后,我们可以通过压力测试工具(webbench)来对压力测试:

[root@Nginx conf]# webbench -c 200 -t 180 http://127.0.0.1/    # 压力测试工具的使用
Webbench - Simple Web Benchmark 1.5
Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.

Benchmarking: GET http://127.0.0.1/
200 clients, running 180 sec.

Speed=411369 pages/min, 2975571 bytes/sec.
Requests: 1234109 susceed, 0 failed.  

  PS:然后我们可以通过 top 查看CPU的使用率的平均值,其实变化并不大 原因是因为默认的nginx不需要添加这个优化参数已经默认做了绑定,所有也可以只做一下了解

3、调整单个进程运行的最大连接数:

  控制这个优化的参数的值是nginx.conf 主配置文件中的 worker_connections 参数:

  语法:

  参数语法:worker_connections number

  默认配置:worker_connections 1024

  添加位置:events区块

  具体修改参数,如下:(红色标记为修改或者添加项)

[root@Nginx conf]# cat nginx.conf
worker_processes  4;
worker_cpu_affinity 0001 0010 0100 100;
error_log logs/error.log;
events {
    worker_connections  20480;      # 这个就是来设置nginx的最大连接数,最大连接数由worker_processes和worker_connections决定,设置参考:client=worker_processes*worker_connections
}
http {
    include       mime.types;
    server_tokens   on;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
include www_date/brian.conf;
include www_date/brianzjz.conf;
include www_date/status.conf;
}

4、worker进程打开的最大文件数:

  控制这个优化的参数的值是nginx.conf 主配置文件中的 worker_rlimit_nofile 参数:

  语法:

  参数语法:worker_rlimit_nofile number

  默认配置:无

  添加位置:主标签段

  说明:此参数的作用是改变worker procrsses 能打开的最大文件数

  具体修改参数,如下:(红色标记为修改或者添加项)

[root@Nginx conf]# cat nginx.conf
worker_processes  4;
worker_rlimit_nofile 65535;            # 添加此参数项 最大打开文件数,可设置为系统优化后的ulimit -HSn的结果
worker_cpu_affinity 0001 0010 0100 100;
error_log logs/error.log;
events {
    worker_connections  20480;
}
http {
    include       mime.types;
    server_tokens   on;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
include www_date/brian.conf;
include www_date/brianzjz.conf;
include www_date/status.conf;
} 

5、高效文件传输模式:

  这个优化是可由下面两个参数来设置完成:

  sendfile参数用于开启高效的文件传输,同时tcp_nopush和tcp_nodelay两个指令设置为no ,可防止网络及磁盘I/O阻塞,提升nginx的工作效率

  语法:

  参数语法:sendfile on | off;

  默认配置:sendfile off;

  添加位置:http、server、location、if in location 标签段

  参数作用:激活或者禁用sendfile()功能。sendfile()是作用于两个文件描述符之间的数据拷贝函数,这个拷贝操作是在内核之中完成的,被称为"零拷贝",sendfile比read和write函数要高效很多,因为read和write要把数据拷贝到应用层在进行操作

  具体修改参数,如下:(红色标记为修改或者添加项)

[root@Nginx conf]# cat nginx.conf
worker_processes  4;
worker_rlimit_nofile 65535;
worker_cpu_affinity 0001 0010 0100 100;
error_log logs/error.log;
events {
    worker_connections  20480;
}
http {
    include       mime.types;
    server_tokens   on;
    sendfile        on;                  # 添加此项
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
include www_date/brian.conf;
include www_date/brianzjz.conf;
include www_date/status.conf;
}

  tcp_nopush:参数作用:激活或者禁用Linux上的TCP_CORK socket选项,此选项仅仅当开启sendfile时才生效,激活这个.tcp_nopush参数可以吧允许把http response header和文件的开始部分放在一个文件里发布

  语法:

  参数语法:tcp_nopush on | off;

  默认配置:tcp_nopush off;

  添加位置:http、server、location 标签段

  具体修改参数,如下:(红色标记为修改或者添加项)

[root@Nginx conf]# cat nginx.conf
worker_processes  4;
worker_rlimit_nofile 65535;
worker_cpu_affinity 0001 0010 0100 100;
error_log logs/error.log;
events {
    worker_connections  20480;
}
http {
    include       mime.types;
    server_tokens   on;
    sendfile        on;
    tcp_nopush        on;              # 添加此项
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
include www_date/brian.conf;
include www_date/brianzjz.conf;
include www_date/status.conf;
}

 

6、连接超时时间:

  这个优化是可由下面参数来设置完成:

  keepalive_timeout参数用于设置客户端连接保持会话的超时时间,超过这个时间,服务器会关闭该连接

  语法:

  参数语法:keepalive_timeout  timeout [header_timeout];

  默认配置:keepalive_timeout 75s;

  添加位置:http、server、location、标签段

  参数作用:keep-alive可以使客户端到服务器端已经建立的连接一直工作不退出,当服务器有持续请求时,keep-alive会使用已经建立的连接提供服务,从而避免了重新建立连接处理请求。

  具体修改参数,如下:(红色标记为修改或者添加项)

[root@Nginx conf]# cat nginx.conf
worker_processes  4;
worker_rlimit_nofile 65535;
worker_cpu_affinity 0001 0010 0100 100;
error_log logs/error.log;
events {
    worker_connections  20480;
}
http {
    include       mime.types;
    server_tokens   on;
    sendfile        on;
    tcp_nopush        on;
    keepalive_timeout 65;         # 添加此项
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
include www_date/brian.conf;
include www_date/brianzjz.conf;
include www_date/status.conf;
}

   tcp_nodelay参数用于激活tcp_nodelay功能,提高I/O性能

  语法:

  参数语法:tcp_nodelay on | off;

  默认配置:tcp_nodelay on;

  添加位置:http、server、location 标签段

  具体修改参数,如下:(红色标记为修改或者添加项)

[root@Nginx conf]# cat nginx.conf
worker_processes  4;
worker_rlimit_nofile 65535;
worker_cpu_affinity 0001 0010 0100 100;
error_log logs/error.log;
events {
    worker_connections  20480;
}
http {
    include       mime.types;
    server_tokens   on;
    sendfile        on;
    tcp_nopush        on;
    tcp_nodelay     on;     # 添加此项
    keepalive_timeout 65;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
include www_date/brian.conf;
include www_date/brianzjz.conf;
include www_date/status.conf;
}

  client_header_timeout参数用于设置读取客户端请求头数据的超时时间

  语法:

  参数语法:client_header_timeout time;

  默认配置:client_header_timeout  60s;

  添加位置:http、server 标签段

  具体修改参数,如下:(红色标记为修改或者添加项)

[root@Nginx conf]# cat nginx.conf
worker_processes  4;
worker_rlimit_nofile 65535;
worker_cpu_affinity 0001 0010 0100 100;
error_log logs/error.log;
events {
    worker_connections  20480;
}
http {
    include       mime.types;
    server_tokens   on;
    sendfile        on;
    tcp_nopush        on;
    tcp_nodelay     on;
    keepalive_timeout 65;
    client_header_timeout 60;          # 添加此项
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
include www_date/brian.conf;
include www_date/brianzjz.conf;
include www_date/status.conf;
}

  client_boby_timeout参数用于设置度读取客户端请求主体的超时时间

  语法:

  参数语法:client_boby_timeout  time;

  默认配置:client_boby_timeout  60s;

  添加位置:http、server、location 标签段

  具体修改参数,如下:(红色标记为修改或者添加项)

[root@Nginx conf]# cat nginx.conf
worker_processes  4;
worker_rlimit_nofile 65535;
worker_cpu_affinity 0001 0010 0100 100;
error_log logs/error.log;
events {
    worker_connections  20480;
}
http {
    include       mime.types;
    server_tokens   on;
    sendfile        on;
    tcp_nopush        on;
    tcp_nodelay     on;
    keepalive_timeout 65;
    client_header_timeout 60;
    client_boby_timeout  60;     # 添加此项
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
include www_date/brian.conf;
include www_date/brianzjz.conf;
include www_date/status.conf;
}

  send_timeout参数用于指定响应客户端的超时时间

  语法:

  参数语法:send_timeout  time;

  默认配置:send_timeout  60s;

  添加位置:http、server、location 标签段

  具体修改参数,如下:(红色标记为修改或者添加项)

[root@Nginx conf]# cat nginx.conf
worker_processes  4;
worker_rlimit_nofile 65535;
worker_cpu_affinity 0001 0010 0100 100;
error_log logs/error.log;
events {
    worker_connections  20480;
}
http {
    include       mime.types;
    server_tokens   on;
    sendfile        on;
    tcp_nopush        on;
    tcp_nodelay     on;
    keepalive_timeout 65;
    client_header_timeout 60;
    client_boby_timeout  60;
    send_timeout    60;         # 添加此项
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
include www_date/brian.conf;
include www_date/brianzjz.conf;
include www_date/status.conf;
}

 

7、优化文件上传大小的限制:

  client_max_boby_size参数用于设置上传文件的大小

  语法:

  参数语法:client_max_boby_size  size;

  默认配置:client_max_boby_size   1m;

  添加位置:http、server、location 标签段

  参数作用:设置最大的允许的客户端请求主体的=大小,在请求头域有"Content-Length",如果超过了此项设置的值,客户端会收到413错误,意思是在请求的条目过大,有可能浏览器不能正常显示,设置为0表示禁止检查客户端请求主体大小,此参数对提高服务器的安全起到一定的作用

  具体修改参数,如下:(红色标记为修改或者添加项)

[root@Nginx conf]# cat nginx.conf
worker_processes  4;
worker_rlimit_nofile 65535;
worker_cpu_affinity 0001 0010 0100 100;
error_log logs/error.log;
events {
    worker_connections  20480;
}
http {
    include       mime.types;
    server_tokens   on;
    sendfile        on;
    tcp_nopush        on;
    tcp_nodelay     on;
    keepalive_timeout 65;
    client_header_timeout 60;
    client_boby_timeout  60;
    send_timeout    60;
    client_max_boby_size  8m;            # 添加此项
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
include www_date/brian.conf;
include www_date/brianzjz.conf;
include www_date/status.conf;
}

8、FastCGI参数优化(配合PHP动态服务):

  FastCGI参数是配合nginx向后端请求PHP动态引擎服务的相关参数,下面的相关参数都属于nginx的配置参数:

Nginx FastCGI 相关参数 说明
fastcgi_connect_timeout 表示Nginx服务器和后端FastCGI服务器连接的超时时间,默认值是60s,这个参数不要超过75s,因为建立的连接越多,消耗的资源就越多
fastcgi_send_timeout 设置nginx允许FastCGI服务器端返回数据的超时时间,默认60s
fastcgi_read_timeout 设置nginx从FastCGI服务器端读取响应信息的超时时间,表示连接建立成功后,nginx等待后端服务器的响应时间,是nginx已经进入后端的排队之中等候处理的时间
fastcgi_buffer_size 这是nginx FastCGI的缓冲区大小参数,用来读取从fastCGI服务器端收到的第一部分响应信息的缓冲区大小,这里的第一部分通常会包含一个小的响应头部
fastcgi_buffers 设定用来读取从FastCGI服务器端收到的响应信息的缓冲区大小和缓冲区数量,默认值为fastcgi_buffer 8 4k|8k;
proxy_busy_buffer_size 用于设置系统很忙时可以使用的proxy_buffers大小,官方推荐的大小是proxy_buffers*2
fastcgi_busy_buffer_size 用于设置系统很忙时可以使用的fastcgi_buffer大小,官方推荐大小为fastcgi_buffer*2
fastcgi_temp_file_write_size FastCGI临时文件的大小,可设置为128~256KB
fastcgi_cache brian_nginx 表示开启FastCGI缓存并为其指定一个名字
fastcgi_cache_path fastcgi_cache缓存目录设置
fastcgi_cache_valid 用来指定应答代码的缓存时间,示例:fastcgi_cache_valid 200 302 1h; 表示将200和302的应答缓存1小时
fastcgi_cache_min_uses 设置请求几次之后响应被缓存,1表示一次就被缓存
fastcgi_cache_use_stale 定义在什么情况下使用过期缓存,示例fastcgi_cache_use_stale error timeout invalid_header http_500 ;
fastcgi_cache_key

示例:fastcgi_cache_key $request_method://$host$request_uri;

示例:fastcgi_cache_key http://$host$request_uri;

定义fastcgi_cache的key,示例中以请求的uri作为缓存的key,nginx会取这个key的md5作为缓存文件,如果设置了缓存散列目录,nginx会从后往前取相应的位数作为目录

posted @ 2018-03-26 15:51  Brian_Zhu  阅读(506)  评论(0编辑  收藏  举报