前面我们完成了nginx在centos下部署《【nginx学习一】Linux centos7虚拟机下安装Nginx》,这里我们主要学习nginx的配置及性能调优。
1、查看Nginx配置文件
启动Nginx,
进入安装nginx安装目录:cd /usr/local/nginx/conf
查看nginx主配置文件(如下):vi nginx.conf
#--------全局配置模块开始---------------
#user nobody;
worker_processes 1;
#error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info;
#pid logs/nginx.pid;
#--------全局配置模块结束---------------
#--------事件模块开始---------------
events {
worker_connections 1024;
}
#--------事件模块结束-------
#--------http模块开始-------
http {
#--------http模块全局子模块开始-------
include mime.types;
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"';
#access_log logs/access.log main;
sendfile on;
#tcp_nopush on;
#keepalive_timeout 0;
keepalive_timeout 65;
#gzip on;
#--------http模块全局子模块结束-------
#--------默认虚拟主机模块(可以有多个)开始-------
server {
listen 80;
server_name localhost;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
root html;
index index.html index.htm;
}
#error_page 404 /404.html;
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}
# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}
# another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias;
# location / {
# root html;
# index index.html index.htm;
# }
#}
# HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost;
# ssl_certificate cert.pem;
# ssl_certificate_key cert.key;
# ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m;
# ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on;
# location / {
# root html;
# index index.html index.htm;
# }
#}
}
#--------http模块结束-------
主要分为:
2、全局模块的调优
(1)、worker processes设置
work processes,工作进程,用于指定Nginx的工作进程数量,一般设置为CPU内核数量,或内核的整数倍。该值不完全取决于CPU内核数量,与硬盘数量及负载均衡模式相关,不确定时(工作中)可以指定为 auto。
查看默认nginx进程:ps aux|grep nginx
显示一个master process 和 一个 worker process
设置work process为多个,假设设置为2个,vi nginx.conf编辑,设置 worker_process 2; !wq 保存退出
重新加载nginx:nginx -s reload
查看nginx进程:出现两个worker process
(2)、worker_cpu_affinity
将worker进程与具体的内核进程绑定。通过二进制进行的,每个内核使用一个二进制表示,0代表内核关闭,1代表内核开启。有几个内核就有几个二进制位。
根据(1)中设置两个内核进程,设置第一个内核进程开启,第二个内核进程为开启,设置如下:
vi nginx.conf
配置Nginx多核CPU,worker_cpu_affinity使用方法和范例:
2核CPU,开启2个进程
worker_processes 2;
worker_cpu_affinity 01 10;
01表示启用第一个CPU内核,10表示启用第二个CPU内核
worker_cpu_affinity 01 10;表示开启两个进程,第一个进程对应着第一个CPU内核,第二个进程对应着第二个CPU内核。
2核CPU,开启4个进程
worker_processes 4;
worker_cpu_affinity 01 10 01 10;
开启了四个进程,它们分别对应着开启2个CPU内核
4核CPU,开户4个进程
worker_processes 4;
worker_cpu_affinity 0001 0010 0100 1000;
0001表示启用第一个CPU内核,0010表示启用第二个CPU内核,依此类推
4核CPU,开启2个进程
worker_processes 2;
worker_cpu_affinity 0101 1010;
0101表示开启第一个和第三个内核,1010表示开启第二个和第四个内核
2个进程对应着四个内核
worker_cpu_affinity配置是写在/etc/nginx/nginx.conf里面的。
2核是 01,四核是0001,8核是00000001,有多少个核,就有几位数,1表示该内核开启,0表示该内核关闭。
8核CPU,开户8个进程
worker_processes 8;
worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
0001表示启用第一个CPU内核,0010表示启用第二个CPU内核,依此类推。
worker_processes最多开启8个,8个以上性能提升不会再提升了,而且稳定性变得更低,所以8个进程够用了。
(3)、worker_rlimit_nofile
设置每个worker进程所能打开的最大文件数,默认值与linux系统(查看可打开文件数命令:ulimit -n)可以打开的最大文件数一样 。
3、events模块的调优
(1)、worker_connections default 1024
worker进程可以处理最大并发连接数。该值不能超过worker_rlimit_nofile 的值。
(2)、accept_mutex default on
on:当有新连接时,处于空闲工作进程(work process)将以串连方式来处理,唤醒阻塞队列中队首进行处理,其他工作进程继续保持阻塞;
off:当有新连接时,所有空闲工作进程都会被唤醒,不过只有一个工作进程会能获取连接,其他工作进程进入阻塞状态,即惊群现象。
(3)、accept_mutex_delay default 500ms
accept_mutex_delay使用的前提是accept_mutex设置为on。该属性用于设置队首工作进程尝试获取请求(互斥资源)的时间间隔。
(4)、multi_accept default on
工作进程都在工作时,新连接的处理机制。
on:当有多个新连接时,系统会按照负载均衡的策略分配连接给工作进程。
off:系统会实时统计各个工作进程(worker process)当前正在处理的连接个数,然后会按照每个工作进程正在处理的连接个数将多个新连接一次性分配给当前连接数量最少的工作进程(缺编)。
(5)、use 推荐 epoll
use用来指定Nginx的工作模式,设置 worker 与后端服务器连接的处理方式。Nginx 会自动选择适合当前系统的最高效的方式,可以使用 use 配置指定所要使用的连接处理方式。user 的取值有以下几种: select | poll | epoll | rtsig | kqueue | /dev/poll 。
a、select | poll | epoll
三种多路复用机制:select和poll都是标准的工作模式,epoll是高效的工作模式,推荐使用。
b、rtsig
realtime signal,实时信号,Linux 2.2.19+的高效连接处理方式。但在 在 Linux 2.6 版本后, 不再支持该方式。
c、kqueue
与epoll一样高效,应用在 BSD 系统(Berkeley Software Distribution,伯克利软件套件,Unix的衍生系统)上。
d、/dev/poll
UNIX 系统上使用的 poll。
3、http模块的调优
(1)非调优配置
a、include mime.types;
将当前目录(conf 目录)中的 mime.types 文件包含进来。
b、default_type application/octet-stream;
对于无扩展名的文件,默认其为 application/octet-stream 类型,即 Nginx 会将其作为一 个八进制流文件来处理。
c、charset utf-8;
设置请求与响应的字符编码。
(2)、sendfile
设置为 on 则开启 Linux 系统的零拷贝机制,否则不启用零拷贝。当然,开启后是否起 作用,要看所使用的系统版本。CentOS6 及其以上版本支持 sendfile 零拷贝。
(3)、tcp_nopush (default off,推荐使用on)
a、 on:以单独的数据包形式发送 Nginx 的响应头信息,而真正的响应体数据会再以数据包的形式发送,这个数据包中就不再包含响应头信息了。当向客户端响应的数据量较大时, 取 on 值,可以降低数据冗余度,减少数据发送量。
b、 off:默认值,响应头信息包含在每一个响应体数据包中。
(4)、tcp_nodelay
a、 on:不设置数据发送缓存,即不推迟发送,适合于传输小数据,无需缓存。
b、 off:开启发送缓存。若传输的数据是图片等大数据量文件,则建议设置为 off。
(5)、keepalive_timeout
设置客户端与Nginx间所建立的长连接的生命超时时间,时间到达,则连接将自动关闭。 单位秒。设置0是禁止长连接。
(6)、keepalive_requests
设置一个长连接最多可以发送的请求数,可设置与并发量正相关。
更多配置,请参考:https://www.cnblogs.com/paul8339/p/6943396.html