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 ;
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的性能优化的更多相关文章
- Nginx配置性能优化与压力测试webbench【转】
这一篇我们来说Nginx配置性能优化与压力测试webbench. 基本的 (优化过的)配置 我们将修改的唯一文件是nginx.conf,其中包含Nginx不同模块的所有设置.你应该能够在服务器的/et ...
- Nginx配置性能优化
大多数的Nginx安装指南告诉你如下基础知识--通过apt-get安装,修改这里或那里的几行配置,好了,你已经有了一个Web服务器了.而且,在大多数情况下,一个常规安装的nginx对你的网站来说已经能 ...
- Nginx配置性能优化(转)
大多数的Nginx安装指南告诉你如下基础知识——通过apt-get安装,修改这里或那里的几行配置,好了,你已经有了一个Web服务器了.而且,在大多数情况下,一个常规安装的nginx对你的网站来说已经能 ...
- Nginx的性能优化方案
nginx的优化 . gzip压缩优化 . expires缓存有还 . 网络IO事件模型优化 . 隐藏软件名称和版本号 . 防盗链优化 . 禁止恶意域名解析 . 禁止通过IP地址访问网站 . HTTP ...
- [转] Nginx配置性能优化
大多数的Nginx安装指南告诉你如下基础知识——通过apt-get安装,修改这里或那里的几行配置,好了,你已经有了一个Web服务器了.而且,在大多数情况下,一个常规安装的nginx对你的网站来说已经能 ...
- 22、编译安装nginx及性能优化
22.1.编译安装nginx: 1.下载nginx: [root@slave-node1 ~]# mkdir -p /tools/ [root@slave-node1 ~]# cd /tools/ [ ...
- Nginx服务器性能优化与安全配置实践指南
转载自:https://www.bilibili.com/read/cv16151784?spm_id_from=333.999.0.0 1.引言 1.1 目的 为了更好的指导部署与测试艺术升系统ng ...
- 关于Nginx配置性能优化
基本的 (优化过的)配置 将修改的唯一文件是nginx.conf,其中包含Nginx不同模块的所有设置.在服务器的/etc/nginx目录中找到nginx.conf. 首先,我们将谈论一些全局设置,然 ...
- nginx https性能优化
影响HTTPS速度的主要原因:秘钥交换算法 常见的密钥交换算法有 RSA,ECDHE,DH,DHE 等算法.它们的特性如下: RSA:算法实现简单,诞生于 1977 年,历史悠久,经过了长时间的破解测 ...
随机推荐
- odoo开发笔记 -- 异常、错误、警告、提示、确认信息显示
1.检查业务逻辑中的错误,终止代码执行,显示错误或警告信息: raise osv.except_osv(_('Error!'), _('Error Message.')) 示例代码: #删除当前销售单 ...
- odoo API装饰器one、model、multi的区别
1.one装饰器详解 odoo新API中定义方式: date=fields.Date(string="date",compute="_get_date") ...
- (转)Cognos的下载地址分享
原文:https://blog.csdn.net/Wikey_Zhang/article/details/76138965 刚开始接触Cognos,发现Cognos真是一款挺不错的报表工具,先分享一下 ...
- C++ 操作符、局部 全局变量及自动转换原则
1.&:表示与操作,eg:2&1=2&&:表示and操作,eg:1&&0=0|:表示或操作,eg:2|1=3||:表示or操作,eg:1||0=12.全 ...
- 在Postgresql中添加新角色(Role)
Postgresql安装完成之后,默认会创建名为postgres的用户.角色(Role)和数据库(Database).而使用你自己原有的用户运行psql时会提示错误. bob@localhost:~$ ...
- 使用控制台程序搭建WebApi
原文参考: ASP.NET Web Api 2.2: Create a Self-Hosted OWIN-Based Web Api from Scratch 新建控制台程序,引入Owin包 PM&g ...
- Linux环境下Java中文乱码解决方案
相信很多朋友遇到过Java的乱码问题,最近我也在解决一个“使用文本生成图片过程中中文以及特殊字符乱码”的问题:花了我大量时间,Debug了sun.font.sun.awt下面的各种源码,终于搞懂了其机 ...
- 如何测试你给客户端app开的接口
这里介绍一款工具用于测试后台给客户端开的接口. 采用http或者https 采用表单或者json格式 这款工具之前是谷歌浏览器的一款插件,后来出现了各个平台的客户端.非常实用. 名叫postman 官 ...
- 每一行代码都有记录—如何用git一步步探索项目的历史
每一行代码都有一块被隐藏了的文档信息. 下面的代码片段不管是谁写的,其第4行因为某些原因要访问一个DOM结点的clientLeft属性,但却对结果不作任何处理.这十分的莫名其妙,你能告诉我他们为什么要 ...
- Java并发编程-synchronized指南
在多线程程序中,同步修饰符用来控制对临界区代码的访问.其中一种方式是用synchronized关键字来保证代码的线程安全性.在Java中,synchronized修饰的代码块或方法不会被多个线程并发访 ...