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 ;
worker_cpu_affinity 0001 0010 0100 100; # 添加此行内容(这个配置nginx进程与cpu核心数的亲和力参数,就是把不同的进程给到不同的CPU)
error_log logs/error.log;
events {
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"';
sendfile on;
keepalive_timeout ;
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 ;
worker_cpu_affinity ;
error_log logs/error.log;
events {
worker_connections ; # 这个就是来设置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 ;
worker_rlimit_nofile ; # 添加此参数项 最大打开文件数,可设置为系统优化后的ulimit -HSn的结果
worker_cpu_affinity ;
error_log logs/error.log;
events {
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;

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 ;
worker_rlimit_nofile ;
worker_cpu_affinity ;
error_log logs/error.log;
events {
worker_connections ;
}
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 ;
worker_rlimit_nofile ;
worker_cpu_affinity ;
error_log logs/error.log;
events {
worker_connections ;
}
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 ;
worker_rlimit_nofile ;
worker_cpu_affinity ;
error_log logs/error.log;
events {
worker_connections ;
}
http {
include mime.types;
server_tokens on;
sendfile on;
tcp_nopush on;
keepalive_timeout ; # 添加此项
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 ;
worker_rlimit_nofile ;
worker_cpu_affinity ;
error_log logs/error.log;
events {
worker_connections ;
}
http {
include mime.types;
server_tokens on;
sendfile on;
tcp_nopush on;
tcp_nodelay on; # 添加此项
keepalive_timeout ;
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 ;
worker_rlimit_nofile ;
worker_cpu_affinity ;
error_log logs/error.log;
events {
worker_connections ;
}
http {
include mime.types;
server_tokens on;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout ;
client_header_timeout ; # 添加此项
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 ;
worker_rlimit_nofile ;
worker_cpu_affinity ;
error_log logs/error.log;
events {
worker_connections ;
}
http {
include mime.types;
server_tokens on;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout ;
client_header_timeout ;
client_boby_timeout ; # 添加此项
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 ;
worker_rlimit_nofile ;
worker_cpu_affinity ;
error_log logs/error.log;
events {
worker_connections ;
}
http {
include mime.types;
server_tokens on;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout ;
client_header_timeout ;
client_boby_timeout ;
send_timeout ; # 添加此项
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 ;
worker_rlimit_nofile ;
worker_cpu_affinity ;
error_log logs/error.log;
events {
worker_connections ;
}
http {
include mime.types;
server_tokens on;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout ;
client_header_timeout ;
client_boby_timeout ;
send_timeout ;
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会从后往前取相应的位数作为目录

Nginx的性能优化的更多相关文章

  1. Nginx配置性能优化与压力测试webbench【转】

    这一篇我们来说Nginx配置性能优化与压力测试webbench. 基本的 (优化过的)配置 我们将修改的唯一文件是nginx.conf,其中包含Nginx不同模块的所有设置.你应该能够在服务器的/et ...

  2. Nginx配置性能优化

    大多数的Nginx安装指南告诉你如下基础知识--通过apt-get安装,修改这里或那里的几行配置,好了,你已经有了一个Web服务器了.而且,在大多数情况下,一个常规安装的nginx对你的网站来说已经能 ...

  3. Nginx配置性能优化(转)

    大多数的Nginx安装指南告诉你如下基础知识——通过apt-get安装,修改这里或那里的几行配置,好了,你已经有了一个Web服务器了.而且,在大多数情况下,一个常规安装的nginx对你的网站来说已经能 ...

  4. Nginx的性能优化方案

    nginx的优化 . gzip压缩优化 . expires缓存有还 . 网络IO事件模型优化 . 隐藏软件名称和版本号 . 防盗链优化 . 禁止恶意域名解析 . 禁止通过IP地址访问网站 . HTTP ...

  5. [转] Nginx配置性能优化

    大多数的Nginx安装指南告诉你如下基础知识——通过apt-get安装,修改这里或那里的几行配置,好了,你已经有了一个Web服务器了.而且,在大多数情况下,一个常规安装的nginx对你的网站来说已经能 ...

  6. 22、编译安装nginx及性能优化

    22.1.编译安装nginx: 1.下载nginx: [root@slave-node1 ~]# mkdir -p /tools/ [root@slave-node1 ~]# cd /tools/ [ ...

  7. Nginx服务器性能优化与安全配置实践指南

    转载自:https://www.bilibili.com/read/cv16151784?spm_id_from=333.999.0.0 1.引言 1.1 目的 为了更好的指导部署与测试艺术升系统ng ...

  8. 关于Nginx配置性能优化

    基本的 (优化过的)配置 将修改的唯一文件是nginx.conf,其中包含Nginx不同模块的所有设置.在服务器的/etc/nginx目录中找到nginx.conf. 首先,我们将谈论一些全局设置,然 ...

  9. nginx https性能优化

    影响HTTPS速度的主要原因:秘钥交换算法 常见的密钥交换算法有 RSA,ECDHE,DH,DHE 等算法.它们的特性如下: RSA:算法实现简单,诞生于 1977 年,历史悠久,经过了长时间的破解测 ...

随机推荐

  1. vs 2017 IIS EXPRESS 增加局域网访问

    在VS调试站点,默认使用IISExpress,locall+端口,为了使用IP地址.多域名调试,找到 IISExpress下的applicationhost.config,在目标站点下增加类似行: & ...

  2. 关于SVM(support vector machine)----支持向量机的一个故事

    一.预告篇: 很久很久以前,有个SVM, 然后,……………………被deep learning 杀死了…………………………………… . 完结……撒花 二.正式篇 好吧,关于支持向量机有一个故事 ,故事是 ...

  3. JDK中线程组ThreadGroup

    如果线程有100条...分散的不好管理... 线程同样可以分组ThreadGroup类. 线程组表示一个线程的集合.此外,线程组也可以包含其他线程组.线程组构成一棵树,在树中,除了初始线程组外,每个线 ...

  4. (转) lsof 一切皆文件

    原文:https://linuxtools-rst.readthedocs.io/zh_CN/latest/tool/lsof.html lsof(list open files)是一个查看当前系统文 ...

  5. React VR 技术开发群 579149907

    React VR 技术开发群 579149907,欢迎加入讨论!分享经验!

  6. Google Optimization Tools实现员工排班计划Scheduling【Python版】

    上一篇介绍了<使用.Net Core与Google Optimization Tools实现员工排班计划Scheduling>,这次将Google官方文档python实现的版本的完整源码献 ...

  7. ThreadPoolExecutor 中的 shutdown() 、 shutdownNow() 、 awaitTermination() 的用法和区别

    Java并发编程中在使用到ThreadPoolExecutor时,对它的三个关闭方法(shutdown().shutdownNow().awaitTermination())的异同点如下: shutd ...

  8. Kafka 副本失效

    Kafka源码注释中说明了一般有两种情况会导致副本失效: follower副本进程卡住,在一段时间内根本没有想leader副本发起同步请求,比如频繁的Full GC. follower副本进程同步过慢 ...

  9. redisTemplate实现轻量级消息队列, 异步处理excel并实现腾讯云cos文件上传下载

    背景 公司项目有个需求, 前端上传excel文件, 后端读取数据.处理数据.返回错误数据, 最简单的方式同步处理, 客户端上传文件后一直阻塞等待响应, 但用户体验无疑很差, 处理数据可能十分耗时, 没 ...

  10. windows系统搭建禅道系统(BUG管理工具)

    我也呆过三家公司了,用过的BUG管理工具也是五花八门的,常见的一般有禅道,bugzilla,jira等 个人比较推荐禅道,功能强大,主页的说明文档也是相当详细,最主要的是,用的人比较多,出现使用问题一 ...